Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up old errors #3633

Merged
merged 29 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1a40991
Chore: convert password mismatch to UnleashError
thomasheartman Apr 26, 2023
816f843
chore: update roleinuseerror
thomasheartman Apr 26, 2023
e582203
chore: update remaining errors
thomasheartman Apr 26, 2023
45d2604
Fix: doesn't double punctuation
thomasheartman Apr 26, 2023
5e433f8
Chore: write tests for all non-standard error types
thomasheartman Apr 27, 2023
47201b9
chore: make authenticationrequired an UnleashError subclass
thomasheartman Apr 27, 2023
010b97d
Wip: update some tests
thomasheartman Apr 28, 2023
60fc243
Chore : Move some types around:
thomasheartman Apr 28, 2023
f8a09bd
chore: cleanup after branch update
thomasheartman May 5, 2023
a54ffce
chore: remove console log
thomasheartman May 5, 2023
9879753
chore: remove a little bit of duplication
thomasheartman May 5, 2023
7fb6f37
chore: remove badrequesterror
thomasheartman May 8, 2023
ea15a7a
chore: extract `get-prop-from-string` function.
thomasheartman May 8, 2023
7ca15ca
chore: simplify owaspvalidationerror format
thomasheartman May 8, 2023
9197dc3
refactor: rely on inheritance to propagate required extra fields
thomasheartman May 8, 2023
48544de
chore: remove unused error string.
thomasheartman May 8, 2023
7753491
chore: typo
thomasheartman May 8, 2023
e477e2b
chore: add extra logging when we get an unexpected error type
thomasheartman May 8, 2023
7ff3e95
chore: add new error types
thomasheartman May 8, 2023
539b800
chore: add other missing error type
thomasheartman May 8, 2023
b3c2e18
chore: make bad data errors put message in top level if no errors
thomasheartman May 8, 2023
8d8dec8
chore: use error.statusCode instead of explicit number.
thomasheartman May 9, 2023
c1fdbfd
Chore: fill out missing words in test name
thomasheartman May 9, 2023
59517e2
chore: remove redundant constructors and super calls
thomasheartman May 10, 2023
e002678
Chore: use lodash.get instead of homegrown getPropFromString
thomasheartman May 11, 2023
b1b7a29
chore: extract methods
thomasheartman May 11, 2023
c21a299
refactor: use expect().toMatchObject when there are multiple props
thomasheartman May 11, 2023
aa21d78
chore: remove redundant tests
thomasheartman May 11, 2023
71cf907
chore: rename api-error to unleash-error
thomasheartman May 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Knex } from 'knex';
import maintenanceMiddleware from './middleware/maintenance-middleware';
import { unless } from './middleware/unless-middleware';
import { catchAllErrorHandler } from './middleware/catch-all-error-handler';
import { UnleashError } from './error/api-error';
import NotFoundError from './error/notfound-error';

