From ff1417f815e13fe94f0919f07285132e151aebe5 Mon Sep 17 00:00:00 2001 From: Anshuman Kumar Date: Tue, 21 Jun 2022 21:36:49 +0530 Subject: [PATCH] Re-add APIError file contents File was perhaps deleted by mistakes --- src/api/utils/APIError.js | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/api/utils/APIError.js b/src/api/utils/APIError.js index e69de29b..36d8e141 100644 --- a/src/api/utils/APIError.js +++ b/src/api/utils/APIError.js @@ -0,0 +1,46 @@ +const httpStatus = require('http-status'); + +/** + * @extends Error + */ +class ExtendableError extends Error { + constructor({ + message, errors, status, isPublic, stack, + }) { + super(message); + this.name = this.constructor.name; + this.message = message; + this.errors = errors; + this.status = status; + this.isPublic = isPublic; + this.isOperational = true; // This is required since bluebird 4 doesn't append it anymore. + this.stack = stack; + // Error.captureStackTrace(this, this.constructor.name); + } +} + +/** + * Class representing an API error. + * @extends ExtendableError + */ +class APIError extends ExtendableError { + /** + * Creates an API error. + * @param {string} message - Error message. + * @param {number} status - HTTP status code of error. + * @param {boolean} isPublic - Whether the message should be visible to user or not. + */ + constructor({ + message, + errors, + stack, + status = httpStatus.INTERNAL_SERVER_ERROR, + isPublic = false, + }) { + super({ + message, errors, status, isPublic, stack, + }); + } +} + +module.exports = APIError;