Skip to content

Commit cd06d66

Browse files
abelsoaresnunorafaelrocha
authored andcommitted
Merge pull request #1 from seegno/feature/initial-implementation
Add initial implementation
2 parents 9c73689 + 5821df5 commit cd06d66

11 files changed

Lines changed: 6051 additions & 0 deletions

File tree

.eslintrc.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
extends: seegno
2+
3+
env:
4+
jasmine: true
5+
jest: true
6+
7+
plugins:
8+
- jest
9+
10+
rules:
11+
jest/no-disabled-tests: 2
12+
jest/no-focused-tests: 2
13+
jest/no-identical-title: 2

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/coverage
2+
/node_modules

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.eslintrc.yml
2+
.gitignore
3+
.travis.yml
4+
/test

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
3+
node_js:
4+
- "4.2.0"
5+
- "6.0.0"
6+
- "8.0.0"
7+
8+
after_success:
9+
- yarn coveralls

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,105 @@
11
# Easy Http Errors
22

3+
[![npm](https://img.shields.io/npm/v/easy-http-errors.svg?style=flat-square)](https://npmjs.org/package/easy-http-errors)
4+
![node](https://img.shields.io/node/v/easy-http-errors.svg?style=flat-square)
5+
[![Build Status](https://img.shields.io/travis/seegno/easy-http-errors/master.svg?style=flat-square)](https://travis-ci.org/seegno/easy-http-errors)
6+
[![Coverage Status](https://img.shields.io/coveralls/seegno/easy-http-errors/master.svg?style=flat-square)](https://coveralls.io/github/seegno/easy-http-errors?branch=master)
7+
8+
A preset of HTTP errors that can be easily used to throw errors in your applications.
9+
10+
## Installation
11+
12+
### NPM
13+
14+
```sh
15+
npm i easy-http-errors --save
16+
```
17+
18+
### Yarn
19+
20+
```sh
21+
yarn add easy-http-errors
22+
```
23+
24+
## Usage
25+
26+
```js
27+
// ES6 import.
28+
import { BadRequestError } from 'easy-http-errors';
29+
30+
// Throw the default bad request.
31+
throw new BadRequestError();
32+
33+
// Throw a bad request with a custom message and properties.
34+
throw new BadRequestError('Ups, this is a bad request', { foo: 'bar' });
35+
```
36+
37+
## List of errors
38+
39+
| Status Code | Name |
40+
| :---------: | -----------------------------------|
41+
| 400 | BadRequestError |
42+
| 401 | UnauthorizedError |
43+
| 402 | PaymentRequiredError |
44+
| 403 | ForbiddenError |
45+
| 404 | NotFoundError |
46+
| 405 | MethodNotAllowedError |
47+
| 406 | NotAcceptableError |
48+
| 407 | ProxyAuthenticationRequiredError |
49+
| 408 | RequestTimeoutError |
50+
| 409 | ConflictError |
51+
| 410 | GoneError |
52+
| 411 | LengthRequiredError |
53+
| 412 | PreconditionFailedError |
54+
| 413 | PayloadTooLargeError |
55+
| 414 | URITooLongError |
56+
| 415 | UnsupportedMediaTypeError |
57+
| 416 | RangeNotSatisfiableError |
58+
| 417 | ExpectationFailedError |
59+
| 418 | ImATeapotError |
60+
| 421 | MisdirectedRequestError |
61+
| 422 | UnprocessableEntityError |
62+
| 423 | LockedError |
63+
| 424 | FailedDependencyError |
64+
| 425 | UnorderedCollectionError |
65+
| 426 | UpgradeRequiredError |
66+
| 428 | PreconditionRequiredError |
67+
| 429 | TooManyRequestsError |
68+
| 431 | RequestHeaderFieldsTooLargeError |
69+
| 451 | UnavailableForLegalReasonsError |
70+
| 500 | InternalServerError |
71+
| 501 | NotImplementedError |
72+
| 502 | BadGatewayError |
73+
| 503 | ServiceUnavailableError |
74+
| 504 | GatewayTimeoutError |
75+
| 505 | HTTPVersionNotSupportedError |
76+
| 506 | VariantAlsoNegotiatesError |
77+
| 507 | InsufficientStorageError |
78+
| 508 | LoopDetectedError |
79+
| 509 | BandwidthLimitExceededError |
80+
| 510 | NotExtendedError |
81+
| 511 | NetworkAuthenticationRequiredError |
82+
83+
## Tests
84+
85+
Run the tests from the root directory:
86+
87+
```sh
88+
npm test
89+
```
90+
91+
## Contributing & Development
92+
93+
### Contributing
94+
95+
Found a bug or want to suggest something? Take a look first on the current and closed [issues](https://github.com/seegno/easy-http-errors/issues). If it is something new, please [submit an issue](https://github.com/seegno/easy-http-errors/issues/new).
96+
97+
### Develop
98+
99+
It will be awesome if you can help us evolve `easy-http-errors`. Want to help?
100+
101+
1. [Fork it](https://github.com/seegno/easy-http-errors).
102+
2. `npm install`.
103+
3. Hack away.
104+
4. Run the tests: `npm test`.
105+
5. Create a [Pull Request](https://github.com/seegno/easy-http-errors/compare).

errors.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
/**
3+
* Export `errors`.
4+
*/
5+
6+
module.exports = [
7+
{ message: 'Bad request', name: 'BadRequestError', status: 400 },
8+
{ message: 'Unauthorized', name: 'UnauthorizedError', status: 401 },
9+
{ message: 'Payment required', name: 'PaymentRequiredError', status: 402 },
10+
{ message: 'Forbidden', name: 'ForbiddenError', status: 403 },
11+
{ message: 'Not Found', name: 'NotFoundError', status: 404 },
12+
{ message: 'Method Not Allowed', name: 'MethodNotAllowedError', status: 405 },
13+
{ message: 'Not Acceptable', name: 'NotAcceptableError', status: 405 },
14+
{ message: 'Proxy Authentication Required', name: 'ProxyAuthenticationRequiredError', status: 407 },
15+
{ message: 'Request Timeout', name: 'RequestTimeoutError', status: 408 },
16+
{ message: 'Conflict', name: 'ConflictError', status: 409 },
17+
{ message: 'Gone', name: 'GoneError', status: 410 },
18+
{ message: 'Length Required', name: 'LengthRequiredError', status: 411 },
19+
{ message: 'Precondition Failed', name: 'PreconditionFailedError', status: 412 },
20+
{ message: 'Payload Too Large', name: 'PayloadTooLargeError', status: 413 },
21+
{ message: 'URI Too Long', name: 'URITooLongError', status: 414 },
22+
{ message: 'Unsupported Media Type', name: 'UnsupportedMediaTypeError', status: 415 },
23+
{ message: 'Range Not Satisfiable', name: 'RangeNotSatisfiableError', status: 416 },
24+
{ message: 'Expectation Failed', name: 'ExpectationFailedError', status: 417 },
25+
{ message: 'I\'m A Tea pot', name: 'ImATeapotError', status: 418 },
26+
{ message: 'Misdirected Request', name: 'MisdirectedRequestError', status: 421 },
27+
{ message: 'Unprocessable Entity', name: 'UnprocessableEntityError', status: 422 },
28+
{ message: 'Locked', name: 'LockedError', status: 423 },
29+
{ message: 'Failed Dependency', name: 'FailedDependencyError', status: 424 },
30+
{ message: 'Unordered Collection', name: 'UnorderedCollectionError', status: 425 },
31+
{ message: 'Upgrade Required', name: 'UpgradeRequiredError', status: 426 },
32+
{ message: 'Precondition Required', name: 'PreconditionRequiredError', status: 428 },
33+
{ message: 'Too Many Requests', name: 'TooManyRequestsError', status: 429 },
34+
{ message: 'Request Header Fields Too Large', name: 'RequestHeaderFieldsTooLargeError', status: 431 },
35+
{ message: 'Unavailable For Legal Reasons', name: 'UnavailableForLegalReasonsError', status: 451 },
36+
{ message: 'Internal Server Error', name: 'InternalServerError', status: 500 },
37+
{ message: 'Not Implemented', name: 'NotImplementedError', status: 501 },
38+
{ message: 'Bad Gateway', name: 'BadGatewayError', status: 502 },
39+
{ message: 'Service Unavailable', name: 'ServiceUnavailableError', status: 503 },
40+
{ message: 'Gateway Timeout', name: 'GatewayTimeoutError', status: 504 },
41+
{ message: 'HTTP Version Not Supported', name: 'HTTPVersionNotSupportedError', status: 505 },
42+
{ message: 'Variant Also Negotiates', name: 'VariantAlsoNegotiatesError', status: 506 },
43+
{ message: 'Insufficient Storage', name: 'InsufficientStorageError', status: 507 },
44+
{ message: 'Loop Detected', name: 'LoopDetectedError', status: 508 },
45+
{ message: 'Bandwidth Limit Exceeded', name: 'BandwidthLimitExceededError', status: 509 },
46+
{ message: 'Not Extended', name: 'NotExtendedError', status: 510 },
47+
{ message: 'Network Authentication Required', name: 'NetworkAuthenticationRequiredError', status: 511 }
48+
];

index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const StandardHttpError = require('standard-http-error');
8+
const errors = require('./errors');
9+
10+
/**
11+
* Error class factory.
12+
*/
13+
14+
function factory(options) {
15+
return class extends StandardHttpError {
16+
17+
/**
18+
* Class name.
19+
*/
20+
21+
static get name() {
22+
return options.name;
23+
}
24+
25+
/**
26+
* Constructor.
27+
*/
28+
29+
constructor(message, properties) {
30+
super(options.status, message || options.message, properties);
31+
}
32+
};
33+
}
34+
35+
/**
36+
* Export `easy-http-errors`.
37+
*/
38+
39+
module.exports = errors.reduce((result, error) => {
40+
result[error.name] = factory(error);
41+
42+
return result;
43+
}, {});

0 commit comments

Comments
 (0)