Skip to content

Commit a024bd2

Browse files
nowellsNickHeiner
authored andcommitted
Add additional error metadata. (#8)
1 parent 384b08b commit a024bd2

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

lib/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,29 @@ function spawn(command, args, options, done) {
3232
child.on('error', onerror)
3333
child.on('close', onclose)
3434

35+
function fixUpError(error) {
36+
error.stdout = out
37+
error.stderr = err
38+
error.command = command
39+
error.args = args
40+
error.options = options
41+
error.killed = child.killed
42+
return error
43+
}
44+
3545
function onerror(err) {
3646
cleanup()
37-
done(err)
47+
done(fixUpError(err))
3848
}
3949

4050
function onclose(code, signal) {
4151
cleanup()
4252
if (code === 0 && signal == null) return done(null, out)
4353

4454
var error = new Error('Command failed: ' + command + '\n' + err)
45-
error.stdout = out
46-
error.stderr = err
47-
error.killed = child.killed
4855
error.code = code
4956
error.signal = signal
50-
done(error)
57+
done(fixUpError(error))
5158
}
5259

5360
function onoutdata(chunk) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "co-child-process",
33
"description": "easily spawn a child process with co",
4-
"version": "1.0.0",
4+
"version": "1.1.0",
55
"author": {
66
"name": "Jonathan Ong",
77
"email": "[email protected]",

test/spawn.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,30 @@ describe('co-spawn', function () {
2424

2525
it('should work with spawn errors', () => co(function* () {
2626
try {
27-
yield spawn('laksjdflkajsdf')
27+
yield spawn('laksjdflkajsdf', ['foo', 'bar'], {cwd: __dirname})
2828
throw new Error('wtf')
2929
} catch (err) {
3030
err.code.should.equal('ENOENT')
31+
err.stdout.should.equal('')
32+
err.stderr.should.equal('')
33+
err.command.should.equal('laksjdflkajsdf')
34+
err.args.should.deepEqual(['foo', 'bar'])
35+
err.options.should.deepEqual({cwd: __dirname})
3136
}
3237
}))
3338

3439
it('should work with program errors', () => co(function* () {
3540
try {
36-
yield spawn(path.join(__dirname, 'fixtures', 'error-bin.js'))
41+
yield spawn(path.join(__dirname, 'fixtures', 'error-bin.js'), ['foo', 'bar'], {cwd: __dirname})
3742
throw new Error('wtf')
3843
} catch (err) {
3944
err.code.should.equal(1)
4045
err.stdout.should.equal('stdout output\n')
4146
err.stderr.should.containEql('stderr output\n')
4247
err.stderr.should.containEql('Error: Error message\n')
48+
err.command.should.equal(path.join(__dirname, 'fixtures', 'error-bin.js'))
49+
err.args.should.deepEqual(['foo', 'bar'])
50+
err.options.should.deepEqual({cwd: __dirname})
4351
}
4452
}))
4553
})

0 commit comments

Comments
 (0)