import { Response } from './response'; const ERRORS = { BadRequest: 'The request was malformed.', Unauthorized: 'The request failed authorization.', NotFound: 'The request resource was not found.', MethodNotAllowed: 'The requested method is not allowed.', InternalError: 'An internal error occured.', Forbidden: 'Insufficient permissions to access the resource.', }; /** * The base error class. Implements a standard Javascript Error object that * can be sent as an HTTP response. * * @export * @class HTTPError * @extends {Error} */ export class HTTPError extends Error { /** * The status code of the response. * * @memberof HTTPError */ code = 500; /** * The message associated with the response code. * * @memberof HTTPError */ message = ERRORS.InternalError; /** * Creates an instance of HTTPError. * @param {number} [code] * @param {string} [message] * @memberof HTTPError */ constructor(code?: number, message?: string) { super(message); // https://bit.ly/2jFnR9i Object.setPrototypeOf(this, HTTPError.prototype); this.code = code || this.code; this.message = message || this.message; } /** * Returns a response object suitable for serverless. * * @returns {Response} * @memberof HTTPError */ toResponse(res?: Response): Response { let body = { code: this.code, message: this.message }; const errRes = new Response(this.code, body); if (res) { errRes.headers = res.headers; } return errRes; } } /** * An implementation of a Bad Request error. * * @export * @class BadRequest * @extends {HTTPError} */ export class BadRequest extends HTTPError { constructor(message: string=ERRORS.BadRequest) { super(400, message); Object.setPrototypeOf(this, BadRequest.prototype); } } /** * An implementation of an Unauthorized error. * * @export * @class Unauthorized * @extends {HTTPError} */ export class Unauthorized extends HTTPError { constructor(message: string=ERRORS.Unauthorized) { super(401, message); Object.setPrototypeOf(this, Unauthorized.prototype); } } /** * An implementation of a Forbidden error. * * @export * @class Forbidden * @extends {HTTPError} */ export class Forbidden extends HTTPError { constructor(message: string=ERRORS.Forbidden) { super(403, message); Object.setPrototypeOf(this, Forbidden.prototype); } } /** * An implementation of a Not Found error. * * @export * @class NotFound * @extends {HTTPError} */ export class NotFound extends HTTPError { constructor(message: string=ERRORS.NotFound) { super(404, message); Object.setPrototypeOf(this, NotFound.prototype); } } /** * An implementation of a Method Not Allowed error. * * @export * @class MethodNotAllowed * @extends {HTTPError} */ export class MethodNotAllowed extends HTTPError { constructor(message: string=ERRORS.MethodNotAllowed) { super(405, message); Object.setPrototypeOf(this, MethodNotAllowed.prototype); } } /** * An implementation of an Internal Error error. * * @export * @class InternalError * @extends {HTTPError} */ export class InternalError extends HTTPError { constructor(message: string=ERRORS.InternalError) { super(500, message); Object.setPrototypeOf(this, InternalError.prototype); } }