Skip to content

Commit

Permalink
fix: propagate userMessage to 404 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JackuB committed Jul 19, 2021
1 parent 20b05d0 commit 42288e0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/lib/errors/index.ts
Expand Up @@ -28,3 +28,4 @@ export {
IllegalIacFileErrorMsg,
NotSupportedIacAllProjects,
} from './invalid-iac-file';
export { NotFoundError } from './not-found-error';
12 changes: 0 additions & 12 deletions src/lib/errors/non-existing-package-error.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/lib/errors/not-found-error.ts
@@ -0,0 +1,12 @@
import { CustomError } from './custom-error';

export class NotFoundError extends CustomError {
private static ERROR_CODE = 404;
private static ERROR_MESSAGE = "Couldn't find the requested resource";

constructor(userMessage) {
super(userMessage || NotFoundError.ERROR_MESSAGE);
this.code = NotFoundError.ERROR_CODE;
this.userMessage = userMessage || NotFoundError.ERROR_MESSAGE;
}
}
4 changes: 2 additions & 2 deletions src/lib/snyk-test/run-test.ts
Expand Up @@ -29,6 +29,7 @@ import {
InternalServerError,
NoSupportedManifestsFoundError,
UnsupportedFeatureFlagError,
NotFoundError,
} from '../errors';
import * as snyk from '../';
import { isCI } from '../is-ci';
Expand Down Expand Up @@ -70,7 +71,6 @@ import { getAuthHeader } from '../api-token';
import { getEcosystem } from '../ecosystems';
import { Issue } from '../ecosystems/types';
import { assembleEcosystemPayloads } from './assemble-payloads';
import { NonExistingPackageError } from '../errors/non-existing-package-error';
import request = require('../request');
import spinner = require('../spinner');

Expand Down Expand Up @@ -495,7 +495,7 @@ function handleTestHttpErrorResponse(res, body) {
err.innerError = body.stack;
break;
case 404:
err = new NonExistingPackageError();
err = new NotFoundError(userMessage);
err.innerError = body.stack;
break;
case 405:
Expand Down
6 changes: 3 additions & 3 deletions test/acceptance/cli-test/cli-test.generic.spec.ts
Expand Up @@ -60,7 +60,7 @@ export const GenericTests: AcceptanceTests = {
} catch (err) {
t.equal(
err.userMessage,
"Couldn't find the requested package",
'Org missing-org was not found or you may not have the correct permissions',
'got correct err message',
);
t.equal(err.code, 404);
Expand All @@ -82,7 +82,7 @@ export const GenericTests: AcceptanceTests = {
} catch (err) {
t.has(
err.jsonStringifiedResults,
"Couldn't find the requested package",
'Org missing-org was not found or you may not have the correct permissions',
'got correct err message',
);
t.equal(err.code, 404);
Expand All @@ -101,7 +101,7 @@ export const GenericTests: AcceptanceTests = {
} catch (err) {
t.equal(
err.userMessage,
"Couldn't find the requested package",
'Org missing-org was not found or you may not have the correct permissions',
'got correct err message',
);
}
Expand Down
15 changes: 10 additions & 5 deletions test/acceptance/fake-server.ts
Expand Up @@ -91,7 +91,8 @@ export function fakeServer(root, apikey) {
res.status(404);
res.send({
code: 404,
userMessage: 'cli error message',
userMessage:
'Org missing-org was not found or you may not have the correct permissions',
});
return next();
}
Expand All @@ -115,7 +116,8 @@ export function fakeServer(root, apikey) {
res.status(404);
res.send({
code: 404,
userMessage: 'cli error message',
userMessage:
'Org missing-org was not found or you may not have the correct permissions',
});
return next();
}
Expand Down Expand Up @@ -185,7 +187,8 @@ export function fakeServer(root, apikey) {
res.status(404);
res.send({
code: 404,
userMessage: 'cli error message',
userMessage:
'Org missing-org was not found or you may not have the correct permissions',
});
return next();
}
Expand Down Expand Up @@ -234,7 +237,8 @@ export function fakeServer(root, apikey) {
res.status(404);
res.send({
code: 404,
userMessage: 'cli error message',
userMessage:
'Org missing-org was not found or you may not have the correct permissions',
});
return next();
}
Expand All @@ -258,7 +262,8 @@ export function fakeServer(root, apikey) {
res.status(404);
res.send({
code: 404,
userMessage: 'cli error message',
userMessage:
'Org missing-org was not found or you may not have the correct permissions',
});
return next();
}
Expand Down
24 changes: 24 additions & 0 deletions test/smoke/spec/snyk_test_spec.sh
Expand Up @@ -49,6 +49,30 @@ Describe "Snyk test command"
The output should include "https://snyk.io/vuln/npm:minimatch:20160620"
The stderr should equal ""
End

It "tests a library on a specific version when passed a library@version"
Skip if "execute only in regression test" check_if_regression_test
When run snyk test lodash@4.17.15
The status should be failure # issues found
The output should include "Testing lodash@4.17.15"
The stderr should equal ""
End

It "fails with a correct user message on a non-existent library"
Skip if "execute only in regression test" check_if_regression_test
When run snyk test nonexistentpackage123456789
The status should be failure
The output should include "Couldn't find the requested package or version"
The stderr should equal ""
End

It "fails with a correct user message on a non-existent library"
Skip if "execute only in regression test" check_if_regression_test
When run snyk test lodash --org=nope
The status should be failure
The output should include "Org nope was not found or you may not have the correct permissions"
The stderr should equal ""
End
End

Describe "npm test with JSON output"
Expand Down

0 comments on commit 42288e0

Please sign in to comment.