Skip to content

Commit

Permalink
feat: improve sub-process debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Jdunsby committed Jul 22, 2022
1 parent 0af7de4 commit 21b6857
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/error-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function formatGenericPluginError(
'on this project.\n\n' +
(mavenCommand.indexOf('mvnw') >= 0 ? mvnwCommandTipMessage : '') +
'If the problem persists, collect the output of `' +
'DEBUG=* ' +
fullCommand +
'` and contact support@snyk.io\n'
);
Expand Down
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const WRAPPERS = ['mvnw', 'mvnw.cmd'];
// To enable debugging output, use `snyk -d`
let logger: debugModule.Debugger | null = null;

export function debug(s: string) {
export function debug(...messages: string[]) {
if (logger === null) {
// Lazy init: Snyk CLI needs to process the CLI argument "-d" first.
// TODO(BST-648): more robust handling of the debug settings
Expand All @@ -30,7 +30,7 @@ export function debug(s: string) {
}
logger = debugModule('snyk-mvn-plugin');
}
logger(s);
messages.forEach((m) => logger(m));
}

export interface MavenOptions extends legacyPlugin.BaseInspectOptions {
Expand Down
25 changes: 24 additions & 1 deletion lib/sub-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,30 @@ export function execute(command, args, options): Promise<string> {

proc.on('close', (code) => {
if (code !== 0) {
return reject(new Error(stderr || stdout));
debug(
`Child process failed with exit code: ${code}`,
'----------------',
'STDERR:',
stderr,
'----------------',
'STDOUT:',
stdout,
'----------------',
);

const stdErrMessage = stderr ? `\nSTDERR:\n${stderr}` : '';
const stdOutMessage = stdout ? `\nSTDOUT:\n${stdout}` : '';
const debugSuggestion = process.env.DEBUG
? ''
: `\nRun in debug mode (-d) to see STDERR and STDOUT.`;

return reject(
new Error(
`Child process failed with exit code: ${code}.` +
debugSuggestion +
(stdErrMessage || stdOutMessage),
),
);
}
resolve(stdout || stderr);
});
Expand Down
3 changes: 2 additions & 1 deletion tests/system/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ test('inspect on root that does not contain a pom.xml and no target file', async
if (err instanceof Error) {
t.match(
err.message,
'BUILD FAILURE',
'Child process failed with exit code: 1.',
'should throw expected error with build failure message',
);
t.match(
Expand Down Expand Up @@ -235,6 +235,7 @@ test('inspect on mvn error', async (t) => {
'` executes successfully ' +
'on this project.\n\n' +
'If the problem persists, collect the output of `' +
'DEBUG=* ' +
fullCommand +
'` and contact support@snyk.io\n';
t.match(
Expand Down

0 comments on commit 21b6857

Please sign in to comment.