Skip to content

Commit

Permalink
fix: do not show invalidSeverityThreshold error for HTTP 400 responses (
Browse files Browse the repository at this point in the history
#4920)

* fix: do not show invalidSeverityThreshold error for HTTP 400 responses

fixes SUP-2006

* test: add tests for legacy-errors
  • Loading branch information
gergo-papp committed Nov 7, 2023
1 parent 351c7bd commit b202896
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/lib/errors/legacy-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const codes = {
411: errors.endpoint, // try to post to a weird endpoint
403: errors.endpoint,
401: errors.auth,
400: errors.invalidSeverityThreshold,
Unauthorized: errors.auth,
MISSING_NODE_MODULES: errors.nodeModules,
OLD_DOTFILE_FORMAT: errors.oldsnyk,
Expand Down
87 changes: 87 additions & 0 deletions test/jest/unit/lib/errors/legacy-errors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as errors from '../../../../../src/lib/errors/legacy-errors';
import { FormattedCustomError } from '../../../../../src/lib/errors';

describe('errors.message', () => {
it('returns the error message if it is a VULNS error', () => {
const errorMessage = 'This is an error message';
const error = new Error(errorMessage) as any;
(error as any).code = 'VULNS';

const result = errors.message(error);

expect(result).toBe(errorMessage);
});

it('returns the error message if there is a message and it is a FormattedCustomError', () => {
const formattedUserMessage = 'This is a formatted user error message';
const error = new FormattedCustomError(
'This is an error message',
formattedUserMessage,
);

const result = errors.message(error);

expect(result).toBe(error.formattedUserMessage);
});

it('returns the error message based on the error code', () => {
const error = new Error('Unauthorized') as any;
error.code = 'Unauthorized';

const result: string = errors.message(error);

// comparing with toContain because the error message is formatted to be red and bold
expect(result).toContain(
'Unauthorized: please ensure you are logged in using `snyk auth`',
);
});

it('returns the error message based on the error message (404)', () => {
const error = new Error('404');

const result: string = errors.message(error);

// comparing with toContain because the error message is formatted to be red and bold
expect(result).toContain(
'The package could not be found or does not exist',
);
});

it('returns the error message based on the error message (NOT_FOUND)', () => {
const error = new Error('NOT_FOUND');

const result: string = errors.message(error);

// comparing with toContain because the error message is formatted to be red and bold
expect(result).toContain(
'The package could not be found or does not exist',
);
});

it('returns unknown error message for unknowne error code', () => {
const error = new Error('Bad request') as any;
error.code = 400;

const result = errors.message(error);

expect(result).toBe(
'An unknown error occurred. Please run with `-d` and include full trace when reporting to Snyk',
);
});

it('returns the error message if it is not recognized', () => {
const error = new Error('This is an unknown error');

const result = errors.message(error);

expect(result).toBe<string>(error.message);
});

it('returns the error message if it is a string', () => {
const errorMessage = 'This is an error message';

const result = errors.message(errorMessage);

expect(result).toBe(errorMessage);
});
});

0 comments on commit b202896

Please sign in to comment.