Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
[changed] NativeProcess converts stderr strings that look like th…
Browse files Browse the repository at this point in the history
…e output of `Error.prototype.toString()` into actual error instances, preserving all of the underlying information
  • Loading branch information
thealjey committed Jan 29, 2017
1 parent aae39ea commit 42d1413
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@
"babel-preset-stage-2": "^6.22.0",
"babel-runtime": "^6.22.0",
"cheerio": "^0.22.0",
"clean-stack": "^1.1.1",
"codemirror": "^5.23.0",
"docdash": "^0.4.0",
"error-stack-parser": "^2.0.0",
"eslint": "^3.14.1",
"eslint-plugin-babel": "^4.0.1",
"eslint-plugin-flowtype": "^2.30.0",
Expand Down
30 changes: 29 additions & 1 deletion src/NativeProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type {StringOrErrorCallback} from './typedef';
import {spawn} from 'child_process';
import noop from 'lodash/noop';

const errorPattern = /([^\s]*Error): (.*)[\s\S]*/;

/**
* Encapsulates a {@link https://nodejs.org/api/child_process.html#child_process_class_childprocess ChildProcess}
* instance of a `task`
Expand Down Expand Up @@ -46,6 +48,32 @@ export class NativeProcess {
this.task = task;
}

/**
* If the `stderr` string matches the return format of `Error.prototype.toString()`, turns it into an actual error
* instance, preserving all of the underlying information.
*
* @memberOf NativeProcess
* @static
* @private
* @method stderrToError
* @param {string} stderr - error output
* @return {Error} an error instance
*/
static stderrToError(stderr: string): Error {
const matched = stderr.match(errorPattern);

if (!matched) {
return new Error(stderr);
}
const [stack, name, message] = matched,
error = new Error(message);

error.name = name;
error.stack = stack;

return error;
}

/**
* Execute the command
*
Expand Down Expand Up @@ -89,7 +117,7 @@ export class NativeProcess {
});
this.proc.on('close', code => {
this.proc = null;
callback(code ? new Error(stderr) : null, stdout);
callback(code ? NativeProcess.stderrToError(stderr) : null, stdout);
});
}

Expand Down
31 changes: 30 additions & 1 deletion test/NativeProcess.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ chai.use(sinonChai);

/* eslint-disable no-unused-expressions */

let cmp, callback, on, kill, stdoutOn, stderrOn;
let cmp, callback, on, kill, stdoutOn, stderrOn, errorResult;

describe('NativeProcess', () => {

Expand All @@ -24,6 +24,35 @@ describe('NativeProcess', () => {
expect(cmp.task).equal('script');
});

describe('stderrToError plain error message', () => {

beforeEach(() => {
errorResult = NativeProcess.stderrToError('something is wrong');
});

it('returns result', () => {
expect(errorResult.name).equal('Error');
expect(errorResult.message).equal('something is wrong');
});

});

describe('stderrToError matches format of `Error.prototype.toString()`', () => {

beforeEach(() => {
errorResult = NativeProcess.stderrToError(`Something unusable SyntaxError: something is wrong again
at <anonymous>:1:1`);
});

it('returns result', () => {
expect(errorResult.name).equal('SyntaxError');
expect(errorResult.message).equal('something is wrong again');
expect(errorResult.stack).equal(`SyntaxError: something is wrong again
at <anonymous>:1:1`);
});

});

describe('run', () => {

describe('already running', () => {
Expand Down

0 comments on commit 42d1413

Please sign in to comment.