@magic.batua/error
Version:
The Error module powers the error handling features of the Magic Batua.
57 lines • 2.24 kB
JavaScript
;
/**
* @module @magic.batua/error
* @overview Defines the `Error` handling interfaces used throughout the Magic Batua
* codebase.
*
* @author Animesh Mishra <hello@animesh.ltd>
* @copyright © 2018 Animesh Ltd. All Rights Reserved.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const Code_1 = require("./Source/Code");
exports.Code = Code_1.Code;
const ClientError_1 = require("./Source/ClientError");
exports.ClientError = ClientError_1.ClientError;
const ExternalError_1 = require("./Source/ExternalError");
exports.ExternalError = ExternalError_1.ExternalError;
/**
* @exports Error
*/
exports.description = "Exports error handling functionalities";
/**
* All the errors thrown by the application code, in any of the modules,
* are routed through this method.
*
* Upon receiving an `error`, the method first runs an identity check.
*
* - If the `error` is of type `ClientError`, it sends an error response with
* one of the 4xx status codes.
* - If the `error` is of type `ExternalError`, it sends an error response with
* a `424 Failed Dependency` status code.
* - If the `error` is of any other type, it's assumed to an internal server
* error and a `500 Internal Server Error` response is sent.
*
*
* Regardless of the error type, each response includes a detailed description,
* help message and debugging information in the response body.
*
* @param { Error } error The error thrown
* @param { Express.Response } response The response object that will be used to
* communicate the error back to the client
*/
function Handle(error, response) {
switch (error.constructor) {
case ClientError_1.ClientError:
let clientError = error;
response.status(clientError.code).json(clientError.Export());
break;
case ExternalError_1.ExternalError:
let externalError = error;
response.status(externalError.code).json(externalError.Export());
break;
default:
response.status(Code_1.Code.Internal).send(error.message);
}
}
exports.Handle = Handle;
//# sourceMappingURL=index.js.map