'use strict'; function isObject(item) { return !!item && typeof item === 'object' && !Array.isArray(item); } function createExtractOptionsFn(fn) { return (...input)=>{ const output = {}; for(let i = 0; i < input.length; i++){ const element = input[i]; if (typeof element === 'string') { output.message = element; continue; } // message,stack & cause are not enumerable if (element instanceof Error) { output.message = element.message; output.stack = element.stack; output.cause = element; } // if element prototype is not of instance Error, // then message, stack & cause get extracted here. if (fn(element)) { const keys = Object.keys(element); for(let i = 0; i < keys.length; i++){ output[keys[i]] = element[keys[i]]; } } } return output; }; } function isOptions(input) { if (!isObject(input)) { return false; } if (typeof input.message !== 'undefined' && typeof input.message !== 'string') { return false; } if (typeof input.stack !== 'undefined' && typeof input.stack !== 'string') { return false; } if (typeof input.code !== 'undefined' && typeof input.code !== 'number' && typeof input.code !== 'string' && input.code !== null) { return false; } if (typeof input.expose !== 'undefined' && typeof input.expose !== 'boolean') { return false; } if (typeof input.logMessage !== 'undefined' && typeof input.logMessage !== 'boolean') { return false; } return typeof input.logLevel === 'undefined' || typeof input.logLevel === 'number' || typeof input.logLevel === 'string'; } const check = createExtractOptionsFn(isOptions); function extractOptions(...input) { return check(...input); } class BaseError extends Error { //-------------------------------------------------------------------- constructor(...input){ const options = extractOptions(...input); super(options.message, { cause: options.cause }); if (typeof this.name === 'undefined' || this.name === 'Error') { Object.defineProperty(this, 'name', { configurable: true, enumerable: false, value: this.constructor.name, writable: true }); } if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } // override existing stack if (options.stack) { this.stack = options.stack; } this.code = options.code; this.expose = options.expose; this.logMessage = options.logMessage; this.logLevel = options.logLevel; this.data = options.data; } } function isBaseError(input) { if (!isOptions(input)) { return false; } return typeof input.message === 'string'; } exports.BaseError = BaseError; exports.createExtractOptionsFn = createExtractOptionsFn; exports.extractOptions = extractOptions; exports.isBaseError = isBaseError; exports.isObject = isObject; exports.isOptions = isOptions; //# sourceMappingURL=index.cjs.map