@bombillazo/error-x 0.1.1 → 0.2.1
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 +41 -14
- package/dist/index.cjs +512 -61
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +437 -25
- package/dist/index.d.ts +437 -25
- package/dist/index.js +512 -62
- package/dist/index.js.map +1 -1
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# error-x
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@bombillazo/error-x)
|
|
4
|
+
[](https://www.npmjs.com/package/@bombillazo/error-x)
|
|
5
|
+
[](https://github.com/bombillazo/error-x/blob/master/LICENSE)
|
|
6
|
+
|
|
3
7
|
A smart, isomorphic, and satisfying error library for TypeScript applications. Provides type-safe error handling with great DX, solving common pain points like unknown error types, lost stack traces, async error handling, and error serialization.
|
|
4
8
|
|
|
5
9
|
## Features
|
|
@@ -38,18 +42,26 @@ import { ErrorX, HandlingTargets, type HandlingTarget, type ErrorAction } from '
|
|
|
38
42
|
// Minimal usage (all parameters optional)
|
|
39
43
|
const error = new ErrorX()
|
|
40
44
|
|
|
41
|
-
// Simple
|
|
42
|
-
const error = new ErrorX(
|
|
45
|
+
// Simple string message
|
|
46
|
+
const error = new ErrorX('Database connection failed')
|
|
47
|
+
|
|
48
|
+
// String message with additional options
|
|
49
|
+
const error = new ErrorX('User authentication failed', {
|
|
50
|
+
name: 'AuthError',
|
|
51
|
+
code: 'AUTH_FAILED',
|
|
52
|
+
uiMessage: 'Please check your credentials and try again',
|
|
53
|
+
metadata: { userId: 123, loginAttempt: 3 }
|
|
54
|
+
})
|
|
43
55
|
|
|
44
|
-
//
|
|
56
|
+
// Options object (backward compatible)
|
|
45
57
|
const error = new ErrorX({
|
|
46
58
|
message: 'User authentication failed',
|
|
47
59
|
name: 'AuthError',
|
|
48
60
|
code: 'AUTH_FAILED',
|
|
49
61
|
uiMessage: 'Please check your credentials and try again',
|
|
50
62
|
cause: originalError, // Chain errors while preserving stack traces
|
|
51
|
-
metadata: {
|
|
52
|
-
userId: 123,
|
|
63
|
+
metadata: {
|
|
64
|
+
userId: 123,
|
|
53
65
|
loginAttempt: 3,
|
|
54
66
|
},
|
|
55
67
|
actions: [
|
|
@@ -58,6 +70,10 @@ const error = new ErrorX({
|
|
|
58
70
|
{ action: 'custom', payload: { type: 'analytics', event: 'auth_failed', userId: 123, category: 'errors', severity: 'high' } }
|
|
59
71
|
]
|
|
60
72
|
})
|
|
73
|
+
|
|
74
|
+
// Smart conversion from unknown errors
|
|
75
|
+
const apiError = { message: 'User not found', code: 404, statusText: 'Not Found' }
|
|
76
|
+
const error = new ErrorX(apiError)
|
|
61
77
|
```
|
|
62
78
|
|
|
63
79
|
## Documentation
|
|
@@ -73,6 +89,17 @@ For complete API documentation with detailed descriptions, examples, and type in
|
|
|
73
89
|
### Constructor
|
|
74
90
|
|
|
75
91
|
```typescript
|
|
92
|
+
// String message signature
|
|
93
|
+
new ErrorX(message: string, options?: {
|
|
94
|
+
name?: string // Optional: Error type
|
|
95
|
+
code?: string | number // Optional: Error code (auto-generated from name if not provided)
|
|
96
|
+
uiMessage?: string // Optional: User-friendly message
|
|
97
|
+
cause?: Error | unknown // Optional: Original error that caused this (preserves stack traces)
|
|
98
|
+
metadata?: Record<string, any> // Optional: Additional context data
|
|
99
|
+
actions?: ErrorAction[] // Optional: Configuration for application actions to perform when error occurs
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
// Options object signature (backward compatible)
|
|
76
103
|
new ErrorX(options?: {
|
|
77
104
|
name?: string // Optional: Error type
|
|
78
105
|
message?: string // Optional: Technical error message (default: 'An error occurred')
|
|
@@ -80,8 +107,11 @@ new ErrorX(options?: {
|
|
|
80
107
|
uiMessage?: string // Optional: User-friendly message
|
|
81
108
|
cause?: Error | unknown // Optional: Original error that caused this (preserves stack traces)
|
|
82
109
|
metadata?: Record<string, any> // Optional: Additional context data
|
|
83
|
-
actions?: ErrorAction[] // Optional: Configuration for application actions to perform when error occurs
|
|
110
|
+
actions?: ErrorAction[] // Optional: Configuration for application actions to perform when error occurs
|
|
84
111
|
})
|
|
112
|
+
|
|
113
|
+
// Smart conversion signature (converts any unknown input)
|
|
114
|
+
new ErrorX(input: unknown)
|
|
85
115
|
```
|
|
86
116
|
|
|
87
117
|
**All parameters are optional** - ErrorX uses sensible defaults and auto-generates missing values.
|
|
@@ -244,8 +274,7 @@ import { ErrorX } from 'error-x'
|
|
|
244
274
|
|
|
245
275
|
function validateUser(user: unknown) {
|
|
246
276
|
if (!user) {
|
|
247
|
-
throw new ErrorX({
|
|
248
|
-
message: 'User validation failed: user is required',
|
|
277
|
+
throw new ErrorX('User validation failed: user is required', {
|
|
249
278
|
name: 'ValidationError',
|
|
250
279
|
code: 'USER_REQUIRED',
|
|
251
280
|
uiMessage: 'Please provide user information',
|
|
@@ -262,8 +291,7 @@ async function fetchUser(id: string) {
|
|
|
262
291
|
try {
|
|
263
292
|
const response = await fetch(`/api/users/${id}`)
|
|
264
293
|
if (!response.ok) {
|
|
265
|
-
throw new ErrorX({
|
|
266
|
-
message: `Failed to fetch user: ${response.statusText}`,
|
|
294
|
+
throw new ErrorX(`Failed to fetch user: ${response.statusText}`, {
|
|
267
295
|
code: `HTTP_${response.status}`,
|
|
268
296
|
uiMessage: 'Unable to load user data',
|
|
269
297
|
metadata: { status: response.status, statusText: response.statusText }
|
|
@@ -272,7 +300,7 @@ async function fetchUser(id: string) {
|
|
|
272
300
|
return response.json()
|
|
273
301
|
} catch (error) {
|
|
274
302
|
// Convert any error to ErrorX and add context
|
|
275
|
-
const errorX = ErrorX
|
|
303
|
+
const errorX = new ErrorX(error)
|
|
276
304
|
throw errorX.withMetadata({
|
|
277
305
|
userId: id,
|
|
278
306
|
operation: 'fetchUser',
|
|
@@ -290,8 +318,7 @@ try {
|
|
|
290
318
|
})
|
|
291
319
|
} catch (dbError) {
|
|
292
320
|
// Create new ErrorX while preserving the original error in the cause chain
|
|
293
|
-
const error = new ErrorX({
|
|
294
|
-
message: 'User creation failed',
|
|
321
|
+
const error = new ErrorX('User creation failed', {
|
|
295
322
|
name: 'UserCreationError',
|
|
296
323
|
code: 'USER_CREATE_FAILED',
|
|
297
324
|
uiMessage: 'Unable to create user account',
|
|
@@ -301,7 +328,7 @@ try {
|
|
|
301
328
|
userData: { email: userData.email } // Don't log sensitive data
|
|
302
329
|
}
|
|
303
330
|
})
|
|
304
|
-
|
|
331
|
+
|
|
305
332
|
// Add more context while preserving the error chain
|
|
306
333
|
throw error.withMetadata({
|
|
307
334
|
requestId: generateRequestId(),
|