From 287ae14ea173418b710447b8b06cbe36c5c9d534 Mon Sep 17 00:00:00 2001 From: "romain.lenzotti" Date: Mon, 15 Apr 2019 08:50:04 +0200 Subject: [PATCH] feat: HttpException and his inherited exception accept an origin parameters Closes: #4 --- docs/getting-started.md | 68 ++++++++++++------- readme.md | 66 +++++++++++------- src/clientErrors/BadMapping.ts | 12 +--- src/clientErrors/BadRequest.ts | 5 +- src/clientErrors/Conflict.ts | 5 +- src/clientErrors/ExpectationFailed.ts | 5 +- src/clientErrors/Forbidden.ts | 5 +- src/clientErrors/Gone.ts | 5 +- src/clientErrors/ImATeapot.ts | 5 +- src/clientErrors/LengthRequired.ts | 5 +- src/clientErrors/MethodNotAllowed.ts | 5 +- src/clientErrors/MisdirectedRequest.ts | 5 +- src/clientErrors/NotAcceptable.ts | 5 +- src/clientErrors/NotFound.ts | 5 +- src/clientErrors/PaymentRequired.ts | 5 +- src/clientErrors/PreconditionFailed.ts | 5 +- src/clientErrors/PreconditionRequired.ts | 5 +- .../ProxyAuthentificationRequired.ts | 5 +- src/clientErrors/RequestEntityTooLarge.ts | 5 +- .../RequestHeaderFieldsTooLarge.ts | 5 +- src/clientErrors/RequestTimeout.ts | 5 +- src/clientErrors/RequestURITooLong.ts | 5 +- .../RequestedRangeUnsatisfiable.ts | 5 +- src/clientErrors/TooManyRequests.ts | 5 +- src/clientErrors/Unauthorized.ts | 5 +- .../UnavailabledForLegalReasons.ts | 5 +- src/clientErrors/UnprocessableEntity.ts | 5 +- src/clientErrors/UnsupportedMediaType.ts | 5 +- src/clientErrors/UpgradeRequired.ts | 5 +- src/core/Exception.ts | 37 +++++----- src/redirections/MovedPermanently.ts | 5 +- src/redirections/MovedTemporarily.ts | 5 +- src/redirections/MultipleChoices.ts | 5 +- src/redirections/NotModified.ts | 5 +- src/redirections/PermanentRedirect.ts | 5 +- src/redirections/SeeOther.ts | 5 +- src/redirections/TemporaryRedirect.ts | 5 +- src/redirections/TooManyRedirects.ts | 5 +- src/redirections/UseProxy.ts | 5 +- src/serverErrors/BadGateway.ts | 5 +- src/serverErrors/BandwidthLimitExceeded.ts | 5 +- src/serverErrors/GatewayTimeout.ts | 5 +- src/serverErrors/InternalServerError.ts | 5 +- .../NetworkAuthenticationRequired.ts | 5 +- src/serverErrors/NotExtended.ts | 5 +- src/serverErrors/NotImplemented.ts | 5 +- src/serverErrors/ProxyError.ts | 5 +- src/serverErrors/ServiceUnvailable.ts | 5 +- src/serverErrors/VariantAlsoNegotiates.ts | 5 +- test/Exception.spec.ts | 18 +++-- 50 files changed, 255 insertions(+), 171 deletions(-) diff --git a/docs/getting-started.md b/docs/getting-started.md index e433fba..3406672 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -8,39 +8,57 @@ You can get the latest release using npm: $ npm install --save ts-httpexceptions ``` -> **Important!** Ts.ED requires Node >= 6, Express >= 4 and TypeScript >= 2.0. +> **Important!** Ts.ED requires Node >= 8 and TypeScript >= 2.0. ## Example ```typescript - import {BadRequest, Exception} from 'ts-httpexceptions'; - let express = require('express'); - let app = express(); - - app.get('/my/route', (req, res) => { +import {BadRequest, Exception, NotFound} from 'ts-httpexceptions'; +const express = require('express'); // Koa works also +const app = express(); + +app.get('/my/route/:id', async (req, res, next) => { + if (req.params.id === undefined) { + const error = new BadRequest("ID is required") - throw new BadRequest('Custom Message'); //Emit - // OR - // throw new Exception(510, 'Custom Message'); + // Additionally + error.headers = { + 'x-header': 'value' + } + error.errors = [{'message': "ID is required"}] + error.body = [{'message': "ID is required"}] - }); + next(error); + } + + try { + const user = await getUser(res.params.id) + res.json(user); + } catch(origin) { + next(new NotFound('User not found', origin)) + } +}); + + +//GlobalHandler middleware catch exception and send response to the client +app.use((err, request, response, next) => { + if(err instanceof Exception) { + if (err.errors) { // If errors is provided + response.set({'x-errors': JSON.stringify(err.errors)}) + } - app.get('/my/route/params', (req, res) => { - - if (req.params.id === undefined){ - throw new BadRequest(); - } - - }); + if (err.headers) { + response.set(err.headers) + } + if (err.body) { // If a body is provided + return response.status(err.status).json(err.body) + } - //GlobalHandler middleware catch exception and send response to the client - app.use((err, request, response) => { - - if(err instanceof Exception){ - response.status(err.status).send(err.message); - } - - }); + return response.status(err.status).send(err.message); + } + + next() +}); ``` diff --git a/readme.md b/readme.md index 89b992d..7c0c55f 100644 --- a/readme.md +++ b/readme.md @@ -83,35 +83,53 @@ $ npm install ts-httpexceptions ## API ```typescript - import {BadRequest, Exception} from 'ts-httpexceptions'; - let express = require('express'); - let app = express(); - - app.get('/my/route', (req, res) => { +import {BadRequest, Exception, NotFound} from 'ts-httpexceptions'; +const express = require('express'); +const app = express(); + +app.get('/my/route/:id', async (req, res, next) => { + if (req.params.id === undefined) { + const error = new BadRequest("ID is required") - throw new BadRequest('Custom Message'); //Emit - // OR - // throw new Exception(510, 'Custom Message'); + // Additionally + error.headers = { + 'x-header': 'value' + } + error.errors = [{'message': "ID is required"}] + error.body = [{'message': "ID is required"}] - }); + next(error); + } + + try { + const user = await getUser(res.params.id) + res.json(user); + } catch(origin) { + next(new NotFound('User not found', origin)) + } +}); + + +//GlobalHandler middleware catch exception and send response to the client +app.use((err, request, response, next) => { + if(err instanceof Exception) { + if (err.errors) { // If errors is provided + response.set({'x-errors': JSON.stringify(err.errors)}) + } - app.get('/my/route/params', (req, res) => { - - if (req.params.id === undefined){ - throw new BadRequest(); - } - - }); + if (err.headers) { + response.set(err.headers) + } + if (err.body) { // If a body is provided + return response.status(err.status).json(err.body) + } - //GlobalHandler middleware catch exception and send response to the client - app.use((err, request, response) => { - - if(err instanceof Exception){ - response.status(err.status).send(err.message); - } - - }); + return response.status(err.status).send(err.message); + } + + next() +}); ``` diff --git a/src/clientErrors/BadMapping.ts b/src/clientErrors/BadMapping.ts index e5cc271..8b7b38c 100644 --- a/src/clientErrors/BadMapping.ts +++ b/src/clientErrors/BadMapping.ts @@ -1,16 +1,10 @@ import {Exception} from "../core/Exception"; -/** - * - */ export class BadMapping extends Exception { + static readonly STATUS = 421; name: string = "BAD_MAPPING"; - /** - * - * @param message - */ - constructor(message: string) { - super(421, message); + constructor(message: string, origin?: Error | string | any) { + super(BadMapping.STATUS, message, origin); } } diff --git a/src/clientErrors/BadRequest.ts b/src/clientErrors/BadRequest.ts index 5d93ce6..782083d 100644 --- a/src/clientErrors/BadRequest.ts +++ b/src/clientErrors/BadRequest.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class BadRequest extends Exception { + static readonly STATUS = 400; name: string = "BAD_REQUEST"; - constructor(message: string) { - super(400, message); + constructor(message: string, origin?: Error | string | any) { + super(BadRequest.STATUS, message, origin); } } diff --git a/src/clientErrors/Conflict.ts b/src/clientErrors/Conflict.ts index d17482c..380d19d 100644 --- a/src/clientErrors/Conflict.ts +++ b/src/clientErrors/Conflict.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class Conflict extends Exception { + static readonly STATUS = 409; name: string = "CONFLICT"; - constructor(message: string) { - super(409, message); + constructor(message: string, origin?: Error | string | any) { + super(Conflict.STATUS, message, origin); } } diff --git a/src/clientErrors/ExpectationFailed.ts b/src/clientErrors/ExpectationFailed.ts index dad3744..35e575a 100644 --- a/src/clientErrors/ExpectationFailed.ts +++ b/src/clientErrors/ExpectationFailed.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class ExpectationFailed extends Exception { + static readonly STATUS = 417; name: string = "EXPECTATION_FAILED"; - constructor(message: string) { - super(417, message); + constructor(message: string, origin?: Error | string | any) { + super(ExpectationFailed.STATUS, message, origin); } } diff --git a/src/clientErrors/Forbidden.ts b/src/clientErrors/Forbidden.ts index f11be76..4c04090 100644 --- a/src/clientErrors/Forbidden.ts +++ b/src/clientErrors/Forbidden.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class Forbidden extends Exception { + static readonly STATUS = 403; name: string = "FORBIDDEN"; - constructor(message: string) { - super(403, message); + constructor(message: string, origin?: Error | string | any) { + super(Forbidden.STATUS, message, origin); } } diff --git a/src/clientErrors/Gone.ts b/src/clientErrors/Gone.ts index f7f4358..b404e7e 100644 --- a/src/clientErrors/Gone.ts +++ b/src/clientErrors/Gone.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class Gone extends Exception { + static readonly STATUS = 410; name: string = "GONE"; - constructor(message: string) { - super(410, message); + constructor(message: string, origin?: Error | string | any) { + super(Gone.STATUS, message, origin); } } diff --git a/src/clientErrors/ImATeapot.ts b/src/clientErrors/ImATeapot.ts index 404be02..a7020e3 100644 --- a/src/clientErrors/ImATeapot.ts +++ b/src/clientErrors/ImATeapot.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class ImATeapot extends Exception { + static readonly STATUS = 418; name: string = "IM_A_TEAPOT"; - constructor(message: string) { - super(418, message); + constructor(message: string, origin?: Error | string | any) { + super(ImATeapot.STATUS, message, origin); } } diff --git a/src/clientErrors/LengthRequired.ts b/src/clientErrors/LengthRequired.ts index 93e652b..858ac9c 100644 --- a/src/clientErrors/LengthRequired.ts +++ b/src/clientErrors/LengthRequired.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class LengthRequired extends Exception { + static readonly STATUS = 411; name: string = "LENGTH_REQUIRED"; - constructor(message: string) { - super(411, message); + constructor(message: string, origin?: Error | string | any) { + super(LengthRequired.STATUS, message, origin); } } diff --git a/src/clientErrors/MethodNotAllowed.ts b/src/clientErrors/MethodNotAllowed.ts index 7c51191..065e48a 100644 --- a/src/clientErrors/MethodNotAllowed.ts +++ b/src/clientErrors/MethodNotAllowed.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class MethodNotAllowed extends Exception { + static readonly STATUS = 405; name: string = "METHOD_NOT_ALLOWED"; - constructor(message: string) { - super(405, message); + constructor(message: string, origin?: Error | string | any) { + super(MethodNotAllowed.STATUS, message, origin); } } diff --git a/src/clientErrors/MisdirectedRequest.ts b/src/clientErrors/MisdirectedRequest.ts index 9403a5c..b81abf7 100644 --- a/src/clientErrors/MisdirectedRequest.ts +++ b/src/clientErrors/MisdirectedRequest.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class MisdirectedRequest extends Exception { + static readonly STATUS = 421; name: string = "MISDIRECTED_REQUEST"; - constructor(message: string) { - super(421, message); + constructor(message: string, origin?: Error | string | any) { + super(MisdirectedRequest.STATUS, message, origin); } } diff --git a/src/clientErrors/NotAcceptable.ts b/src/clientErrors/NotAcceptable.ts index 51d03eb..33c3aa2 100644 --- a/src/clientErrors/NotAcceptable.ts +++ b/src/clientErrors/NotAcceptable.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class NotAcceptable extends Exception { + static readonly STATUS = 406; name: string = "NOT_ACCEPTABLE"; - constructor(message: string) { - super(406, "You must accept content-type " + message); + constructor(message: string, origin: Error | string | any = "You must accept content-type " + message) { + super(NotAcceptable.STATUS, origin); } } diff --git a/src/clientErrors/NotFound.ts b/src/clientErrors/NotFound.ts index 2760501..17aa2c6 100644 --- a/src/clientErrors/NotFound.ts +++ b/src/clientErrors/NotFound.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class NotFound extends Exception { + static readonly STATUS = 404; name: string = "NOT_FOUND"; - constructor(message: string) { - super(404, message); + constructor(message: string, origin?: Error | string | any) { + super(NotFound.STATUS, message, origin); } } diff --git a/src/clientErrors/PaymentRequired.ts b/src/clientErrors/PaymentRequired.ts index e888263..7de752c 100644 --- a/src/clientErrors/PaymentRequired.ts +++ b/src/clientErrors/PaymentRequired.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class PaymentRequired extends Exception { + static readonly STATUS = 402; name: string = "PAYMENT_REQUIRED"; - constructor(message: string) { - super(402, message); + constructor(message: string, origin?: Error | string | any) { + super(PaymentRequired.STATUS, message, origin); } } diff --git a/src/clientErrors/PreconditionFailed.ts b/src/clientErrors/PreconditionFailed.ts index 4a9d367..5164b26 100644 --- a/src/clientErrors/PreconditionFailed.ts +++ b/src/clientErrors/PreconditionFailed.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class PreconditionFailed extends Exception { + static readonly STATUS = 412; name: string = "PRECONDITION_FAILED"; - constructor(message: string) { - super(412, message); + constructor(message: string, origin?: Error | string | any) { + super(PreconditionFailed.STATUS, message, origin); } } diff --git a/src/clientErrors/PreconditionRequired.ts b/src/clientErrors/PreconditionRequired.ts index c565113..01e778b 100644 --- a/src/clientErrors/PreconditionRequired.ts +++ b/src/clientErrors/PreconditionRequired.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class PreconditionRequired extends Exception { + static readonly STATUS = 428; name: string = "PRECONDITION_REQUIRED"; - constructor(message: string) { - super(428, message); + constructor(message: string, origin?: Error | string | any) { + super(PreconditionRequired.STATUS, message, origin); } } diff --git a/src/clientErrors/ProxyAuthentificationRequired.ts b/src/clientErrors/ProxyAuthentificationRequired.ts index bd75801..16fb2b5 100644 --- a/src/clientErrors/ProxyAuthentificationRequired.ts +++ b/src/clientErrors/ProxyAuthentificationRequired.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class ProxyAuthentificationRequired extends Exception { + static readonly STATUS = 407; name: string = "PROXY_AUTHENTIFICATION_REQUIRED"; - constructor(message: string) { - super(407, message); + constructor(message: string, origin?: Error | string | any) { + super(ProxyAuthentificationRequired.STATUS, message, origin); } } diff --git a/src/clientErrors/RequestEntityTooLarge.ts b/src/clientErrors/RequestEntityTooLarge.ts index b80af8b..e45d044 100644 --- a/src/clientErrors/RequestEntityTooLarge.ts +++ b/src/clientErrors/RequestEntityTooLarge.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class RequestEntityTooLarge extends Exception { + static readonly STATUS = 413; name: string = "REQUEST_ENTITY_TOO_LARGE"; - constructor(message: string) { - super(413, message); + constructor(message: string, origin?: Error | string | any) { + super(RequestEntityTooLarge.STATUS, message, origin); } } diff --git a/src/clientErrors/RequestHeaderFieldsTooLarge.ts b/src/clientErrors/RequestHeaderFieldsTooLarge.ts index c3a300f..ebc96de 100644 --- a/src/clientErrors/RequestHeaderFieldsTooLarge.ts +++ b/src/clientErrors/RequestHeaderFieldsTooLarge.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class RequestHeaderFieldsTooLarge extends Exception { + static readonly STATUS = 431; name: string = "REQUEST_HEADER_FIELDS_TOO_LARGE"; - constructor(message: string) { - super(431, message); + constructor(message: string, origin?: Error | string | any) { + super(RequestHeaderFieldsTooLarge.STATUS, message, origin); } } diff --git a/src/clientErrors/RequestTimeout.ts b/src/clientErrors/RequestTimeout.ts index ee3437c..2031032 100644 --- a/src/clientErrors/RequestTimeout.ts +++ b/src/clientErrors/RequestTimeout.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class RequestTimeout extends Exception { + static readonly STATUS = 408; name: string = "REQUEST_TIMEOUT"; - constructor(message: string) { - super(408, message); + constructor(message: string, origin?: Error | string | any) { + super(RequestTimeout.STATUS, message, origin); } } diff --git a/src/clientErrors/RequestURITooLong.ts b/src/clientErrors/RequestURITooLong.ts index 13e5333..e66d808 100644 --- a/src/clientErrors/RequestURITooLong.ts +++ b/src/clientErrors/RequestURITooLong.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class RequestURITooLong extends Exception { + static readonly STATUS = 414; name: string = "REQUEST_URI_TOO_LONG"; - constructor(message: string) { - super(414, message); + constructor(message: string, origin?: Error | string | any) { + super(RequestURITooLong.STATUS, message, origin); } } diff --git a/src/clientErrors/RequestedRangeUnsatisfiable.ts b/src/clientErrors/RequestedRangeUnsatisfiable.ts index ca9c057..13d66db 100644 --- a/src/clientErrors/RequestedRangeUnsatisfiable.ts +++ b/src/clientErrors/RequestedRangeUnsatisfiable.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class RequestedRangeUnsatisfiable extends Exception { + static readonly STATUS = 416; name: string = "REQUESTED_RANGE_UNSATISFIABLE"; - constructor(message: string) { - super(416, message); + constructor(message: string, origin?: Error | string | any) { + super(RequestedRangeUnsatisfiable.STATUS, message, origin); } } diff --git a/src/clientErrors/TooManyRequests.ts b/src/clientErrors/TooManyRequests.ts index bb85fd4..310b117 100644 --- a/src/clientErrors/TooManyRequests.ts +++ b/src/clientErrors/TooManyRequests.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class TooManyRequests extends Exception { + static readonly STATUS = 429; name: string = "TOO_MANY_REQUESTS"; - constructor(message: string) { - super(429, message); + constructor(message: string, origin?: Error | string | any) { + super(TooManyRequests.STATUS, message, origin); } } diff --git a/src/clientErrors/Unauthorized.ts b/src/clientErrors/Unauthorized.ts index 1724434..3d2a15f 100644 --- a/src/clientErrors/Unauthorized.ts +++ b/src/clientErrors/Unauthorized.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class Unauthorized extends Exception { + static readonly STATUS = 401; name: string = "UNAUTHORIZED"; - constructor(message: string) { - super(401, message); + constructor(message: string, origin?: Error | string | any) { + super(Unauthorized.STATUS, message, origin); } } diff --git a/src/clientErrors/UnavailabledForLegalReasons.ts b/src/clientErrors/UnavailabledForLegalReasons.ts index 23ac2b6..6715b95 100644 --- a/src/clientErrors/UnavailabledForLegalReasons.ts +++ b/src/clientErrors/UnavailabledForLegalReasons.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class UnavailabledForLegalReasons extends Exception { + static readonly STATUS = 451; name: string = "UNAVAILABLED_FOR_LEGAL_REASONS"; - constructor(message: string) { - super(451, message); + constructor(message: string, origin?: Error | string | any) { + super(UnavailabledForLegalReasons.STATUS, message, origin); } } diff --git a/src/clientErrors/UnprocessableEntity.ts b/src/clientErrors/UnprocessableEntity.ts index c69dd12..e081635 100644 --- a/src/clientErrors/UnprocessableEntity.ts +++ b/src/clientErrors/UnprocessableEntity.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class UnprocessableEntity extends Exception { + static readonly STATUS = 422; name: string = "UNPROCESSABLE_ENTITY"; - constructor(message: string) { - super(422, message); + constructor(message: string, origin?: Error | string | any) { + super(UnprocessableEntity.STATUS, message, origin); } } diff --git a/src/clientErrors/UnsupportedMediaType.ts b/src/clientErrors/UnsupportedMediaType.ts index 02c9792..b89f493 100644 --- a/src/clientErrors/UnsupportedMediaType.ts +++ b/src/clientErrors/UnsupportedMediaType.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class UnsupportedMediaType extends Exception { + static readonly STATUS = 415; name: string = "UNSUPPORTED_MEDIA_TYPE"; - constructor(message: string) { - super(415, message); + constructor(message: string, origin?: Error | string | any) { + super(UnsupportedMediaType.STATUS, message, origin); } } diff --git a/src/clientErrors/UpgradeRequired.ts b/src/clientErrors/UpgradeRequired.ts index ebc90bd..1cabb13 100644 --- a/src/clientErrors/UpgradeRequired.ts +++ b/src/clientErrors/UpgradeRequired.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class UpgradeRequired extends Exception { + static readonly STATUS = 426; name: string = "UPGRADE_REQUIRED"; - constructor(message: string) { - super(426, message); + constructor(message: string, origin?: Error | string | any) { + super(UpgradeRequired.STATUS, message, origin); } } diff --git a/src/core/Exception.ts b/src/core/Exception.ts index 7095a9e..e8e3b81 100644 --- a/src/core/Exception.ts +++ b/src/core/Exception.ts @@ -17,38 +17,39 @@ export class Exception extends Error { * Stack calling */ public stack: string; - public innerException: Error; + public origin: Error; /** * HTTP Code Status */ public status: number; - /** * - * @param status - * @param message - * @param innerException */ - constructor(status: any, message?: string, innerException?: any) { + public body: any; + + [key: string]: any; + + constructor(status: number = 500, message: string = "", origin?: Error | string | any) { super(message); this.status = status; - this.message = message || ""; + this.message = message; - if (innerException) { - if (innerException instanceof Error) { - this.innerException = innerException; - this.message = this.message + ", innerException: " + this.innerException.message; - } else if (typeof innerException === "string") { - this.innerException = new Error(innerException); - this.message = this.message + ", innerException: " + this.innerException.message; + this.setOrigin(origin); + } + + setOrigin(origin: Error | string | any) { + if (origin) { + if (origin instanceof Error) { + this.origin = origin; + this.message = `${this.message}, innerException: ${this.origin.message}`.trim(); + } else if (typeof origin === "string") { + this.origin = new Error(origin); + this.message = `${this.message}, innerException: ${this.origin.message}`.trim(); } else { - this.innerException = innerException; - this.message = this.message + ", innerException: " + this.innerException; + this.body = origin; } } - - this.message = (this.message + " ").trim(); } toString() { diff --git a/src/redirections/MovedPermanently.ts b/src/redirections/MovedPermanently.ts index f0e73ba..acc1f72 100644 --- a/src/redirections/MovedPermanently.ts +++ b/src/redirections/MovedPermanently.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class MovedPermanently extends Exception { + static readonly STATUS = 301; name: string = "MOVED_PERMANENTLY"; - constructor(message: string) { - super(301, message); + constructor(message: string, origin?: Error | string | any) { + super(MovedPermanently.STATUS, message, origin); } } diff --git a/src/redirections/MovedTemporarily.ts b/src/redirections/MovedTemporarily.ts index 46c2322..f4f22d3 100644 --- a/src/redirections/MovedTemporarily.ts +++ b/src/redirections/MovedTemporarily.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class MovedTemporarily extends Exception { + static readonly STATUS = 302; name: string = "MOVED_TEMPORARILY"; - constructor(message: string) { - super(302, message); + constructor(message: string, origin?: Error | string | any) { + super(MovedTemporarily.STATUS, message, origin); } } diff --git a/src/redirections/MultipleChoices.ts b/src/redirections/MultipleChoices.ts index 1a72016..371a995 100644 --- a/src/redirections/MultipleChoices.ts +++ b/src/redirections/MultipleChoices.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class MultipleChoices extends Exception { + static readonly STATUS = 300; name: string = "MULTIPLE_CHOICES"; - constructor(message: string) { - super(300, message); + constructor(message: string, origin?: Error | string | any) { + super(MultipleChoices.STATUS, message, origin); } } diff --git a/src/redirections/NotModified.ts b/src/redirections/NotModified.ts index b9c667e..a2b6121 100644 --- a/src/redirections/NotModified.ts +++ b/src/redirections/NotModified.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class NotModified extends Exception { + static readonly STATUS = 304; name: string = "NOT_MODIFIED"; - constructor(message: string) { - super(304, message); + constructor(message: string, origin?: Error | string | any) { + super(NotModified.STATUS, message, origin); } } diff --git a/src/redirections/PermanentRedirect.ts b/src/redirections/PermanentRedirect.ts index 5486bc4..d1df816 100644 --- a/src/redirections/PermanentRedirect.ts +++ b/src/redirections/PermanentRedirect.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class PermanentRedirect extends Exception { + static readonly STATUS = 308; name: string = "PERMANENT_REDIRECT"; - constructor(message: string) { - super(308, message); + constructor(message: string, origin?: Error | string | any) { + super(PermanentRedirect.STATUS, message, origin); } } diff --git a/src/redirections/SeeOther.ts b/src/redirections/SeeOther.ts index 45a61b2..f9ed094 100644 --- a/src/redirections/SeeOther.ts +++ b/src/redirections/SeeOther.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class SeeOther extends Exception { + static readonly STATUS = 303; name: string = "SEE_OTHER"; - constructor(message: string) { - super(303, message); + constructor(message: string, origin?: Error | string | any) { + super(SeeOther.STATUS, message, origin); } } diff --git a/src/redirections/TemporaryRedirect.ts b/src/redirections/TemporaryRedirect.ts index b726ab3..d9d5a2a 100644 --- a/src/redirections/TemporaryRedirect.ts +++ b/src/redirections/TemporaryRedirect.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class TemporaryRedirect extends Exception { + static readonly STATUS = 307; name: string = "TEMPORARY_REDIRECT"; - constructor(message: string) { - super(307, message); + constructor(message: string, origin?: Error | string | any) { + super(TemporaryRedirect.STATUS, message, origin); } } diff --git a/src/redirections/TooManyRedirects.ts b/src/redirections/TooManyRedirects.ts index 5cc9a72..4cac443 100644 --- a/src/redirections/TooManyRedirects.ts +++ b/src/redirections/TooManyRedirects.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class TooManyRedirects extends Exception { + static readonly STATUS = 310; name: string = "TOO_MANY_REDIRECTS"; - constructor(message: string) { - super(310, message); + constructor(message: string, origin?: Error | string | any) { + super(TooManyRedirects.STATUS, message, origin); } } diff --git a/src/redirections/UseProxy.ts b/src/redirections/UseProxy.ts index 9e6c7bf..effce06 100644 --- a/src/redirections/UseProxy.ts +++ b/src/redirections/UseProxy.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class UseProxy extends Exception { + static readonly STATUS = 305; name: string = "USE_PROXY"; - constructor(message: string) { - super(305, message); + constructor(message: string, origin?: Error | string | any) { + super(UseProxy.STATUS, message, origin); } } diff --git a/src/serverErrors/BadGateway.ts b/src/serverErrors/BadGateway.ts index ed5f089..4580d93 100644 --- a/src/serverErrors/BadGateway.ts +++ b/src/serverErrors/BadGateway.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class BadGateway extends Exception { + static readonly STATUS = 502; name: string = "BAD_GATEWAY"; - constructor(message: string) { - super(502, message); + constructor(message: string, origin?: Error | string | any) { + super(BadGateway.STATUS, message, origin); } } diff --git a/src/serverErrors/BandwidthLimitExceeded.ts b/src/serverErrors/BandwidthLimitExceeded.ts index a86bc41..f663413 100644 --- a/src/serverErrors/BandwidthLimitExceeded.ts +++ b/src/serverErrors/BandwidthLimitExceeded.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class BandwidthLimitExceeded extends Exception { + static readonly STATUS = 509; name: string = "BANDWIDTH_LIMIT_EXCEEDED"; - constructor(message: string) { - super(509, message); + constructor(message: string, origin?: Error | string | any) { + super(BandwidthLimitExceeded.STATUS, message, origin); } } diff --git a/src/serverErrors/GatewayTimeout.ts b/src/serverErrors/GatewayTimeout.ts index a9ca69e..13ccd02 100644 --- a/src/serverErrors/GatewayTimeout.ts +++ b/src/serverErrors/GatewayTimeout.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class GatewayTimeout extends Exception { + static readonly STATUS = 504; name: string = "GATEWAY_TIMEOUT"; - constructor(message: string) { - super(504, message); + constructor(message: string, origin?: Error | string | any) { + super(GatewayTimeout.STATUS, message, origin); } } diff --git a/src/serverErrors/InternalServerError.ts b/src/serverErrors/InternalServerError.ts index 9dd529a..38f3459 100644 --- a/src/serverErrors/InternalServerError.ts +++ b/src/serverErrors/InternalServerError.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class InternalServerError extends Exception { + static readonly STATUS = 500; name: string = "INTERNAL_SERVER_ERROR"; - constructor(message: string) { - super(500, message); + constructor(message: string, origin?: Error | string | any) { + super(InternalServerError.STATUS, message, origin); } } diff --git a/src/serverErrors/NetworkAuthenticationRequired.ts b/src/serverErrors/NetworkAuthenticationRequired.ts index 7a96086..0385d6b 100644 --- a/src/serverErrors/NetworkAuthenticationRequired.ts +++ b/src/serverErrors/NetworkAuthenticationRequired.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class NetworkAuthenticationRequired extends Exception { + static readonly STATUS = 511; name: string = "NETWORK_AUTHENTICATION_REQUIRED"; - constructor(message: string) { - super(511, message); + constructor(message: string, origin?: Error | string | any) { + super(NetworkAuthenticationRequired.STATUS, message, origin); } } diff --git a/src/serverErrors/NotExtended.ts b/src/serverErrors/NotExtended.ts index 0ebd5ff..659c011 100644 --- a/src/serverErrors/NotExtended.ts +++ b/src/serverErrors/NotExtended.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class NotExtended extends Exception { + static readonly STATUS = 510; name: string = "NOT_EXTENDED"; - constructor(message: string) { - super(510, message); + constructor(message: string, origin?: Error | string | any) { + super(NotExtended.STATUS, message, origin); } } diff --git a/src/serverErrors/NotImplemented.ts b/src/serverErrors/NotImplemented.ts index a2063cc..5cf63b0 100644 --- a/src/serverErrors/NotImplemented.ts +++ b/src/serverErrors/NotImplemented.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class NotImplemented extends Exception { + static readonly STATUS = 501; name: string = "NOT_IMPLEMENTED"; - constructor(message: string) { - super(501, message); + constructor(message: string, origin?: Error | string | any) { + super(NotImplemented.STATUS, message, origin); } } diff --git a/src/serverErrors/ProxyError.ts b/src/serverErrors/ProxyError.ts index 46ef25e..d6eb9f2 100644 --- a/src/serverErrors/ProxyError.ts +++ b/src/serverErrors/ProxyError.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class ProxyError extends Exception { + static readonly STATUS = 502; name: string = "PROXY_ERROR"; - constructor(message: string) { - super(502, message); + constructor(message: string, origin?: Error | string | any) { + super(ProxyError.STATUS, message, origin); } } diff --git a/src/serverErrors/ServiceUnvailable.ts b/src/serverErrors/ServiceUnvailable.ts index 2e52c72..fb7de6c 100644 --- a/src/serverErrors/ServiceUnvailable.ts +++ b/src/serverErrors/ServiceUnvailable.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class ServiceUnvailable extends Exception { + static readonly STATUS = 503; name: string = "SERVICE_UNVAILABLE"; - constructor(message: string) { - super(503, message); + constructor(message: string, origin?: Error | string | any) { + super(ServiceUnvailable.STATUS, message, origin); } } diff --git a/src/serverErrors/VariantAlsoNegotiates.ts b/src/serverErrors/VariantAlsoNegotiates.ts index 0d9ff02..edb5a41 100644 --- a/src/serverErrors/VariantAlsoNegotiates.ts +++ b/src/serverErrors/VariantAlsoNegotiates.ts @@ -1,9 +1,10 @@ import {Exception} from "../core/Exception"; export class VariantAlsoNegotiates extends Exception { + static readonly STATUS = 506; name: string = "VARIANT_ALSO_NEGOTIATES"; - constructor(message: string) { - super(506, message); + constructor(message: string, origin?: Error | string | any) { + super(VariantAlsoNegotiates.STATUS, message, origin); } } diff --git a/test/Exception.spec.ts b/test/Exception.spec.ts index 24fcc71..1f4ae82 100644 --- a/test/Exception.spec.ts +++ b/test/Exception.spec.ts @@ -2,25 +2,33 @@ import {expect} from "chai"; import {Exception} from "../src"; describe("Exception", () => { - it("should use innerException", () => { + it("should use origin", () => { + const exception = new Exception(undefined, "test", new Error("test")); + + expect(exception.status).to.equal(500); + expect(exception.toString()).to.equal("HTTP_EXCEPTION(500): test, innerException: test"); + }); + + it("should use origin", () => { const exception = new Exception(203, "test", new Error("test")); expect(exception.status).to.equal(203); expect(exception.toString()).to.equal("HTTP_EXCEPTION(203): test, innerException: test"); }); - it("should use innerException as string", () => { + it("should use origin as string", () => { const exception = new Exception(203, "test", "test"); expect(exception.status).to.equal(203); expect(exception.toString()).to.equal("HTTP_EXCEPTION(203): test, innerException: test"); }); - it("should use innerException as string", () => { - const exception = new Exception(203, "test", 1); + it("should use origin as string", () => { + const exception = new Exception(203, "test", {}); expect(exception.status).to.equal(203); - expect(exception.toString()).to.equal("HTTP_EXCEPTION(203): test, innerException: 1"); + expect(exception.toString()).to.equal("HTTP_EXCEPTION(203): test"); + expect(exception.body).to.deep.equal({}); }); it("should return empty message when message parameters is undefined", () => {