@bombillazo/error-x 0.4.4 → 0.4.6
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 +11 -128
- package/dist/index.cjs +44 -136
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +122 -100
- package/dist/index.d.ts +122 -100
- package/dist/index.js +44 -136
- package/dist/index.js.map +1 -1
- package/package.json +47 -42
package/README.md
CHANGED
|
@@ -42,6 +42,10 @@ This library uses modern JavaScript features and ES2022 APIs. For browser compat
|
|
|
42
42
|
>
|
|
43
43
|
> Once we reach version 1.0, we plan to minimize API changes and follow semantic versioning.
|
|
44
44
|
|
|
45
|
+
## Documentation
|
|
46
|
+
|
|
47
|
+
[Documentation](docs/index.md)
|
|
48
|
+
|
|
45
49
|
## Quick Start
|
|
46
50
|
|
|
47
51
|
```typescript
|
|
@@ -57,9 +61,7 @@ throw new ErrorX({
|
|
|
57
61
|
code: 'AUTH_FAILED',
|
|
58
62
|
uiMessage: 'Please check your credentials and try again',
|
|
59
63
|
metadata: { userId: 123, loginAttempt: 3 },
|
|
60
|
-
|
|
61
|
-
source: 'auth-service',
|
|
62
|
-
httpStatus: 401
|
|
64
|
+
source: 'auth-service'
|
|
63
65
|
})
|
|
64
66
|
|
|
65
67
|
// Using HTTP presets
|
|
@@ -97,11 +99,9 @@ All parameters are optional. ErrorX uses sensible defaults:
|
|
|
97
99
|
| name | `string` | `'Error'` | Error type/title |
|
|
98
100
|
| code | `string \| number` | Auto-generated from name or `'ERROR'` | Error identifier (auto-generated from name in UPPER_SNAKE_CASE) |
|
|
99
101
|
| uiMessage | `string \| undefined` | `undefined` | User-friendly message for display |
|
|
100
|
-
| cause | `Error \| unknown`
|
|
101
|
-
| metadata | `Record<string, unknown> \| undefined` | `undefined` | Additional context and data
|
|
102
|
-
| httpStatus | `number \| undefined` | `undefined` | HTTP status code (100-599) for HTTP-related errors |
|
|
102
|
+
| cause | `ErrorXCause \| Error \| unknown` | `undefined` | Original error that caused this (preserves full error chain) |
|
|
103
|
+
| metadata | `Record<string, unknown> \| undefined` | `undefined` | Additional context and data (flexible storage for any extra info) |
|
|
103
104
|
| type | `string \| undefined` | `undefined` | Error type for categorization (e.g., 'http', 'validation') |
|
|
104
|
-
| sourceUrl | `string \| undefined` | `undefined` | URL related to the error (API endpoint, page URL, resource URL) |
|
|
105
105
|
| docsUrl | `string \| undefined` | `undefined` or auto-generated | Documentation URL for this specific error |
|
|
106
106
|
| source | `string \| undefined` | `undefined` or from config | Where the error originated (service name, module, component) |
|
|
107
107
|
| timestamp | `number` | `Date.now()` | Unix epoch timestamp in milliseconds when error was created |
|
|
@@ -140,19 +140,12 @@ try {
|
|
|
140
140
|
#### Available Presets
|
|
141
141
|
|
|
142
142
|
All presets are indexed by **HTTP status code** (numeric keys) and include:
|
|
143
|
-
|
|
143
|
+
|
|
144
144
|
- `code`: Error code in UPPER_SNAKE_CASE
|
|
145
145
|
- `name`: Descriptive error name
|
|
146
146
|
- `message`: Technical message with proper sentence casing and period
|
|
147
147
|
- `uiMessage`: User-friendly message
|
|
148
|
-
- `
|
|
149
|
-
|
|
150
|
-
**4xx Client Errors:**
|
|
151
|
-
|
|
152
|
-
`400`, `401`, `402`, `403`, `404`, `405`, `406`, `407`, `408`, `409`, `410`, `411`, `412`, `413`, `414`, `415`, `416`, `417`, `418`, `422`, `423`, `424`, `425`, `426`, `428`, `429`, `431`, `451`
|
|
153
|
-
|
|
154
|
-
**5xx Server Errors:**
|
|
155
|
-
`500`, `501`, `502`, `503`, `504`, `505`, `506`, `507`, `508`, `510`, `511`
|
|
148
|
+
- `metadata`: Contains `{ status: <number> }` with the HTTP status code
|
|
156
149
|
|
|
157
150
|
#### Creating Your Own Presets
|
|
158
151
|
|
|
@@ -185,7 +178,7 @@ export const authErrors = {
|
|
|
185
178
|
code: 'AUTH_INVALID_TOKEN',
|
|
186
179
|
message: 'Invalid authentication token.',
|
|
187
180
|
uiMessage: 'Your session has expired. Please log in again.',
|
|
188
|
-
|
|
181
|
+
metadata: { status: 401 },
|
|
189
182
|
type: 'authentication',
|
|
190
183
|
},
|
|
191
184
|
insufficientPermissions: {
|
|
@@ -193,7 +186,7 @@ export const authErrors = {
|
|
|
193
186
|
code: 'AUTH_INSUFFICIENT_PERMISSIONS',
|
|
194
187
|
message: 'Insufficient permissions.',
|
|
195
188
|
uiMessage: 'You do not have permission to perform this action.',
|
|
196
|
-
|
|
189
|
+
metadata: { status: 403 },
|
|
197
190
|
type: 'authorization',
|
|
198
191
|
},
|
|
199
192
|
} satisfies Record<string, ErrorXOptions>;
|
|
@@ -205,116 +198,6 @@ throw new ErrorX({ ...authErrors.invalidToken, metadata: { userId: 123 } });
|
|
|
205
198
|
|
|
206
199
|
This approach keeps your error handling consistent while remaining flexible for your specific domain.
|
|
207
200
|
|
|
208
|
-
### Static Methods
|
|
209
|
-
|
|
210
|
-
#### `ErrorX.from(error: unknown): ErrorX`
|
|
211
|
-
|
|
212
|
-
Converts any error type to ErrorX with intelligent property extraction:
|
|
213
|
-
|
|
214
|
-
```typescript
|
|
215
|
-
// Convert Error instances
|
|
216
|
-
const error = new Error('Something failed')
|
|
217
|
-
const errorX = ErrorX.from(error)
|
|
218
|
-
// Preserves: name, message, cause, stack
|
|
219
|
-
|
|
220
|
-
// Convert API responses
|
|
221
|
-
const apiError = { status: 404, statusText: 'Not Found', error: 'User not found' }
|
|
222
|
-
const errorX = ErrorX.from(apiError)
|
|
223
|
-
// Extracts: message, httpStatus, stores original in metadata
|
|
224
|
-
|
|
225
|
-
// Convert strings
|
|
226
|
-
const errorX = ErrorX.from('Something went wrong')
|
|
227
|
-
// Creates ErrorX with string as message
|
|
228
|
-
|
|
229
|
-
// Already ErrorX? Returns as-is
|
|
230
|
-
const errorX = ErrorX.from(new ErrorX('test'))
|
|
231
|
-
// Returns the same instance
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
#### `ErrorX.isErrorX(value: unknown): value is ErrorX`
|
|
235
|
-
|
|
236
|
-
Type guard to check if a value is an ErrorX instance:
|
|
237
|
-
|
|
238
|
-
```typescript
|
|
239
|
-
if (ErrorX.isErrorX(error)) {
|
|
240
|
-
console.log(error.code, error.uiMessage)
|
|
241
|
-
}
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
#### `ErrorX.configure(config: ErrorXConfig): void`
|
|
245
|
-
|
|
246
|
-
Set global defaults for all ErrorX instances:
|
|
247
|
-
|
|
248
|
-
```typescript
|
|
249
|
-
ErrorX.configure({
|
|
250
|
-
source: 'my-api-service',
|
|
251
|
-
docsBaseURL: 'https://docs.example.com',
|
|
252
|
-
docsMap: {
|
|
253
|
-
'AUTH_FAILED': 'errors/authentication',
|
|
254
|
-
'NOT_FOUND': 'errors/not-found'
|
|
255
|
-
}
|
|
256
|
-
})
|
|
257
|
-
|
|
258
|
-
// Now all errors automatically get:
|
|
259
|
-
// - source: 'my-api-service' (unless overridden)
|
|
260
|
-
// - docsUrl: auto-generated from docsBaseURL + docsMap[code]
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
#### `ErrorX.getConfig(): ErrorXConfig | null`
|
|
264
|
-
|
|
265
|
-
Get the current global configuration:
|
|
266
|
-
|
|
267
|
-
```typescript
|
|
268
|
-
const config = ErrorX.getConfig()
|
|
269
|
-
console.log(config?.source) // 'my-api-service'
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
### Instance Methods
|
|
273
|
-
|
|
274
|
-
#### `withMetadata(additionalMetadata: Record<string, unknown>): ErrorX`
|
|
275
|
-
|
|
276
|
-
Creates a new ErrorX instance with merged metadata:
|
|
277
|
-
|
|
278
|
-
```typescript
|
|
279
|
-
const error = new ErrorX({ message: 'test', metadata: { a: 1 } })
|
|
280
|
-
const enriched = error.withMetadata({ b: 2 })
|
|
281
|
-
// enriched.metadata = { a: 1, b: 2 }
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
#### `ErrorX.cleanStack(stack?: string, delimiter?: string): string`
|
|
285
|
-
|
|
286
|
-
Cleans a stack trace by removing ErrorX internal calls and optionally trimming after a delimiter:
|
|
287
|
-
|
|
288
|
-
```typescript
|
|
289
|
-
const error = new ErrorX('test')
|
|
290
|
-
|
|
291
|
-
// Clean with pattern-based removal only
|
|
292
|
-
const cleaned = ErrorX.cleanStack(error.stack)
|
|
293
|
-
|
|
294
|
-
// Clean and trim after delimiter
|
|
295
|
-
const trimmed = ErrorX.cleanStack(error.stack, 'my-app-boundary')
|
|
296
|
-
// Returns stack trace starting after the line containing 'my-app-boundary'
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
#### `toJSON(): ErrorXSerialized`
|
|
300
|
-
|
|
301
|
-
Serializes the error for network transfer:
|
|
302
|
-
|
|
303
|
-
```typescript
|
|
304
|
-
const error = new ErrorX({ message: 'test', code: 'TEST' })
|
|
305
|
-
const json = error.toJSON()
|
|
306
|
-
// Returns plain object with all error properties
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
#### `fromJSON(serialized: ErrorXSerialized): ErrorX`
|
|
310
|
-
|
|
311
|
-
Deserializes a JSON object back to ErrorX:
|
|
312
|
-
|
|
313
|
-
```typescript
|
|
314
|
-
const json = { name: 'TestError', message: 'test', code: 'TEST', ... }
|
|
315
|
-
const error = ErrorX.fromJSON(json)
|
|
316
|
-
// Returns fully reconstructed ErrorX instance
|
|
317
|
-
```
|
|
318
201
|
|
|
319
202
|
## Usage Examples
|
|
320
203
|
|