Skip to content

Commit

Permalink
Add GenericError for status related errors
Browse files Browse the repository at this point in the history
  • Loading branch information
knor-el-snor committed Feb 12, 2020
1 parent 58ff00b commit 97cfe08
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,19 @@ const optionalArgs = {
throw new ApiError(400, error, optionalArgs);
```

### GenericError

Extends from ApiError with a preset of status code 0 and GENERIC_ERROR as error.
This should be used when it's internal without needing an actual status code.

```javascript
throw new GenericError(); // or
throw new GenericError(error, optionalArgs);
```

### BadRequestError

extends from ApiError with a preset of status code 400 and BAD_REQUEST as error.
Extends from ApiError with a preset of status code 400 and BAD_REQUEST as error.

```javascript
throw new BadRequestError(); // or
Expand All @@ -55,7 +65,7 @@ throw new BadRequestError(error, optionalArgs);

### NotFoundError

extends from ApiError with a preset of status code 404 and RESOURCE_NOT_FOUND as error.
Extends from ApiError with a preset of status code 404 and RESOURCE_NOT_FOUND as error.

```javascript
throw new NotFoundError(); // or
Expand All @@ -64,7 +74,7 @@ throw new NotFoundError(error, optionalArgs);

### ForbiddenError

extends from ApiError with a preset of status code 403 and FORBIDDEN as error.
Extends from ApiError with a preset of status code 403 and FORBIDDEN as error.

```javascript
throw new ForbiddenError(); // or
Expand All @@ -73,7 +83,7 @@ throw new ForbiddenError(error, optionalArgs);

### InternalServerError

extends from ApiError with a preset of status code 500 and INTERNAL_ERROR as error.
Extends from ApiError with a preset of status code 500 and INTERNAL_ERROR as error.

```javascript
throw new InternalServerError(); // or
Expand All @@ -82,7 +92,7 @@ throw new InternalServerError(error, optionalArgs);

### UnauthorizedError

extends from ApiError with a preset of status code 401 and UNAUTHORIZED as error.
Extends from ApiError with a preset of status code 401 and UNAUTHORIZED as error.

```javascript
throw new UnauthorizedError(); // or
Expand All @@ -91,7 +101,7 @@ throw new UnauthorizedError(error, optionalArgs);

### ValidationError

extends from ApiError with a preset of status code 400 and INVALID_INPUT as error.
Extends from ApiError with a preset of status code 400 and INVALID_INPUT as error.

```javascript
throw new ValidationError(); // or
Expand All @@ -100,7 +110,7 @@ throw new ValidationError(error, optionalArgs);

### AuthenticationError

extends from ApiError with a preset of status code 400 and AUTHENTICATION_FAILED as error.
Extends from ApiError with a preset of status code 400 and AUTHENTICATION_FAILED as error.

```javascript
throw new AuthenticationError(); // or
Expand Down
1 change: 1 addition & 0 deletions src/config/errors.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const errors = {
GENERIC_ERROR: { code: 'GENERIC_ERROR', i18n: 'internal_error', message: 'An unkown error occurred' },
INTERNAL_ERROR: { code: 'INTERNAL_ERROR', i18n: 'internal_error', message: 'An unkown error occurred' },
INVALID_INPUT: { code: 'INVALID_INPUT', i18n: 'invalid_input', message: 'Invalid input provided' },
AUTHENTICATION_FAILED: { code: 'AUTHENTICATION_FAILED', i18n: 'authentication_failed', message: 'Authentication failed' },
Expand Down
7 changes: 7 additions & 0 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export class ApiError extends Error {
}
}

export class GenericError extends ApiError {
constructor(error?: ErrorType, args?: ErrorArgs) {
super(0, error == null ? errors.GENERIC_ERROR : error, args);
this.name = 'GenericError';
}
}

export class BadRequestError extends ApiError {
constructor(error?: ErrorType, args?: ErrorArgs) {
super(httpStatus.BAD_REQUEST, error == null ? errors.BAD_REQUEST : error, args);
Expand Down
37 changes: 37 additions & 0 deletions tests/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
UnauthorizedError,
ValidationError,
AuthenticationError,
GenericError,
} from '../src';

describe('errors', () => {
Expand Down Expand Up @@ -50,6 +51,42 @@ describe('errors', () => {
});
});

describe('GenericError', () => {
it('Should throw a GenericError with default arguments', () => {
try {
throw new GenericError();
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(GenericError);
expect(error.name).toEqual('GenericError');
expect(error.id).toEqual(expect.any(String));
expect(error.toString()).toEqual(`GenericError: ${errors.GENERIC_ERROR.message}`);
expect(error.stack).not.toBeNull();
expect(error.detail).toBeUndefined();
}
});

it('Should throw an InternalServerError with custom error', () => {
const args = {
code: 'CUSTOM_CODE',
message: 'Error with a custom message',
};

try {
throw new GenericError(args);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(GenericError);
expect(error.name).toEqual('GenericError');
expect(error.code).toEqual(args.code);
expect(error.id).toEqual(expect.any(String));
expect(error.toString()).toEqual('GenericError: Error with a custom message');
expect(error.stack).not.toBeNull();
expect(error.detail).toBeUndefined();
}
});
});

describe('BadRequestError', () => {
it('Should throw an BadRequestError with default arguments', () => {
try {
Expand Down

0 comments on commit 97cfe08

Please sign in to comment.