https://github.com/kbariotis/throw.js
HTTP Error collection to use in your next REST API.
https://github.com/kbariotis/throw.js
errors javascript nodejs
Last synced: 7 months ago
JSON representation
HTTP Error collection to use in your next REST API.
- Host: GitHub
- URL: https://github.com/kbariotis/throw.js
- Owner: kbariotis
- License: mit
- Created: 2015-04-24T18:36:31.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2021-01-11T19:08:19.000Z (almost 5 years ago)
- Last Synced: 2024-04-14T13:10:02.580Z (over 1 year ago)
- Topics: errors, javascript, nodejs
- Language: TypeScript
- Homepage:
- Size: 55.7 KB
- Stars: 274
- Watchers: 5
- Forks: 18
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# throw.js
An HTTP Error collection to use in your next REST API.
## Installation
```
npm install --save throw.js
```
or
```
yarn add throw.js
```
## Example
```javascript
const express = require("express");
const app = express();
const { NotFound } = require("throw.js");
const logger = require("winston");
app.get("/", (req, res, next) => {
next(new NotFound());
});
app.use((err, req, res, next) => {
logger.error(err);
if (req.app.get("env") !== "development" && req.app.get("env") !== "test") {
delete err.stack;
}
res.status(err.statusCode || 500).json(err);
});
app.listen(3000);
```
## Custom Errors
```javascript
const { CustomError } = require("throw.js");
throw new CustomError(message, statusCode?, errorCode?, originalError?);
```
Parameters:
- **message**[optional]: Detailed message of this error
- **statusCode**[optional]: The HTTP Status number to return
- **errorCode**[optional]: An internal unique code identifier of this error
## Stacking Errors
```javascript
const { CustomError } = require("throw.js");
try {
// throws an error
} catch (e) {
// Pass the error as a parameter to keep its original stack trace
throw new CustomError(message, statusCode, errorCode, e);
}
```
## Errors
All of the classes below have all parameters set up by default, based on [RFC7231](http://tools.ietf.org/html/rfc7231).
But you can override the `message` and the `errorCode` to fit your for personal needs.
### 400 Bad Request
```javascript
BadRequest(message?, errorCode?, originalError?);
```
### 401 Unauthorized
```javascript
Unauthorized(message?, errorCode?, originalError?);
```
### 402 Payment Required
```javascript
PaymentRequired(message?, errorCode?, originalError?);
```
### 403 Forbidden
```javascript
Forbidden(message?, errorCode?, originalError?);
```
### 404 Not Found
```javascript
NotFound(message?, errorCode?, originalError?);
```
### 405 Method Not Allowed
```javascript
MethodNotAllowed(message?, errorCode?, originalError?);
```
### 406 Not Acceptable
```javascript
NotAcceptable(message?, errorCode?, originalError?);
```
### 407 Proxy Authentication Required
```javascript
ProxyAuthenticationRequired(message?, errorCode?, originalError?);
```
### 408 Request Timeout
```javascript
RequestTimeout(message?, errorCode?, originalError?);
```
### 409 Conflict
```javascript
Conflict(message?, errorCode?, originalError?);
```
### 410 Gone
```javascript
Gone(message?, errorCode?, originalError?);
```
### 422 Unprocessable Entity
```javascript
UnprocessableEntity(message?, errorCode?, originalError?);
```
### 424 Failed Dependency
```javascript
FailedDependency(message?, errorCode?, originalError?);
```
### 500 Internal Server Error
```javascript
InternalServerError(message?, errorCode?, originalError?);
```
### 501 Not Implemented
```javascript
NotImplemented(message?, errorCode?, originalError?);
```
### 502 Bad Gateway
```javascript
BadGateway(message?, errorCode?, originalError?);
```
### 503 Service Unavailable
```javascript
ServiceUnavailable(message?, errorCode?, originalError?);
```
### 504 Gateway Timeout
```javascript
GatewayTimeout(message?, errorCode?, originalError?);
```
### 505 HTTP Version Not Supported
```javascript
HttpVersionNotSupported(message?, errorCode?, originalError?);
```
### 511 Network Authentication Required
```javascript
NetworkAuthenticationRequired(message?, errorCode?, originalError?);
```
## TODO
- Implement more Error Classes