@xylabs/error
Advanced tools
| type AssertCallback = (value: unknown, message: string) => string | boolean; | ||
| /** Configuration for assertion behavior: a static message string, a boolean toggle, or a callback. */ | ||
| export type AssertConfig = string | AssertCallback | boolean; | ||
| /** | ||
| * Throws an Error based on the assert configuration when a value fails validation. | ||
| * @param value - The value being validated | ||
| * @param assert - Assertion config controlling the error message | ||
| * @param defaultMessage - Fallback message if no custom message is provided | ||
| */ | ||
| export declare const assertError: (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => undefined; | ||
| export {}; | ||
| //# sourceMappingURL=assert.d.ts.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/assert.ts"],"names":[],"mappings":"AAEA,KAAK,cAAc,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,MAAM,GAAG,OAAO,CAAA;AAE3E,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,cAAc,GAAG,OAAO,CAAA;AAE5D,eAAO,MAAM,WAAW,GAAI,OAAO,OAAO,EAAE,QAAQ,YAAY,GAAG,SAAS,EAAE,gBAAgB,MAAM,cAcnG,CAAA"} | ||
| {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/assert.ts"],"names":[],"mappings":"AAEA,KAAK,cAAc,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,MAAM,GAAG,OAAO,CAAA;AAE3E,sGAAsG;AACtG,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,cAAc,GAAG,OAAO,CAAA;AAE5D;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,OAAO,OAAO,EAAE,QAAQ,YAAY,GAAG,SAAS,EAAE,gBAAgB,MAAM,cAcnG,CAAA"} |
@@ -0,3 +1,15 @@ | ||
| /** | ||
| * Invokes the handler if the value is an Error, otherwise re-throws it. | ||
| * @param error - The caught value to inspect | ||
| * @param handler - Callback invoked with the Error if it is one | ||
| * @returns The handler's return value | ||
| */ | ||
| export declare const handleError: <T>(error: any, handler: (error: Error) => T) => T; | ||
| /** | ||
| * Async version of handleError. Invokes the async handler if the value is an Error, otherwise re-throws it. | ||
| * @param error - The caught value to inspect | ||
| * @param handler - Async callback invoked with the Error if it is one | ||
| * @returns The handler's resolved return value | ||
| */ | ||
| export declare const handleErrorAsync: <T>(error: any, handler: (error: Error) => Promise<T>) => Promise<T>; | ||
| //# sourceMappingURL=handleError.d.ts.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"handleError.d.ts","sourceRoot":"","sources":["../../src/handleError.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,WAAW,GAAI,CAAC,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE,KAAK,KAAK,CAAC,MAMtE,CAAA;AAGD,eAAO,MAAM,gBAAgB,GAAU,CAAC,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,CAAC,CAAC,eAM1F,CAAA"} | ||
| {"version":3,"file":"handleError.d.ts","sourceRoot":"","sources":["../../src/handleError.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AAEH,eAAO,MAAM,WAAW,GAAI,CAAC,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE,KAAK,KAAK,CAAC,MAMtE,CAAA;AAED;;;;;GAKG;AAEH,eAAO,MAAM,gBAAgB,GAAU,CAAC,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,CAAC,CAAC,eAM1F,CAAA"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../../src/assert.ts","../../src/handleError.ts","../../src/index.ts"],"sourcesContent":["import { isString, isUndefined } from '@xylabs/typeof'\n\ntype AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (!isUndefined(assert)) {\n const assertString\n = typeof assert === 'string'\n ? assert\n : typeof assert === 'boolean'\n ? defaultMessage\n : assert(value, defaultMessage)\n if (isString(assertString) || assertString === true) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isError } from '@xylabs/typeof'\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const handleError = <T>(error: any, handler: (error: Error) => T) => {\n if (isError(error)) {\n return handler(error)\n } else {\n throw error\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const handleErrorAsync = async <T>(error: any, handler: (error: Error) => Promise<T>) => {\n if (isError(error)) {\n return await handler(error)\n } else {\n throw error\n }\n}\n","export * from './assert.ts'\nexport * from './handleError.ts'\nexport { isError } from '@xylabs/typeof'\n"],"mappings":";AAAA,SAAS,UAAU,mBAAmB;AAM/B,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,CAAC,YAAY,MAAM,GAAG;AACxB,UAAM,eACF,OAAO,WAAW,WAChB,SACA,OAAO,WAAW,YAChB,iBACA,OAAO,OAAO,cAAc;AACpC,QAAI,SAAS,YAAY,KAAK,iBAAiB,MAAM;AACnD,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;ACpBA,SAAS,eAAe;AAGjB,IAAM,cAAc,CAAI,OAAY,YAAiC;AAC1E,MAAI,QAAQ,KAAK,GAAG;AAClB,WAAO,QAAQ,KAAK;AAAA,EACtB,OAAO;AACL,UAAM;AAAA,EACR;AACF;AAGO,IAAM,mBAAmB,OAAU,OAAY,YAA0C;AAC9F,MAAI,QAAQ,KAAK,GAAG;AAClB,WAAO,MAAM,QAAQ,KAAK;AAAA,EAC5B,OAAO;AACL,UAAM;AAAA,EACR;AACF;;;AChBA,SAAS,WAAAA,gBAAe;","names":["isError"]} | ||
| {"version":3,"sources":["../../src/assert.ts","../../src/handleError.ts","../../src/index.ts"],"sourcesContent":["import { isString, isUndefined } from '@xylabs/typeof'\n\ntype AssertCallback = (value: unknown, message: string) => string | boolean\n\n/** Configuration for assertion behavior: a static message string, a boolean toggle, or a callback. */\nexport type AssertConfig = string | AssertCallback | boolean\n\n/**\n * Throws an Error based on the assert configuration when a value fails validation.\n * @param value - The value being validated\n * @param assert - Assertion config controlling the error message\n * @param defaultMessage - Fallback message if no custom message is provided\n */\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (!isUndefined(assert)) {\n const assertString\n = typeof assert === 'string'\n ? assert\n : typeof assert === 'boolean'\n ? defaultMessage\n : assert(value, defaultMessage)\n if (isString(assertString) || assertString === true) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isError } from '@xylabs/typeof'\n\n/**\n * Invokes the handler if the value is an Error, otherwise re-throws it.\n * @param error - The caught value to inspect\n * @param handler - Callback invoked with the Error if it is one\n * @returns The handler's return value\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const handleError = <T>(error: any, handler: (error: Error) => T) => {\n if (isError(error)) {\n return handler(error)\n } else {\n throw error\n }\n}\n\n/**\n * Async version of handleError. Invokes the async handler if the value is an Error, otherwise re-throws it.\n * @param error - The caught value to inspect\n * @param handler - Async callback invoked with the Error if it is one\n * @returns The handler's resolved return value\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const handleErrorAsync = async <T>(error: any, handler: (error: Error) => Promise<T>) => {\n if (isError(error)) {\n return await handler(error)\n } else {\n throw error\n }\n}\n","export * from './assert.ts'\nexport * from './handleError.ts'\nexport { isError } from '@xylabs/typeof'\n"],"mappings":";AAAA,SAAS,UAAU,mBAAmB;AAa/B,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,CAAC,YAAY,MAAM,GAAG;AACxB,UAAM,eACF,OAAO,WAAW,WAChB,SACA,OAAO,WAAW,YAChB,iBACA,OAAO,OAAO,cAAc;AACpC,QAAI,SAAS,YAAY,KAAK,iBAAiB,MAAM;AACnD,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;AC3BA,SAAS,eAAe;AASjB,IAAM,cAAc,CAAI,OAAY,YAAiC;AAC1E,MAAI,QAAQ,KAAK,GAAG;AAClB,WAAO,QAAQ,KAAK;AAAA,EACtB,OAAO;AACL,UAAM;AAAA,EACR;AACF;AASO,IAAM,mBAAmB,OAAU,OAAY,YAA0C;AAC9F,MAAI,QAAQ,KAAK,GAAG;AAClB,WAAO,MAAM,QAAQ,KAAK;AAAA,EAC5B,OAAO;AACL,UAAM;AAAA,EACR;AACF;;;AC5BA,SAAS,WAAAA,gBAAe;","names":["isError"]} |
+4
-4
| { | ||
| "name": "@xylabs/error", | ||
| "version": "5.0.83", | ||
| "version": "5.0.84", | ||
| "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries", | ||
@@ -45,7 +45,7 @@ "keywords": [ | ||
| "dependencies": { | ||
| "@xylabs/typeof": "~5.0.83" | ||
| "@xylabs/typeof": "~5.0.84" | ||
| }, | ||
| "devDependencies": { | ||
| "@xylabs/ts-scripts-yarn3": "~7.4.11", | ||
| "@xylabs/tsconfig": "~7.4.11", | ||
| "@xylabs/ts-scripts-yarn3": "~7.4.13", | ||
| "@xylabs/tsconfig": "~7.4.13", | ||
| "typescript": "~5.9.3", | ||
@@ -52,0 +52,0 @@ "vitest": "^4.0.18" |
+30
-0
@@ -50,2 +50,4 @@ # @xylabs/error | ||
| Throws an Error based on the assert configuration when a value fails validation. | ||
| ## Parameters | ||
@@ -57,4 +59,8 @@ | ||
| The value being validated | ||
| ### assert | ||
| Assertion config controlling the error message | ||
| [`AssertConfig`](#../type-aliases/AssertConfig) | `undefined` | ||
@@ -66,2 +72,4 @@ | ||
| Fallback message if no custom message is provided | ||
| ## Returns | ||
@@ -81,2 +89,4 @@ | ||
| Invokes the handler if the value is an Error, otherwise re-throws it. | ||
| ## Type Parameters | ||
@@ -94,2 +104,4 @@ | ||
| The caught value to inspect | ||
| ### handler | ||
@@ -99,2 +111,4 @@ | ||
| Callback invoked with the Error if it is one | ||
| ## Returns | ||
@@ -104,2 +118,4 @@ | ||
| The handler's return value | ||
| ### <a id="handleErrorAsync"></a>handleErrorAsync | ||
@@ -115,2 +131,4 @@ | ||
| Async version of handleError. Invokes the async handler if the value is an Error, otherwise re-throws it. | ||
| ## Type Parameters | ||
@@ -128,2 +146,4 @@ | ||
| The caught value to inspect | ||
| ### handler | ||
@@ -133,2 +153,4 @@ | ||
| Async callback invoked with the Error if it is one | ||
| ## Returns | ||
@@ -138,2 +160,4 @@ | ||
| The handler's resolved return value | ||
| ### <a id="isError"></a>isError | ||
@@ -151,2 +175,4 @@ | ||
| Type guard that checks whether a value is an Error instance. | ||
| ### Parameters | ||
@@ -168,2 +194,4 @@ | ||
| Type guard that checks whether a value is an Error instance. | ||
| ### Type Parameters | ||
@@ -197,3 +225,5 @@ | ||
| Configuration for assertion behavior: a static message string, a boolean toggle, or a callback. | ||
| Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js) | ||
@@ -200,0 +230,0 @@ |
20388
15.21%63
43.18%252
13.51%Updated