Skip to content

Commit

Permalink
Merge pull request #1688 from snyk/refactor/generate-test-error
Browse files Browse the repository at this point in the history
refactor: generate snyk test error
  • Loading branch information
lili2311 authored Mar 8, 2021
2 parents e83bdea + 4e9ef13 commit 0485089
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
25 changes: 25 additions & 0 deletions src/cli/commands/test/generate-snyk-test-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function generateSnykTestError(error) {
// Possible error cases:
// - the test found some vulns. `error.message` is a
// JSON-stringified
// test result.
// - the flow failed, `error` is a real Error object.
// - the flow failed, `error` is a number or string
// describing the problem.
//
// To standardise this, make sure we use the best _object_ to
// describe the error.
let errorResponse;
if (error instanceof Error) {
errorResponse = error;
} else if (typeof error !== 'object') {
errorResponse = new Error(error);
} else {
try {
errorResponse = JSON.parse(error.message);
} catch (unused) {
errorResponse = error;
}
}
return errorResponse;
}
29 changes: 5 additions & 24 deletions src/cli/commands/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {

import * as iacLocalExecution from './iac-local-execution';
import { validateCredentials } from './validate-credentials';
import { generateSnykTestError } from './generate-snyk-test-error';

const debug = Debug('snyk-test');
const SEPARATOR = '\n-------------------------------------------------------\n';
Expand All @@ -68,8 +69,6 @@ const showVulnPathsMapping: Record<string, ShowVulnPaths> = {
// TODO: avoid using `as any` whenever it's possible

async function test(...args: MethodArgs): Promise<TestCommandResult> {
const resultOptions = [] as any[];
const results = [] as any[];
let options = ({} as any) as Options & TestOptions;

if (typeof args[args.length - 1] === 'object') {
Expand Down Expand Up @@ -119,6 +118,9 @@ async function test(...args: MethodArgs): Promise<TestCommandResult> {
}
}

const resultOptions = [] as any[];
const results = [] as any[];

// Promise waterfall to test all other paths sequentially
for (const path of args as string[]) {
// Create a copy of the options so a specific test can
Expand All @@ -142,28 +144,7 @@ async function test(...args: MethodArgs): Promise<TestCommandResult> {
options.iacDirFiles = testOpts.iacDirFiles;
}
} catch (error) {
// Possible error cases:
// - the test found some vulns. `error.message` is a
// JSON-stringified
// test result.
// - the flow failed, `error` is a real Error object.
// - the flow failed, `error` is a number or string
// describing the problem.
//
// To standardise this, make sure we use the best _object_ to
// describe the error.

if (error instanceof Error) {
res = error;
} else if (typeof error !== 'object') {
res = new Error(error);
} else {
try {
res = JSON.parse(error.message);
} catch (unused) {
res = error;
}
}
res = generateSnykTestError(error);
}

// Not all test results are arrays in order to be backwards compatible
Expand Down

0 comments on commit 0485089

Please sign in to comment.