Skip to content

Commit

Permalink
feat: capture output in case of an error in the routine
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Aug 3, 2019
1 parent 6073db4 commit c9ab26d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/factories/createOutputInterceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,16 @@ export default (userConfiguration?: OutputInterceptorUserConfigurationType): Out
output: '',
};

const result = await domain.run(() => {
return routine();
});
let routineResult;
let routineError;

try {
routineResult = await domain.run(() => {
return routine();
});
} catch (error) {
routineError = error;
}

let output = domain.outputInterceptor.output;

Expand All @@ -70,7 +77,11 @@ export default (userConfiguration?: OutputInterceptorUserConfigurationType): Out

interceptor.output = output;

return result;
if (routineError) {
throw routineError;
}

return routineResult;
};

interceptor.output = null;
Expand Down
18 changes: 18 additions & 0 deletions test/output-interceptor/factories/createOutputInterceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,21 @@ test('distinguishes asynchronous execution domains', async (t) => {
t.is(await run2, 'bar:1\nbar:2\nbar:3\n');
t.is(await run3, 'baz:1\nbaz:2\nbaz:3\n');
});

test('captures output in case of an error', async (t) => {
const intercept = createOutputInterceptor({
interceptStderr: false,
});

// eslint-disable-next-line require-await
await t.throwsAsync(intercept(async () => {
console.log('foo');

throw new Error('test');
}));

const actual = intercept.output;
const expected = 'foo\n';

t.is(actual, expected);
});

0 comments on commit c9ab26d

Please sign in to comment.