@mdf.js/crash
Version:
MMS - API Crash - Enhanced error management library
173 lines • 5.99 kB
JavaScript
"use strict";
/**
* Copyright 2024 Mytra Control S.L. All rights reserved.
*
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
* or at https://opensource.org/licenses/MIT.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Base = void 0;
const uuid_1 = require("uuid");
const const_1 = require("./const");
/** Class Base, manages errors in MDF Systems */
class Base extends Error {
/**
* Create a new Base error
* @param message - human friendly error message
* @param uuid - unique identifier for this particular occurrence of the problem
* @param options - enhanced error options
*/
constructor(message, uuid, options) {
var _a, _b, _c, _d, _e;
super(message);
/** Error name (type) */
this.name = 'BaseError';
this.message = this.isSafeMessage(message, uuid);
this._uuid = this.isSafeId(uuid);
this._options = this.isSafeOptions(options || uuid);
this.name = this.isSafeName((_a = this._options) === null || _a === void 0 ? void 0 : _a.name);
this.date = this.isSafeDate((_c = (_b = this._options) === null || _b === void 0 ? void 0 : _b.info) === null || _c === void 0 ? void 0 : _c.date);
this.subject = this.isSafeSubject((_e = (_d = this._options) === null || _d === void 0 ? void 0 : _d.info) === null || _e === void 0 ? void 0 : _e.subject);
// #endregion
Error.captureStackTrace(this, this.constructor);
}
/** Return the info object for this error */
get info() {
return this.parseInfo();
}
/** Return the unique identifier associated to this instance */
get uuid() {
return this._uuid;
}
/** Return a string formatted as `name:message` */
toString() {
return `${this.name}: ${this.message}`;
}
/** Return Base error in JSON format */
toObject() {
return {
name: this.name,
message: this.message,
uuid: this._uuid,
timestamp: this.date.toISOString(),
subject: this.subject,
info: this.parseInfo(),
};
}
/**
* Clean the info object in order to main only values that are not included in the rest of fields
*/
parseInfo() {
var _a, _b;
if (!((_a = this._options) === null || _a === void 0 ? void 0 : _a.info)) {
return undefined;
}
const info = { ...(_b = this._options) === null || _b === void 0 ? void 0 : _b.info, date: undefined, subject: undefined };
if (Object.values(info).every(value => typeof value === 'undefined')) {
return undefined;
}
else {
return info;
}
}
/**
* Check if the message is safe to be setted
* @param message - human friendly error message
* @param uuid - UUID V4, unique identifier for this particular occurrence of the problem
* @returns
*/
isSafeMessage(message, uuid) {
if (typeof message !== 'string') {
throw new Base('Message parameter must be a string', uuid);
}
if (message.length > const_1.CONFIG_MAX_ERROR_MESSAGE_LENGTH) {
message = `${message.substring(0, const_1.CONFIG_MAX_ERROR_MESSAGE_LENGTH - 18)} ...too long error`;
}
return message;
}
/**
* Check if the passed UUID is valid and safe
* @param uuid - UUID V4, unique identifier for this particular occurrence of the problem
* @returns
*/
isSafeId(uuid) {
if (typeof uuid === 'string' && (0, uuid_1.validate)(uuid)) {
return uuid;
}
else if (typeof uuid === 'object' && !Array.isArray(uuid)) {
return (0, uuid_1.v4)();
}
else if (typeof uuid === 'undefined') {
return (0, uuid_1.v4)();
}
else {
throw new Base('uuid parameter must be an string and RFC 4122 based', (0, uuid_1.v4)());
}
}
/**
* Check if the passed options are valid and safe
* @param options - options to be setted
* @returns
*/
isSafeOptions(options) {
if (typeof options === 'object' && !Array.isArray(options)) {
return options;
}
else if (['string', 'undefined'].includes(typeof options)) {
return undefined;
}
else {
throw new Base('options parameter must be an object', (0, uuid_1.v4)());
}
}
/**
* Check if the name, passed in options, is valid and safe
* @param name - name of the error, used as category name
* @returns
*/
isSafeName(name) {
if (typeof name === 'string') {
return name;
}
else if (typeof name !== 'undefined') {
throw new Base('Option Parameter name must a string', this._uuid);
}
else {
return this.name;
}
}
/**
* Check if the date, passed in options, is valid and safe
* @param date - date of the error
* @returns
*/
isSafeDate(date) {
if (date instanceof Date) {
return date;
}
else if (typeof date !== 'undefined') {
throw new Base('Option Parameter info.date, if its setted, must be a Date', this._uuid);
}
else {
return new Date();
}
}
/**
* Check if the subject, passed in options, is valid and safe
* @param subject - subject of the error, `'common' as default`
* @returns
*/
isSafeSubject(subject) {
if (typeof subject === 'string') {
return subject;
}
else if (typeof subject !== 'undefined') {
throw new Base('Option Parameter info.subject, if it is setted, must be a string', this._uuid);
}
else {
return 'common';
}
}
}
exports.Base = Base;
//# sourceMappingURL=BaseError.js.map