Skip to content

Commit

Permalink
Merge pull request #1508 from snyk/fix/improve-error-message-for-scan…
Browse files Browse the repository at this point in the history
…ning-an-image-that-doesnt-exist

fix: Improve error message for scanning an image that doesn't exist
  • Loading branch information
ahmed-agabani-snyk committed Nov 5, 2020
2 parents 47189a5 + a5ccb25 commit 90b61dd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/lib/ecosystems/monitor.ts
Expand Up @@ -11,7 +11,11 @@ import { getPlugin } from './plugins';
import { BadResult, GoodResult } from '../../cli/commands/monitor/types';
import { formatMonitorOutput } from '../../cli/commands/monitor/formatters/format-monitor-response';
import { getExtraProjectCount } from '../plugins/get-extra-project-count';
import { AuthFailedError, MonitorError } from '../errors';
import {
AuthFailedError,
DockerImageNotFoundError,
MonitorError,
} from '../errors';
import {
Ecosystem,
ScanResult,
Expand All @@ -32,11 +36,24 @@ export async function monitorEcosystem(
const plugin = getPlugin(ecosystem);
const scanResultsByPath: { [dir: string]: ScanResult[] } = {};
for (const path of paths) {
await spinner(`Analyzing dependencies in ${path}`);
options.path = path;
const pluginResponse = await plugin.scan(options);
scanResultsByPath[path] = pluginResponse.scanResults;
spinner.clearAll();
try {
await spinner(`Analyzing dependencies in ${path}`);
options.path = path;
const pluginResponse = await plugin.scan(options);
scanResultsByPath[path] = pluginResponse.scanResults;
} catch (error) {
if (
ecosystem === 'docker' &&
error.statusCode === 401 &&
error.message === 'authentication required'
) {
throw new DockerImageNotFoundError(path);
}

throw error;
} finally {
spinner.clearAll();
}
}
const [monitorResults, errors] = await monitorDependencies(
scanResultsByPath,
Expand Down
12 changes: 12 additions & 0 deletions src/lib/errors/docker-image-not-found-error.ts
@@ -0,0 +1,12 @@
import { CustomError } from './custom-error';

export class DockerImageNotFoundError extends CustomError {
private static ERROR_CODE = 404;

constructor(image: string) {
const message = `Failed to scan image "${image}". Please make sure the image and/or repository exist.`;
super(message);
this.code = DockerImageNotFoundError.ERROR_CODE;
this.userMessage = message;
}
}
1 change: 1 addition & 0 deletions src/lib/errors/index.ts
Expand Up @@ -19,6 +19,7 @@ export { OptionMissingErrorError } from './option-missing-error';
export { ExcludeFlagBadInputError } from './exclude-flag-bad-input';
export { UnsupportedOptionCombinationError } from './unsupported-option-combination-error';
export { FeatureNotSupportedByPackageManagerError } from './feature-not-supported-by-package-manager-error';
export { DockerImageNotFoundError } from './docker-image-not-found-error';
export {
NotSupportedIacFileError,
NotSupportedIacFileErrorMsg,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/snyk-test/run-test.ts
Expand Up @@ -27,6 +27,7 @@ import {
FailedToGetVulnsFromUnavailableResource,
FailedToRunTestError,
UnsupportedFeatureFlagError,
DockerImageNotFoundError,
} from '../errors';
import * as snyk from '../';
import { isCI } from '../is-ci';
Expand Down Expand Up @@ -318,6 +319,13 @@ export async function runTest(
if (hasFailedToGetVulnerabilities) {
throw FailedToGetVulnsFromUnavailableResource(root, error.code);
}
if (
getEcosystem(options) === 'docker' &&
error.statusCode === 401 &&
error.message === 'authentication required'
) {
throw new DockerImageNotFoundError(root);
}

throw new FailedToRunTestError(
error.userMessage ||
Expand Down
17 changes: 17 additions & 0 deletions test/acceptance/cli-monitor/cli-monitor.acceptance.test.ts
Expand Up @@ -1873,6 +1873,23 @@ if (!isWindows) {
);
});

test('`monitor doesnotexist --docker`', async (t) => {
try {
await cli.monitor('doesnotexist', {
docker: true,
org: 'explicit-org',
});
t.fail('should have failed');
} catch (err) {
t.match(
err.message,
'Failed to scan image "doesnotexist". Please make sure the image and/or repository exist.',
'show err message',
);
t.pass('throws err');
}
});

test('monitor --json multiple folders', async (t) => {
chdirWorkspaces('fail-on');

Expand Down
16 changes: 16 additions & 0 deletions test/acceptance/cli-test/cli-test.docker.spec.ts
Expand Up @@ -671,6 +671,22 @@ export const DockerTests: AcceptanceTests = {
);
t.end();
},

'`test --docker doesnotexist`': (params) => async (t) => {
try {
await params.cli.test('doesnotexist', {
docker: true,
org: 'explicit-org',
});
t.fail('should have thrown');
} catch (err) {
const msg = err.message;
t.match(
msg,
'Failed to scan image "doesnotexist". Please make sure the image and/or repository exist.',
);
}
},
},
};

Expand Down

0 comments on commit 90b61dd

Please sign in to comment.