Skip to content

Commit

Permalink
refactor to handle exec.exec return values together
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Fyson committed Sep 2, 2020
1 parent 6ef5334 commit 231537b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
19 changes: 10 additions & 9 deletions lib/exec_wrapper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/exec_wrapper.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions src/exec_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,30 @@ export async function exec_wrapper(commandLine: string, args?: string[],
};

// we capture the original return code and error so that (if no match is found) we can duplicate the behaviour
let originalReturnCode: number;
let originalError: Error|undefined;
let originalReturnValue: Error|number;
try {
originalReturnCode = await exec.exec(
originalReturnValue = await exec.exec(
commandLine,
args,
{
listeners: listeners,
...options
});
} catch (e) {
originalError = e;
originalReturnCode = 1; // TODO linter insists, but presumably there's a better way to do _all_ this...
originalReturnValue = e;
}

if (matchers) {
for (const [customCode, regex, message] of matchers) {
if (customCode === originalReturnCode || regex.test(stderr) || regex.test(stdout) ) {
if (customCode === originalReturnValue || regex.test(stderr) || regex.test(stdout) ) {
throw new Error(message);
}
}
}

if (originalError) throw originalError;
return originalReturnCode;
if (typeof originalReturnValue === 'number') {
return originalReturnValue;
} else {
throw originalReturnValue;
}
}

0 comments on commit 231537b

Please sign in to comment.