@leonardoraele/err
Advanced tools
| export interface ErrJSON { | ||
| message: string; | ||
| details?: Record<string, unknown>; | ||
| cause?: ErrJSON; | ||
| } | ||
| export declare class Err extends Error { | ||
| #private; | ||
| static from(error: unknown): Err; | ||
| static assert(condition: unknown, message?: string, detailsProvider?: () => Record<string, unknown>): asserts condition; | ||
| static throw(...args: ConstructorParameters<typeof Err>): never; | ||
| private details; | ||
| with(details: Record<string, unknown>): Err; | ||
| causes(message: string): Err; | ||
| throw(): never; | ||
| toJSON(): ErrJSON; | ||
| toString(): string; | ||
| } |
| var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
| if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
| if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
| return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
| }; | ||
| var _Err_instances, _a, _Err_toJSON; | ||
| export class Err extends Error { | ||
| constructor() { | ||
| super(...arguments); | ||
| _Err_instances.add(this); | ||
| this.details = undefined; | ||
| } | ||
| static from(error) { | ||
| return error instanceof _a ? error : new _a(String(error)); | ||
| } | ||
| static assert(condition, message = 'Assertion failed.', detailsProvider) { | ||
| if (!condition) { | ||
| const error = new _a(message); | ||
| const details = detailsProvider === null || detailsProvider === void 0 ? void 0 : detailsProvider(); | ||
| if (details) { | ||
| error.with(details); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
| static throw(...args) { | ||
| throw new _a(...args); | ||
| } | ||
| with(details) { | ||
| this.details = Object.freeze(Object.assign({}, this.details, details)); | ||
| return this; | ||
| } | ||
| causes(message) { | ||
| return new _a(message, { cause: this }); | ||
| } | ||
| throw() { | ||
| throw this; | ||
| } | ||
| toJSON() { | ||
| return __classPrivateFieldGet(this, _Err_instances, "m", _Err_toJSON).call(this, this); | ||
| } | ||
| toString() { | ||
| const { message, details } = this.toJSON(); | ||
| const stack = this.stack && `at:\n${this.stack}`; | ||
| const cause = this.cause instanceof Error ? '\nCaused by: ' + this.cause.toString() | ||
| : this.cause ? `\nCaused by: ${String(this.cause)}` | ||
| : undefined; | ||
| return [message, details, stack, cause].filter(Boolean).join(' '); | ||
| } | ||
| } | ||
| _a = Err, _Err_instances = new WeakSet(), _Err_toJSON = function _Err_toJSON(value) { | ||
| return value instanceof _a | ||
| ? Object.assign({ message: value.message || '<empty error message>', details: (() => { | ||
| try { | ||
| return JSON.parse(JSON.stringify(value.details)); | ||
| } | ||
| catch (e) { | ||
| return {}; | ||
| } | ||
| })() }, (value.cause ? { cause: __classPrivateFieldGet(this, _Err_instances, "m", _Err_toJSON).call(this, value.cause) } : {})) : value instanceof Error | ||
| ? Object.assign({ message: value.message || '<empty error message>' }, (value.cause ? { cause: __classPrivateFieldGet(this, _Err_instances, "m", _Err_toJSON).call(this, value.cause) } : {})) : Object.assign({ message: String(value) }, (() => { | ||
| try { | ||
| return { details: JSON.parse(JSON.stringify(value)) }; | ||
| } | ||
| catch (e) { | ||
| return {}; | ||
| } | ||
| })()); | ||
| }; |
+3
-2
| { | ||
| "name": "@leonardoraele/err", | ||
| "version": "0.2.0", | ||
| "version": "0.2.1", | ||
| "description": "A simple error class with support for adding context data", | ||
@@ -15,3 +15,4 @@ "author": "Leonardo Raele <leonardoraele@gmail.com>", | ||
| "build": "tsc", | ||
| "prepublish": "npm run build" | ||
| "prebuild": "rimraf dist", | ||
| "prepack": "npm run build" | ||
| }, | ||
@@ -18,0 +19,0 @@ "devDependencies": { |
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
6127
138.4%5
66.67%86
Infinity%0
-100%