Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
298 changes: 150 additions & 148 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,148 +1,150 @@
# http-errors

This module provides a set of errors based on [standard-http-error](https://www.npmjs.com/package/standard-http-error), reducing the boilerplate of adding error classes for the most common HTTP errors.

## Status

[![npm version][npm-image]][npm-url]
[![build status][tests-image]][tests-url]

## Setup

Install **@uphold/http-errors** with npm:

```sh
npm install @uphold/http-errors
```

## Errors

The `HttpError` serves as base error since all other errors extend it, use it to handle errors provided from this library.

```js
const { BadRequestError, HttpError } = require('@uphold/http-errors');

try {
throw new BadRequestError();
} catch (e) {
console.log(e instanceof HttpError);
// true
}
```

Below is the list of all available errors:

| Name | Code | Default message |
| :---------------------- | :--- | :-------------------- |
| AssertionFailedError | 500 | Internal Server Error |
| BadRequestError | 400 | Bad Request |
| ConflictError | 409 | Conflict |
| ForbiddenError | 403 | Forbidden |
| GoneError | 410 | Gone |
| NotFoundError | 404 | Not Found |
| NotImplementedError | 501 | Not Implemented |
| ServiceUnavailableError | 503 | Service Unavailable |
| TooManyRequestsError | 429 | Too Many Requests |
| UnauthorizedError | 401 | Unauthorized |
| ValidationFailedError | 400 | Validation Failed |

## Usage

Import and throw an HTTP error:

```js
const { ForbiddenError, UnauthorizedError } = require('@uphold/http-errors');

function authorize(user, permission) {
if (!user.role) {
throw new UnauthorizedError('User needs to authenticate');
}

if (permission === 'admin' && user.role !== 'admin') {
throw new ForbiddenError('Only admins allowed', { role: 'admin' });
}

return true;
}
```

All errors accept a message and a set of properties as arguments, or both:

```js
const { BadRequestError } = require('@uphold/http-errors');

try {
throw new BadRequestError();
} catch (e) {
console.log(e);
// { BadRequestError: Bad Request
// at ...
// message: 'Bad Request', name: 'BadRequestError', code: 400 }
}

try {
throw new BadRequestError('foo');
} catch (e) {
// { BadRequestError: Bad Request
// at ...
// message: 'foo', name: 'BadRequestError', code: 400 }
}

try {
throw new BadRequestError({ foo: 'bar' });
} catch (e) {
console.log(e);
// { BadRequestError: Bad Request
// at ...
// message: 'Bad Request', foo: 'bar', name: 'BadRequestError', code: 400 }
}

try {
throw new BadRequestError('foo', { bar: 'biz' });
} catch (e) {
console.log(e);
// { BadRequestError: Bad Request
// at ...
// message: 'foo', bar: 'biz', name: 'BadRequestError', code: 400 }
}
```

## Test suite

Use the `test` script to run the test suite:

```sh
npm test
```

To run the tests on watch mode, use the `test:watch` script:

```sh
npm run test:watch
```

To test and check coverage use the `test:coverage` script:

```sh
npm run test:coverage
```

A full coverage report will be generated on `/coverage` folder.

## Contributing

Please create a PR with a description of the changes, its motivation and impacted areas, making sure the build passes.

## Release process

The release of a version is automated via the [release](https://github.com/uphold/http-errors/.github/workflows/release.yml) GitHub workflow.
Run it by clicking the "Run workflow" button.

## License

MIT

[npm-image]: https://img.shields.io/npm/v/@uphold/http-errors.svg
[npm-url]: https://www.npmjs.com/package/@uphold/http-errors
[tests-image]: https://github.com/uphold/http-errors/actions/workflows/tests.yaml/badge.svg?branch=master
[tests-url]: https://github.com/uphold/http-errors/actions/workflows/tests.yaml
# http-errors

This module provides a set of errors based on [standard-http-error](https://www.npmjs.com/package/standard-http-error), reducing the boilerplate of adding error classes for the most common HTTP errors.

## Status

[![npm version][npm-image]][npm-url]
[![build status][tests-image]][tests-url]

## Setup

Install **@uphold/http-errors** with npm:

```sh
npm install @uphold/http-errors
```

## Errors

The `HttpError` serves as base error since all other errors extend it, use it to handle errors provided from this library.

```js
const { BadRequestError, HttpError } = require('@uphold/http-errors');

try {
throw new BadRequestError();
} catch (e) {
console.log(e instanceof HttpError);
// true
}
```

Below is the list of all available errors:

| Name | Code | Default message |
| :---------------------- | :--- | :-------------------- |
| AssertionFailedError | 500 | Assertion Failed |
| BadRequestError | 400 | Bad Request |
| ConflictError | 409 | Conflict |
| ForbiddenError | 403 | Forbidden |
| GoneError | 410 | Gone |
| NotFoundError | 404 | Not Found |
| NotImplementedError | 501 | Not Implemented |
| ServiceUnavailableError | 503 | Service Unavailable |
| TooManyRequestsError | 429 | Too Many Requests |
| UnauthorizedError | 401 | Unauthorized |
| ValidationFailedError | 400 | Validation Failed |

## Usage

Import and throw an HTTP error:

```js
const { ForbiddenError, UnauthorizedError } = require('@uphold/http-errors');

function authorize(user, permission) {
if (!user.role) {
throw new UnauthorizedError('User needs to authenticate');
}

if (permission === 'admin' && user.role !== 'admin') {
throw new ForbiddenError('Only admins allowed', { role: 'admin' });
}

return true;
}
```

All errors accept a message and a set of properties as arguments, or both:

> `AssertionFailedError` and `ValidationFailedError` additionally accept a **single** argument, which is assigned to `error.errors`. This is the form used to carry a `validator.js` violation tree. When two or more arguments are given they follow the `(message, properties)` contract below.

```js
const { BadRequestError } = require('@uphold/http-errors');

try {
throw new BadRequestError();
} catch (e) {
console.log(e);
// { BadRequestError: Bad Request
// at ...
// message: 'Bad Request', name: 'BadRequestError', code: 400 }
}

try {
throw new BadRequestError('foo');
} catch (e) {
// { BadRequestError: Bad Request
// at ...
// message: 'foo', name: 'BadRequestError', code: 400 }
}

try {
throw new BadRequestError({ foo: 'bar' });
} catch (e) {
console.log(e);
// { BadRequestError: Bad Request
// at ...
// message: 'Bad Request', foo: 'bar', name: 'BadRequestError', code: 400 }
}

try {
throw new BadRequestError('foo', { bar: 'biz' });
} catch (e) {
console.log(e);
// { BadRequestError: Bad Request
// at ...
// message: 'foo', bar: 'biz', name: 'BadRequestError', code: 400 }
}
```

## Test suite

Use the `test` script to run the test suite:

```sh
npm test
```

To run the tests on watch mode, use the `test:watch` script:

```sh
npm run test:watch
```

To test and check coverage use the `test:coverage` script:

```sh
npm run test:coverage
```

A full coverage report will be generated on `/coverage` folder.

## Contributing

Please create a PR with a description of the changes, its motivation and impacted areas, making sure the build passes.

## Release process

The release of a version is automated via the [release](https://github.com/uphold/http-errors/.github/workflows/release.yaml) GitHub workflow.
Run it by clicking the "Run workflow" button.

## License

MIT

[npm-image]: https://img.shields.io/npm/v/@uphold/http-errors.svg
[npm-url]: https://www.npmjs.com/package/@uphold/http-errors
[tests-image]: https://github.com/uphold/http-errors/actions/workflows/tests.yaml/badge.svg?branch=master
[tests-url]: https://github.com/uphold/http-errors/actions/workflows/tests.yaml
54 changes: 33 additions & 21 deletions src/errors/assertion-failed-error.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
'use strict';

/**
* Module dependencies.
*/

const HttpError = require('./http-error');

/**
* Export `AssertionFailedError`.
*/

module.exports = class AssertionFailedError extends HttpError {
/**
* Constructor.
*/

constructor(errors) {
super(500, 'Assertion Failed', { errors });
}
};
'use strict';

/**
* Module dependencies.
*/

const HttpError = require('./http-error');

/**
* Export `AssertionFailedError`.
*/

module.exports = class AssertionFailedError extends HttpError {
/**
* Constructor.
*
* A single argument is the `errors` payload (typically a validator.js violation tree) and is
* kept for backwards compatibility. Two or more arguments follow the documented
* `(message, properties)` contract shared by every other error in this package: previously the
* second argument was silently discarded, so `new AssertionFailedError('my message', { foo:
* 'bar' })` produced `message === 'Assertion Failed'` and lost `foo`, contradicting the README.
*/

constructor(...args) {
if (args.length >= 2) {
super(500, ...args);

return;
}

super(500, 'Assertion Failed', { errors: args[0] });
}
};
Loading