Skip to content

Commit fa5f47c

Browse files
committed
Handle errors properly
This groups all streams up with multipipe, except for bl. bl will catch any errors from the multipipe stream and pass them onto the `done` callback appropriately. Fixes #3
1 parent 0a55851 commit fa5f47c

File tree

9 files changed

+57
-4
lines changed

9 files changed

+57
-4
lines changed
File renamed without changes.
File renamed without changes.

fixtures/errors/error.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const through = require('through2')
2+
3+
module.exports = function () {
4+
return through(function () {
5+
var stream = this
6+
7+
setTimeout(function () {
8+
stream.emit('error', new Error('Hello world!'))
9+
}, 500)
10+
})
11+
}

fixtures/errors/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const fs = require('fs')
2+
3+
console.log(fs.readFileSync(__dirname + '/package.json', 'utf8'))

fixtures/errors/no-error.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const through = require('through2')
2+
3+
module.exports = function () {
4+
return through()
5+
}

fixtures/errors/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "fixture",
3+
"version": "1.0.0",
4+
"browserify": {
5+
"transform": [
6+
"./error",
7+
"./no-error",
8+
"brfs"
9+
]
10+
}
11+
}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ function loader (source) {
6969
transforms = []
7070
.concat(from2([source]))
7171
.concat(transforms)
72-
.concat(bl(done))
7372

7473
multipipe.apply(this, transforms)
74+
.pipe(bl(done))
7575
})
7676
})
7777
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"standard": "^5.4.1",
2525
"tap-spec": "^4.1.1",
2626
"tape": "^4.4.0",
27+
"through2": "^2.0.0",
2728
"webpack": "^1.12.9"
2829
},
2930
"scripts": {

test.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ const vm = require('vm')
77

88
test('ify-loader', function (t) {
99
const wpack = which.sync('webpack', { cwd: __dirname })
10-
const input = path.join(__dirname, 'fixture', 'index.js')
11-
const output = path.join(__dirname, 'fixture', 'bundle.js')
12-
const pkg = path.join(__dirname, 'fixture', 'package.json')
10+
const input = path.join(__dirname, 'fixtures', 'basic', 'index.js')
11+
const output = path.join(__dirname, 'fixtures', 'basic', 'bundle.js')
12+
const pkg = path.join(__dirname, 'fixtures', 'basic', 'package.json')
1313

1414
t.plan(1)
1515

@@ -38,3 +38,25 @@ test('ify-loader', function (t) {
3838
})
3939
})
4040
})
41+
42+
test('error handling', function (t) {
43+
const wpack = which.sync('webpack', { cwd: __dirname })
44+
const input = path.join(__dirname, 'fixtures', 'errors', 'index.js')
45+
const output = path.join(__dirname, 'fixtures', 'errors', 'bundle.js')
46+
47+
t.plan(1)
48+
49+
spawn(wpack, [
50+
input,
51+
output,
52+
'--module-bind', 'js=' + __dirname
53+
], {
54+
stdio: ['pipe', 'pipe', 2]
55+
}).once('exit', function (code) {
56+
try {
57+
fs.unlinkSync(output)
58+
} catch (e) {}
59+
60+
t.equal(code, 0, 'exit code zero (they should probably fix that...)')
61+
})
62+
})

0 commit comments

Comments
 (0)