export default async function getApp(
config: IUnleashConfig,
Expand Down Expand Up @@ -186,10 +186,9 @@ export default async function getApp(

// handle all API 404s
app.use(`${baseUriPath}/api`, (req, res) => {
const error = new UnleashError({
name: 'NotFoundError',
message: `The path you were looking for (${baseUriPath}/api${req.path}) is not available.`,
});
const error = new NotFoundError(
`The path you were looking for (${baseUriPath}/api${req.path}) is not available.`,
);
res.status(error.statusCode).send(error);
return;
});
Expand Down
95 changes: 88 additions & 7 deletions src/lib/error/api-error.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import owasp from 'owasp-password-strength-test';
import { ErrorObject } from 'ajv';
import AuthenticationRequired from '../types/authentication-required';
import {
ApiErrorSchema,
fromLegacyError,
fromOpenApiValidationError,
fromOpenApiValidationErrors,
UnleashApiErrorNameWithoutExtraData,
UnleashApiErrorTypes,
UnleashError,
} from './api-error';
import BadDataError from './bad-data-error';
import BadDataError, {
fromOpenApiValidationError,
fromOpenApiValidationErrors,
} from './bad-data-error';
import NoAccessError from './no-access-error';
import OwaspValidationError from './owasp-validation-error';
import IncompatibleProjectError from './incompatible-project-error';
import PasswordUndefinedError from './password-undefined';
import ProjectWithoutOwnerError from './project-without-owner-error';
import NotFoundError from './notfound-error';

describe('v5 deprecation: backwards compatibility', () => {
it.each(UnleashApiErrorTypes)(
'Adds details to error type: "%s"',
(name: UnleashApiErrorNameWithoutExtraData) => {
const message = `Error type: ${name}`;
const error = new UnleashError({ name, message }).toJSON();
const error = new NotFoundError(message).toJSON();

expect(error.message).toBe(message);
expect(error.details).toStrictEqual([
Expand All @@ -35,7 +41,6 @@ describe('Standard/legacy error conversion', () => {
const message = `: message!`;
const result = fromLegacyError(new BadDataError(message)).toJSON();

expect(result.message.includes('`details`'));
expect(result.details).toStrictEqual([
{
message,
Expand Down Expand Up @@ -211,7 +216,7 @@ describe('OpenAPI error conversion', () => {
const serializedUnleashError: ApiErrorSchema =
fromOpenApiValidationErrors({ newprop: 7 }, errors).toJSON();

expect(serializedUnleashError.name).toBe('ValidationError');
expect(serializedUnleashError.name).toBe('BadDataError');
expect(serializedUnleashError.message).toContain('`details`');
expect(
serializedUnleashError.details!![0].description.includes('newprop'),
Expand Down Expand Up @@ -333,3 +338,79 @@ describe('Error serialization special cases', () => {
expect(json.details!![0].validationErrors).toBe(results.errors);
});
});

describe('Error serialization special cases', () => {
it('AuthenticationRequired: adds `path` and `type`', () => {
const type = 'password';
const path = '/api/login';
const error = new AuthenticationRequired({
type,
path,
message: 'this is a message',
});

const json = error.toJSON();

expect(json.path).toBe(path);
expect(json.type).toBe(type);
});

it('NoAccessError: adds `permission`', () => {
const permission = 'x';
const error = new NoAccessError(permission);
const json = error.toJSON();

expect(json.permission).toBe(permission);
});

it('BadDataError: adds `details` with error details', () => {
const description = 'You did **this** wrong';
const error = new BadDataError(description).toJSON();

expect(error.details![0].message).toBe(description);
expect(error.details![0].description).toBe(description);
});

it('OwaspValidationErrors: adds `validationErrors` to `details`', () => {
const results = owasp.test('123');
const error = new OwaspValidationError(results);
const json = error.toJSON();

expect(json.message).toBe(results.errors[0]);
thomasheartman marked this conversation as resolved.
Show resolved Hide resolved
expect(json.details![0].message).toBe(results.errors[0]);
expect(json.details![0].validationErrors).toBe(results.errors);
});

it('IncompatibleProjectError: adds `validationErrors: []` to the `details` list', () => {
const targetProject = 'x';
const error = new IncompatibleProjectError(targetProject);
const json = error.toJSON();

expect(json.details![0].validationErrors).toStrictEqual([]);
expect(json.details![0].message).toMatch(/\bx\b/);
});

it('PasswordUndefinedError: adds `validationErrors: []` to the `details` list', () => {
const error = new PasswordUndefinedError();
const json = error.toJSON();

expect(json.details![0].validationErrors).toStrictEqual([]);
expect(json.details![0].message).toMatch(json.message);
});

it('ProjectWithoutOwnerError: adds `validationErrors: []` to the `details` list', () => {
const error = new ProjectWithoutOwnerError();
const json = error.toJSON();

expect(json.details![0].validationErrors).toStrictEqual([]);
expect(json.details![0].message).toMatch(json.message);
});
});

describe('Stack traces', () => {
it('captures stack traces regardless of whether `Error.captureStackTrace` is called explicitly or not', () => {
const e = new PasswordUndefinedError();

expect(e.stack).toBeTruthy();
});
});
Loading
Loading