UNPKG

@boost/internal

Version:
34 lines (32 loc) 1.14 kB
const internalErrors = { INVALID_SCOPE_NAME: 'Error scope must be 3 characters and all uppercase.', UNKNOWN_ERROR: 'An unknown error has occurred.' }; const TOKEN_PATTERN = /\{(\d+)\}/gu; function createScopedError(scope, name, errors) { function msg(code, messages, params = []) { if (!messages[code]) { return ''; } return `${messages[code].replace(TOKEN_PATTERN, (match, index) => String(params[index]))} [${scope}:${code}]`; } if (process.env.NODE_ENV !== "production" && (scope.length !== 3 || scope !== scope.toUpperCase())) { throw new Error(msg('INVALID_SCOPE_NAME', internalErrors)); } return class InternalError extends Error { constructor(code, params) { super(msg(code, errors, params)); this.code = void 0; this.scope = scope; this.code = code; this.name = name; // If a message was not loaded, we are throwing an unknown error if (!this.message) { this.code = 'UNKNOWN_ERROR'; this.message = msg('UNKNOWN_ERROR', internalErrors); } } }; } export { createScopedError }; //# sourceMappingURL=createScopedError.mjs.map