@xylabs/error 5.0.83 → 5.0.86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
|
|
16
16
|
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
17
17
|
|
|
18
|
+
|
|
19
|
+
|
|
18
20
|
## Reference
|
|
19
21
|
|
|
20
22
|
**@xylabs/error**
|
|
@@ -23,14 +25,18 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
|
23
25
|
|
|
24
26
|
## Type Aliases
|
|
25
27
|
|
|
26
|
-
|
|
28
|
+
| Type Alias | Description |
|
|
29
|
+
| ------ | ------ |
|
|
30
|
+
| [AssertConfig](#type-aliases/AssertConfig) | Configuration for assertion behavior: a static message string, a boolean toggle, or a callback. |
|
|
27
31
|
|
|
28
32
|
## Functions
|
|
29
33
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
| Function | Description |
|
|
35
|
+
| ------ | ------ |
|
|
36
|
+
| [assertError](#functions/assertError) | Throws an Error based on the assert configuration when a value fails validation. |
|
|
37
|
+
| [handleError](#functions/handleError) | Invokes the handler if the value is an Error, otherwise re-throws it. |
|
|
38
|
+
| [handleErrorAsync](#functions/handleErrorAsync) | Async version of handleError. Invokes the async handler if the value is an Error, otherwise re-throws it. |
|
|
39
|
+
| [isError](#functions/isError) | Type guard that checks whether a value is an Error instance. |
|
|
34
40
|
|
|
35
41
|
### functions
|
|
36
42
|
|
|
@@ -42,24 +48,20 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
|
42
48
|
|
|
43
49
|
```ts
|
|
44
50
|
function assertError(
|
|
45
|
-
value,
|
|
46
|
-
assert,
|
|
47
|
-
defaultMessage): undefined;
|
|
51
|
+
value: unknown,
|
|
52
|
+
assert: AssertConfig | undefined,
|
|
53
|
+
defaultMessage: string): undefined;
|
|
48
54
|
```
|
|
49
55
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
### value
|
|
56
|
+
Throws an Error based on the assert configuration when a value fails validation.
|
|
53
57
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
### assert
|
|
57
|
-
|
|
58
|
-
[`AssertConfig`](#../type-aliases/AssertConfig) | `undefined`
|
|
59
|
-
|
|
60
|
-
### defaultMessage
|
|
58
|
+
## Parameters
|
|
61
59
|
|
|
62
|
-
|
|
60
|
+
| Parameter | Type | Description |
|
|
61
|
+
| ------ | ------ | ------ |
|
|
62
|
+
| `value` | `unknown` | The value being validated |
|
|
63
|
+
| `assert` | [`AssertConfig`](#../type-aliases/AssertConfig) \| `undefined` | Assertion config controlling the error message |
|
|
64
|
+
| `defaultMessage` | `string` | Fallback message if no custom message is provided |
|
|
63
65
|
|
|
64
66
|
## Returns
|
|
65
67
|
|
|
@@ -72,29 +74,30 @@ function assertError(
|
|
|
72
74
|
***
|
|
73
75
|
|
|
74
76
|
```ts
|
|
75
|
-
function handleError<T>(error, handler): T;
|
|
77
|
+
function handleError<T>(error: any, handler: (error: Error) => T): T;
|
|
76
78
|
```
|
|
77
79
|
|
|
78
|
-
|
|
80
|
+
Invokes the handler if the value is an Error, otherwise re-throws it.
|
|
79
81
|
|
|
80
|
-
|
|
82
|
+
## Type Parameters
|
|
81
83
|
|
|
82
|
-
|
|
84
|
+
| Type Parameter |
|
|
85
|
+
| ------ |
|
|
86
|
+
| `T` |
|
|
83
87
|
|
|
84
88
|
## Parameters
|
|
85
89
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
`any`
|
|
89
|
-
|
|
90
|
-
### handler
|
|
91
|
-
|
|
92
|
-
(`error`) => `T`
|
|
90
|
+
| Parameter | Type | Description |
|
|
91
|
+
| ------ | ------ | ------ |
|
|
92
|
+
| `error` | `any` | The caught value to inspect |
|
|
93
|
+
| `handler` | (`error`: `Error`) => `T` | Callback invoked with the Error if it is one |
|
|
93
94
|
|
|
94
95
|
## Returns
|
|
95
96
|
|
|
96
97
|
`T`
|
|
97
98
|
|
|
99
|
+
The handler's return value
|
|
100
|
+
|
|
98
101
|
### <a id="handleErrorAsync"></a>handleErrorAsync
|
|
99
102
|
|
|
100
103
|
[**@xylabs/error**](#../README)
|
|
@@ -102,29 +105,30 @@ function handleError<T>(error, handler): T;
|
|
|
102
105
|
***
|
|
103
106
|
|
|
104
107
|
```ts
|
|
105
|
-
function handleErrorAsync<T>(error, handler): Promise<T>;
|
|
108
|
+
function handleErrorAsync<T>(error: any, handler: (error: Error) => Promise<T>): Promise<T>;
|
|
106
109
|
```
|
|
107
110
|
|
|
108
|
-
|
|
111
|
+
Async version of handleError. Invokes the async handler if the value is an Error, otherwise re-throws it.
|
|
109
112
|
|
|
110
|
-
|
|
113
|
+
## Type Parameters
|
|
111
114
|
|
|
112
|
-
|
|
115
|
+
| Type Parameter |
|
|
116
|
+
| ------ |
|
|
117
|
+
| `T` |
|
|
113
118
|
|
|
114
119
|
## Parameters
|
|
115
120
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
`any`
|
|
119
|
-
|
|
120
|
-
### handler
|
|
121
|
-
|
|
122
|
-
(`error`) => `Promise`\<`T`\>
|
|
121
|
+
| Parameter | Type | Description |
|
|
122
|
+
| ------ | ------ | ------ |
|
|
123
|
+
| `error` | `any` | The caught value to inspect |
|
|
124
|
+
| `handler` | (`error`: `Error`) => `Promise`\<`T`\> | Async callback invoked with the Error if it is one |
|
|
123
125
|
|
|
124
126
|
## Returns
|
|
125
127
|
|
|
126
128
|
`Promise`\<`T`\>
|
|
127
129
|
|
|
130
|
+
The handler's resolved return value
|
|
131
|
+
|
|
128
132
|
### <a id="isError"></a>isError
|
|
129
133
|
|
|
130
134
|
[**@xylabs/error**](#../README)
|
|
@@ -134,14 +138,16 @@ function handleErrorAsync<T>(error, handler): Promise<T>;
|
|
|
134
138
|
## Call Signature
|
|
135
139
|
|
|
136
140
|
```ts
|
|
137
|
-
function isError(value): value is Error;
|
|
141
|
+
function isError(value: unknown): value is Error;
|
|
138
142
|
```
|
|
139
143
|
|
|
140
|
-
|
|
144
|
+
Type guard that checks whether a value is an Error instance.
|
|
141
145
|
|
|
142
|
-
###
|
|
146
|
+
### Parameters
|
|
143
147
|
|
|
144
|
-
|
|
148
|
+
| Parameter | Type |
|
|
149
|
+
| ------ | ------ |
|
|
150
|
+
| `value` | `unknown` |
|
|
145
151
|
|
|
146
152
|
### Returns
|
|
147
153
|
|
|
@@ -150,20 +156,22 @@ function isError(value): value is Error;
|
|
|
150
156
|
## Call Signature
|
|
151
157
|
|
|
152
158
|
```ts
|
|
153
|
-
function isError<T>(value): value is Extract<T, Error>;
|
|
159
|
+
function isError<T>(value: T): value is Extract<T, Error>;
|
|
154
160
|
```
|
|
155
161
|
|
|
156
|
-
|
|
162
|
+
Type guard that checks whether a value is an Error instance.
|
|
157
163
|
|
|
158
|
-
###
|
|
164
|
+
### Type Parameters
|
|
159
165
|
|
|
160
|
-
|
|
166
|
+
| Type Parameter |
|
|
167
|
+
| ------ |
|
|
168
|
+
| `T` |
|
|
161
169
|
|
|
162
170
|
### Parameters
|
|
163
171
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
`T`
|
|
172
|
+
| Parameter | Type |
|
|
173
|
+
| ------ | ------ |
|
|
174
|
+
| `value` | `T` |
|
|
167
175
|
|
|
168
176
|
### Returns
|
|
169
177
|
|
|
@@ -181,6 +189,8 @@ function isError<T>(value): value is Extract<T, Error>;
|
|
|
181
189
|
type AssertConfig = string | AssertCallback | boolean;
|
|
182
190
|
```
|
|
183
191
|
|
|
192
|
+
Configuration for assertion behavior: a static message string, a boolean toggle, or a callback.
|
|
193
|
+
|
|
184
194
|
|
|
185
195
|
Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
|
|
186
196
|
|
package/dist/neutral/assert.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
type AssertCallback = (value: unknown, message: string) => string | boolean;
|
|
2
|
+
/** Configuration for assertion behavior: a static message string, a boolean toggle, or a callback. */
|
|
2
3
|
export type AssertConfig = string | AssertCallback | boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Throws an Error based on the assert configuration when a value fails validation.
|
|
6
|
+
* @param value - The value being validated
|
|
7
|
+
* @param assert - Assertion config controlling the error message
|
|
8
|
+
* @param defaultMessage - Fallback message if no custom message is provided
|
|
9
|
+
*/
|
|
3
10
|
export declare const assertError: (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => undefined;
|
|
4
11
|
export {};
|
|
5
12
|
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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"}
|
|
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,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"}
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Invokes the handler if the value is an Error, otherwise re-throws it.
|
|
3
|
+
* @param error - The caught value to inspect
|
|
4
|
+
* @param handler - Callback invoked with the Error if it is one
|
|
5
|
+
* @returns The handler's return value
|
|
6
|
+
*/
|
|
1
7
|
export declare const handleError: <T>(error: any, handler: (error: Error) => T) => T;
|
|
8
|
+
/**
|
|
9
|
+
* Async version of handleError. Invokes the async handler if the value is an Error, otherwise re-throws it.
|
|
10
|
+
* @param error - The caught value to inspect
|
|
11
|
+
* @param handler - Async callback invoked with the Error if it is one
|
|
12
|
+
* @returns The handler's resolved return value
|
|
13
|
+
*/
|
|
2
14
|
export declare const handleErrorAsync: <T>(error: any, handler: (error: Error) => Promise<T>) => Promise<T>;
|
|
3
15
|
//# sourceMappingURL=handleError.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleError.d.ts","sourceRoot":"","sources":["../../src/handleError.ts"],"names":[],"mappings":"
|
|
1
|
+
{"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 @@
|
|
|
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;
|
|
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\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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/error",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.86",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"error",
|
|
@@ -42,11 +42,11 @@
|
|
|
42
42
|
"!**/*.test.*"
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@xylabs/typeof": "~5.0.
|
|
45
|
+
"@xylabs/typeof": "~5.0.86"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@xylabs/ts-scripts-yarn3": "~7.4.
|
|
49
|
-
"@xylabs/tsconfig": "~7.4.
|
|
48
|
+
"@xylabs/ts-scripts-yarn3": "~7.4.16",
|
|
49
|
+
"@xylabs/tsconfig": "~7.4.16",
|
|
50
50
|
"typescript": "~5.9.3",
|
|
51
51
|
"vitest": "^4.0.18"
|
|
52
52
|
},
|