UNPKG

berror

Version:

Simple error handling

64 lines (63 loc) 2.51 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var BError = /** @class */ (function (_super) { __extends(BError, _super); function BError(msg, cause, metadata) { var _this = _super.call(this, msg) || this; _this.metadata = metadata; _this.firstStack = undefined; // Workaround for https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work Object.setPrototypeOf(_this, BError.prototype); if (cause != undefined) { var subMessage = cause instanceof Error ? cause.message : "non-error object thrown: " + JSON.stringify(cause); _this.message = _this.message + ": " + subMessage; if (!_this.firstStack) { _this.firstStack = cause.stack; } } _this.stack = _this.firstStack; _this.metadata = __assign(__assign({}, (cause instanceof BError ? cause.metadata : {})), metadata); return _this; } BError.prototype.log = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var noMetadata = !this.metadata || Object.entries(this.metadata).length === 0; if (noMetadata) { console.error(this.message); } else { console.error(this.message, this.metadata); } }; return BError; }(Error)); exports.BError = BError;