UNPKG

1.91 kBJavaScriptView Raw
1"use strict";
2/**
3 * @author: Prachi Singh (prachi@hackcapital.com)
4 * @date: Monday, 6th May 2019 11:11:49 am
5 * @lastModifiedBy: Prachi Singh (prachi@hackcapital.com)
6 * @lastModifiedTime: Thursday, 3rd October 2019 4:46:05 pm
7 *
8 * DESCRIPTION: Base class that the custom errors should be extending from
9 * Error template that provides a modular, extensible and customizable errors.
10 * Sourced from https://git.cto.ai/hackcapital/tools/errors
11 * Modified slightly to fit the application's need
12 *
13 * @copyright (c) 2019 Hack Capital
14 */
15Object.defineProperty(exports, "__esModule", { value: true });
16const errorSource_1 = require("../constants/errorSource");
17const { UNEXPECTED } = errorSource_1.errorSource;
18/**
19 * ErrorTemplate class provide a base class for customized errors
20 *
21 * @extends Error
22 */
23class ErrorTemplate extends Error {
24 /**
25 * @constructor
26 *
27 * @param {String} [message] Error Message
28 * @param {Object} [extra] Append any extra information to the error message
29 * @param {Number} [statusCode] Specific for HTTP request, e.g.: 404
30 * @param {String} [errorCode] Error codes, e.g.: U0010
31 * @param {Error} [original] Original error object
32 */
33 constructor(message, original, extra = { exit: true, source: UNEXPECTED }, statusCode, errorCode) {
34 super(message);
35 this.message = message;
36 this.original = original;
37 this.statusCode = statusCode;
38 this.errorCode = errorCode;
39 if (!message)
40 throw new Error('Need to specify a message');
41 if (extra.exit === undefined)
42 extra.exit = true;
43 if (extra.source === undefined)
44 extra.source = UNEXPECTED;
45 this.extra = {
46 exit: extra.exit,
47 source: extra.source,
48 };
49 // name the error
50 this.name = this.constructor.name;
51 }
52}
53exports.ErrorTemplate = ErrorTemplate;