Skip to content

Commit

Permalink
Merge pull request #22 from kbariotis/add-error-stacking
Browse files Browse the repository at this point in the history
Add error stacking
  • Loading branch information
kbariotis authored Nov 22, 2020
2 parents 351fc86 + 9e1d89c commit 0abba8b
Show file tree
Hide file tree
Showing 24 changed files with 146 additions and 65 deletions.
55 changes: 34 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ app.listen(3000);
```javascript
const Error = require("throw.js/CustomError");

Error(message, statusCode, errorCode);
throw new Error(message, statusCode?, errorCode?, originalError?);
```
Parameters:
Expand All @@ -53,6 +53,19 @@ Parameters:
- **statusCode**[optional]: The HTTP Status number to return
- **errorCode**[optional]: An internal unique code identifier of this error
## Stacking Errors
```javascript
const Error = require("throw.js/CustomError");

try {
// throws an error
} catch (e) {
// Pass the error as a parameter to keep its original stack trace
throw new Error(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).
Expand All @@ -61,121 +74,121 @@ But you can override the `message` and the `errorCode` to fit your for personal
### 400 Bad Request
```javascript
BadRequest(message, errorCode);
BadRequest(message?, errorCode?, originalError?);
```
### 401 Unauthorized
```javascript
Unauthorized(message, errorCode);
Unauthorized(message?, errorCode?, originalError?);
```
### 402 Payment Required
```javascript
PaymentRequired(message, errorCode);
PaymentRequired(message?, errorCode?, originalError?);
```
### 403 Forbidden
```javascript
Forbidden(message, errorCode);
Forbidden(message?, errorCode?, originalError?);
```
### 404 Not Found
```javascript
NotFound(message, errorCode);
NotFound(message?, errorCode?, originalError?);
```
### 405 Method Not Allowed
```javascript
MethodNotAllowed(message, errorCode);
MethodNotAllowed(message?, errorCode?, originalError?);
```
### 406 Not Acceptable
```javascript
NotAcceptable(message, errorCode);
NotAcceptable(message?, errorCode?, originalError?);
```
### 407 Proxy Authentication Required
```javascript
ProxyAuthenticationRequired(message, errorCode);
ProxyAuthenticationRequired(message?, errorCode?, originalError?);
```
### 408 Request Timeout
```javascript
RequestTimeout(message, errorCode);
RequestTimeout(message?, errorCode?, originalError?);
```
### 409 Conflict
```javascript
Conflict(message, errorCode);
Conflict(message?, errorCode?, originalError?);
```
### 410 Gone
```javascript
Gone(message, errorCode);
Gone(message?, errorCode?, originalError?);
```
### 422 Unprocessable Entity
```javascript
UnprocessableEntity(message, errorCode);
UnprocessableEntity(message?, errorCode?, originalError?);
```
### 424 Failed Dependency
```javascript
FailedDependency(message, errorCode);
FailedDependency(message?, errorCode?, originalError?);
```
### 500 Internal Server Error
```javascript
InternalServerError(message, errorCode);
InternalServerError(message?, errorCode?, originalError?);
```
### 501 Not Implemented
```javascript
NotImplemented(message, errorCode);
NotImplemented(message?, errorCode?, originalError?);
```
### 502 Bad Gateway
```javascript
BadGateway(message, errorCode);
BadGateway(message?, errorCode?, originalError?);
```
### 503 Service Unavailable
```javascript
ServiceUnavailable(message, errorCode);
ServiceUnavailable(message?, errorCode?, originalError?);
```
### 504 Gateway Timeout
```javascript
GatewayTimeout(message, errorCode);
GatewayTimeout(message?, errorCode?, originalError?);
```
### 505 HTTP Version Not Supported
```javascript
HttpVersionNotSupported(message, errorCode);
HttpVersionNotSupported(message?, errorCode?, originalError?);
```
### 511 Network Authentication Required
```javascript
NetworkAuthenticationRequired(message, errorCode);
NetworkAuthenticationRequired(message?, errorCode?, originalError?);
```
## TODO
Expand Down
7 changes: 5 additions & 2 deletions lib/CustomError.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
export default class ExtendableError extends Error {
export default class CustomError extends Error {
errorCode: number;
statusCode: number;
originalError?: Error;

constructor(
message: string,
statusCode: number = 400,
errorCode: number = 400
errorCode: number = 400,
originalError?: Error
) {
super(message);

this.name = this.constructor.name;
this.message = message;
this.statusCode = statusCode;
this.errorCode = errorCode;
this.originalError = originalError;

if (typeof Error.captureStackTrace === "function") {
Error.captureStackTrace(this, this.constructor);
Expand Down
8 changes: 6 additions & 2 deletions lib/errors/BadGateway.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class BadGateway extends ExtendableError {
constructor(message: string = "Bad Gateway", errorCode: number = 502) {
super(message, 502, errorCode);
constructor(
message: string = "Bad Gateway",
errorCode: number = 502,
originalError?: Error
) {
super(message, 502, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/BadRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class BadRequest extends ExtendableError {
constructor(message: string = "Bad Request", errorCode: number = 400) {
super(message, 400, errorCode);
constructor(
message: string = "Bad Request",
errorCode: number = 400,
originalError?: Error
) {
super(message, 400, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/Conflict.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class Conflict extends ExtendableError {
constructor(message: string = "Conflict", errorCode: number = 409) {
super(message, 409, errorCode);
constructor(
message: string = "Conflict",
errorCode: number = 409,
originalError?: Error
) {
super(message, 409, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/FailedDependency.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class FailedDependency extends ExtendableError {
constructor(message: string = "Failed Dependency", errorCode: number = 424) {
super(message, 424, errorCode);
constructor(
message: string = "Failed Dependency",
errorCode: number = 424,
originalError?: Error
) {
super(message, 424, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/Forbidden.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class Forbidden extends ExtendableError {
constructor(message: string = "Forbiden", errorCode: number = 403) {
super(message, 403, errorCode);
constructor(
message: string = "Forbiden",
errorCode: number = 403,
originalError?: Error
) {
super(message, 403, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/GatewayTimeout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class GatewayTimeout extends ExtendableError {
constructor(message: string = "Gateway Timeout", errorCode: number = 504) {
super(message, 504, errorCode);
constructor(
message: string = "Gateway Timeout",
errorCode: number = 504,
originalError?: Error
) {
super(message, 504, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/Gone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class Gone extends ExtendableError {
constructor(message: string = "Gone", errorCode: number = 410) {
super(message, 410, errorCode);
constructor(
message: string = "Gone",
errorCode: number = 410,
originalError?: Error
) {
super(message, 410, errorCode, originalError);
}
}
5 changes: 3 additions & 2 deletions lib/errors/HttpVersionNotSupported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import ExtendableError from "../CustomError";
export default class HttpVersionNotSupported extends ExtendableError {
constructor(
message: string = "HTTP Version Not Supported",
errorCode: number = 505
errorCode: number = 505,
originalError?: Error
) {
super(message, 505, errorCode);
super(message, 505, errorCode, originalError);
}
}
5 changes: 3 additions & 2 deletions lib/errors/InternalServerError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import ExtendableError from "../CustomError";
export default class InternalServerError extends ExtendableError {
constructor(
message: string = "Internal Server Error",
errorCode: number = 500
errorCode: number = 500,
originalError?: Error
) {
super(message, 500, errorCode);
super(message, 500, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/MethodNotAllowed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class MethodNotAllowed extends ExtendableError {
constructor(message: string = "Method Not Allowed", errorCode: number = 405) {
super(message, 405, errorCode);
constructor(
message: string = "Method Not Allowed",
errorCode: number = 405,
originalError?: Error
) {
super(message, 405, errorCode, originalError);
}
}
5 changes: 3 additions & 2 deletions lib/errors/NetworkAuthenticationRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import ExtendableError from "../CustomError";
export default class NetworkAuthenticationRequired extends ExtendableError {
constructor(
message: string = "Network Authentication Required",
errorCode: number = 511
errorCode: number = 511,
originalError?: Error
) {
super(message, 511, errorCode);
super(message, 511, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/NotAcceptable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class NotAcceptable extends ExtendableError {
constructor(message: string = "Not Acceptable", errorCode: number = 406) {
super(message, 406, errorCode);
constructor(
message: string = "Not Acceptable",
errorCode: number = 406,
originalError?: Error
) {
super(message, 406, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/NotFound.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class NotFound extends ExtendableError {
constructor(message: string = "Not Found", errorCode: number = 404) {
super(message, 404, errorCode);
constructor(
message: string = "Not Found",
errorCode: number = 404,
originalError?: Error
) {
super(message, 404, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/NotImplemented.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class NotImplemented extends ExtendableError {
constructor(message: string = "Not Implemented", errorCode: number = 501) {
super(message, 501, errorCode);
constructor(
message: string = "Not Implemented",
errorCode: number = 501,
originalError?: Error
) {
super(message, 501, errorCode, originalError);
}
}
8 changes: 6 additions & 2 deletions lib/errors/PaymentRequired.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ExtendableError from "../CustomError";

export default class PaymentRequired extends ExtendableError {
constructor(message: string = "Payment Required", errorCode: number = 402) {
super(message, 402, errorCode);
constructor(
message: string = "Payment Required",
errorCode: number = 402,
originalError?: Error
) {
super(message, 402, errorCode, originalError);
}
}
Loading

0 comments on commit 0abba8b

Please sign in to comment.