@beethovn/errors 1.0.0
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/LICENSE +21 -0
- package/README.md +375 -0
- package/dist/auth/AuthError.d.ts +95 -0
- package/dist/auth/AuthError.d.ts.map +1 -0
- package/dist/auth/AuthError.js +109 -0
- package/dist/auth/AuthError.js.map +1 -0
- package/dist/base/BeethovnError.d.ts +51 -0
- package/dist/base/BeethovnError.d.ts.map +1 -0
- package/dist/base/BeethovnError.js +79 -0
- package/dist/base/BeethovnError.js.map +1 -0
- package/dist/domain/DomainError.d.ts +88 -0
- package/dist/domain/DomainError.d.ts.map +1 -0
- package/dist/domain/DomainError.js +100 -0
- package/dist/domain/DomainError.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/infrastructure/InfrastructureError.d.ts +98 -0
- package/dist/infrastructure/InfrastructureError.d.ts.map +1 -0
- package/dist/infrastructure/InfrastructureError.js +144 -0
- package/dist/infrastructure/InfrastructureError.js.map +1 -0
- package/dist/integration/IntegrationError.d.ts +71 -0
- package/dist/integration/IntegrationError.d.ts.map +1 -0
- package/dist/integration/IntegrationError.js +111 -0
- package/dist/integration/IntegrationError.js.map +1 -0
- package/dist/utils/errorFactory.d.ts +96 -0
- package/dist/utils/errorFactory.d.ts.map +1 -0
- package/dist/utils/errorFactory.js +116 -0
- package/dist/utils/errorFactory.js.map +1 -0
- package/dist/utils/errorGuards.d.ts +133 -0
- package/dist/utils/errorGuards.d.ts.map +1 -0
- package/dist/utils/errorGuards.js +152 -0
- package/dist/utils/errorGuards.js.map +1 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Beethovn Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
# @beethovn/errors
|
|
2
|
+
|
|
3
|
+
Standardized error types for the Beethovn platform. Provides structured, typed errors with rich metadata for better debugging, logging, and monitoring.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @beethovn/errors
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { ValidationError, ErrorFactory, isDomainError } from '@beethovn/errors';
|
|
15
|
+
|
|
16
|
+
// Throw typed errors
|
|
17
|
+
if (!email.includes('@')) {
|
|
18
|
+
throw new ValidationError('Invalid email format', 'email');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Handle unknown errors
|
|
22
|
+
try {
|
|
23
|
+
await riskyOperation();
|
|
24
|
+
} catch (error: unknown) {
|
|
25
|
+
const beethovnError = ErrorFactory.fromUnknown(error, 'riskyOperation');
|
|
26
|
+
logger.error('Operation failed', beethovnError.toJSON());
|
|
27
|
+
|
|
28
|
+
if (isDomainError(error)) {
|
|
29
|
+
// User-fixable error
|
|
30
|
+
return { error: beethovnError.toClient() };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Error Hierarchy
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
BeethovnError (base)
|
|
39
|
+
├── DomainError (business logic)
|
|
40
|
+
│ ├── ValidationError
|
|
41
|
+
│ ├── BusinessRuleError
|
|
42
|
+
│ ├── NotFoundError
|
|
43
|
+
│ ├── ConflictError
|
|
44
|
+
│ └── InvalidStateError
|
|
45
|
+
├── InfrastructureError (technical)
|
|
46
|
+
│ ├── DatabaseError
|
|
47
|
+
│ ├── NetworkError
|
|
48
|
+
│ ├── ExternalServiceError
|
|
49
|
+
│ ├── TimeoutError
|
|
50
|
+
│ ├── RateLimitError
|
|
51
|
+
│ └── UnknownError
|
|
52
|
+
├── AuthError (authentication/authorization)
|
|
53
|
+
│ ├── InvalidCredentialsError
|
|
54
|
+
│ ├── TokenExpiredError
|
|
55
|
+
│ ├── InvalidTokenError
|
|
56
|
+
│ ├── PermissionDeniedError
|
|
57
|
+
│ ├── ForbiddenError
|
|
58
|
+
│ └── UnauthenticatedError
|
|
59
|
+
└── IntegrationError (inter-service communication)
|
|
60
|
+
├── EventPublishError
|
|
61
|
+
├── MessageQueueError
|
|
62
|
+
├── WebSocketError
|
|
63
|
+
└── EventSubscriptionError
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Usage Examples
|
|
67
|
+
|
|
68
|
+
### Domain Errors
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import {
|
|
72
|
+
ValidationError,
|
|
73
|
+
BusinessRuleError,
|
|
74
|
+
NotFoundError,
|
|
75
|
+
ConflictError,
|
|
76
|
+
} from '@beethovn/errors';
|
|
77
|
+
|
|
78
|
+
// Validation error
|
|
79
|
+
if (!email.includes('@')) {
|
|
80
|
+
throw new ValidationError('Invalid email format', 'email', {
|
|
81
|
+
value: email,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Business rule violation
|
|
86
|
+
if (balance < amount) {
|
|
87
|
+
throw new BusinessRuleError(
|
|
88
|
+
'Insufficient balance',
|
|
89
|
+
'MINIMUM_BALANCE',
|
|
90
|
+
{ balance, required: amount }
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Not found
|
|
95
|
+
const user = await db.getUser(id);
|
|
96
|
+
if (!user) {
|
|
97
|
+
throw new NotFoundError('User', id, { tenantId });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Conflict
|
|
101
|
+
if (existingUser) {
|
|
102
|
+
throw new ConflictError(
|
|
103
|
+
'User with this email already exists',
|
|
104
|
+
{ email }
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Infrastructure Errors
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import {
|
|
113
|
+
DatabaseError,
|
|
114
|
+
NetworkError,
|
|
115
|
+
ExternalServiceError,
|
|
116
|
+
TimeoutError,
|
|
117
|
+
} from '@beethovn/errors';
|
|
118
|
+
|
|
119
|
+
// Database error
|
|
120
|
+
try {
|
|
121
|
+
await db.query(sql, params);
|
|
122
|
+
} catch (error: unknown) {
|
|
123
|
+
throw new DatabaseError('saveUser', error, { userId });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Network error
|
|
127
|
+
try {
|
|
128
|
+
const response = await fetch(url);
|
|
129
|
+
} catch (error: unknown) {
|
|
130
|
+
throw new NetworkError('Failed to fetch user data', { url, method: 'GET' });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// External service error
|
|
134
|
+
try {
|
|
135
|
+
await stripeClient.charge(amount);
|
|
136
|
+
} catch (error: unknown) {
|
|
137
|
+
throw new ExternalServiceError(
|
|
138
|
+
'Stripe',
|
|
139
|
+
'createCharge',
|
|
140
|
+
error,
|
|
141
|
+
{ amount, currency }
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Timeout error
|
|
146
|
+
throw new TimeoutError('Database query', 5000, { query: sql });
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Auth Errors
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import {
|
|
153
|
+
InvalidCredentialsError,
|
|
154
|
+
TokenExpiredError,
|
|
155
|
+
PermissionDeniedError,
|
|
156
|
+
} from '@beethovn/errors';
|
|
157
|
+
|
|
158
|
+
// Invalid credentials
|
|
159
|
+
const user = await verifyCredentials(email, password);
|
|
160
|
+
if (!user) {
|
|
161
|
+
throw new InvalidCredentialsError({ email });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Token expired
|
|
165
|
+
if (token.exp < Date.now()) {
|
|
166
|
+
throw new TokenExpiredError({ userId: token.sub });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Permission denied
|
|
170
|
+
if (!user.permissions.includes('delete:product')) {
|
|
171
|
+
throw new PermissionDeniedError('Product', 'delete', {
|
|
172
|
+
userId: user.id,
|
|
173
|
+
requiredPermission: 'delete:product',
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Error Factory
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { ErrorFactory } from '@beethovn/errors';
|
|
182
|
+
|
|
183
|
+
// Convert unknown errors
|
|
184
|
+
try {
|
|
185
|
+
await operation();
|
|
186
|
+
} catch (error: unknown) {
|
|
187
|
+
throw ErrorFactory.fromUnknown(error, 'operationContext');
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Create database error
|
|
191
|
+
throw ErrorFactory.database('saveUser', error, { userId });
|
|
192
|
+
|
|
193
|
+
// Create validation error
|
|
194
|
+
throw ErrorFactory.validation('Invalid email', 'email');
|
|
195
|
+
|
|
196
|
+
// Aggregate multiple errors
|
|
197
|
+
const errors = [
|
|
198
|
+
new ValidationError('Invalid email', 'email'),
|
|
199
|
+
new ValidationError('Invalid age', 'age'),
|
|
200
|
+
];
|
|
201
|
+
throw ErrorFactory.aggregate(errors, 'User validation');
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Type Guards
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
import {
|
|
208
|
+
isBeethovnError,
|
|
209
|
+
isDomainError,
|
|
210
|
+
isInfrastructureError,
|
|
211
|
+
isRecoverableError,
|
|
212
|
+
isClientSafeError,
|
|
213
|
+
} from '@beethovn/errors';
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
await operation();
|
|
217
|
+
} catch (error: unknown) {
|
|
218
|
+
if (isBeethovnError(error)) {
|
|
219
|
+
logger.error('Beethovn error', error.toJSON());
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (isDomainError(error)) {
|
|
223
|
+
// User-fixable error - return helpful message
|
|
224
|
+
return { error: error.toClient() };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (isInfrastructureError(error) && error.recoverable) {
|
|
228
|
+
// Retry recoverable infrastructure errors
|
|
229
|
+
await retryWithBackoff(operation);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (isClientSafeError(error)) {
|
|
233
|
+
// Safe to send to client
|
|
234
|
+
res.status(error.statusCode).json(error.toClient());
|
|
235
|
+
} else {
|
|
236
|
+
// Internal error - don't expose details
|
|
237
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Error Properties
|
|
243
|
+
|
|
244
|
+
All errors extend `BeethovnError` and include:
|
|
245
|
+
|
|
246
|
+
- `code` - Unique error code (e.g., 'VALIDATION_ERROR')
|
|
247
|
+
- `statusCode` - HTTP status code (e.g., 400, 404, 500)
|
|
248
|
+
- `message` - Human-readable error message
|
|
249
|
+
- `metadata` - Additional context for debugging
|
|
250
|
+
- `timestamp` - When the error occurred
|
|
251
|
+
- `recoverable` - Whether the error can be retried
|
|
252
|
+
- `stack` - Stack trace for debugging
|
|
253
|
+
|
|
254
|
+
## Error Methods
|
|
255
|
+
|
|
256
|
+
### `toJSON()`
|
|
257
|
+
|
|
258
|
+
Serialize error for logging and monitoring (includes all details):
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
const error = new ValidationError('Invalid email', 'email');
|
|
262
|
+
logger.error('Validation failed', error.toJSON());
|
|
263
|
+
// {
|
|
264
|
+
// name: 'ValidationError',
|
|
265
|
+
// code: 'VALIDATION_ERROR',
|
|
266
|
+
// message: 'Invalid email',
|
|
267
|
+
// statusCode: 400,
|
|
268
|
+
// metadata: { field: 'email' },
|
|
269
|
+
// timestamp: '2025-01-03T10:00:00.000Z',
|
|
270
|
+
// recoverable: false,
|
|
271
|
+
// stack: '...'
|
|
272
|
+
// }
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### `toClient()`
|
|
276
|
+
|
|
277
|
+
Sanitized error for API responses (excludes stack trace and sensitive data):
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
const error = new ValidationError('Invalid email', 'email');
|
|
281
|
+
res.status(error.statusCode).json(error.toClient());
|
|
282
|
+
// {
|
|
283
|
+
// code: 'VALIDATION_ERROR',
|
|
284
|
+
// message: 'Invalid email',
|
|
285
|
+
// statusCode: 400,
|
|
286
|
+
// timestamp: '2025-01-03T10:00:00.000Z'
|
|
287
|
+
// }
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
## Integration with DbResult
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
import { DbResult } from '@beethovn/types';
|
|
294
|
+
import { NotFoundError, ErrorFactory } from '@beethovn/errors';
|
|
295
|
+
|
|
296
|
+
async function getUser(id: string): Promise<DbResult<User>> {
|
|
297
|
+
try {
|
|
298
|
+
const user = await db.query('SELECT * FROM users WHERE id = $1', [id]);
|
|
299
|
+
|
|
300
|
+
if (!user) {
|
|
301
|
+
return {
|
|
302
|
+
data: null,
|
|
303
|
+
error: new NotFoundError('User', id),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return { data: user, error: null };
|
|
308
|
+
} catch (error: unknown) {
|
|
309
|
+
return {
|
|
310
|
+
data: null,
|
|
311
|
+
error: ErrorFactory.database('getUser', error, { userId: id }),
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## Best Practices
|
|
318
|
+
|
|
319
|
+
1. **Always use typed errors** - Never throw generic `Error`
|
|
320
|
+
2. **Include context** - Add relevant metadata for debugging
|
|
321
|
+
3. **Use ErrorFactory.fromUnknown** - For handling unknown errors
|
|
322
|
+
4. **Check recoverability** - Retry recoverable errors
|
|
323
|
+
5. **Sanitize client responses** - Use `toClient()` for API responses
|
|
324
|
+
6. **Log with toJSON()** - Include full error details in logs
|
|
325
|
+
7. **Use type guards** - Handle different error types appropriately
|
|
326
|
+
|
|
327
|
+
## Error Decision Tree
|
|
328
|
+
|
|
329
|
+
```
|
|
330
|
+
Is this a business logic issue?
|
|
331
|
+
├─ YES → Use DomainError
|
|
332
|
+
│ ├─ Invalid input? → ValidationError
|
|
333
|
+
│ ├─ Rule violation? → BusinessRuleError
|
|
334
|
+
│ ├─ Not found? → NotFoundError
|
|
335
|
+
│ └─ Already exists? → ConflictError
|
|
336
|
+
│
|
|
337
|
+
└─ NO → Is it a technical issue?
|
|
338
|
+
├─ YES → Use InfrastructureError
|
|
339
|
+
│ ├─ Database? → DatabaseError
|
|
340
|
+
│ ├─ Network? → NetworkError
|
|
341
|
+
│ └─ External service? → ExternalServiceError
|
|
342
|
+
│
|
|
343
|
+
└─ NO → Is it auth/authz?
|
|
344
|
+
├─ YES → Use AuthError
|
|
345
|
+
└─ NO → Use ErrorFactory.fromUnknown()
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
## TypeScript Support
|
|
349
|
+
|
|
350
|
+
Full TypeScript support with type inference:
|
|
351
|
+
|
|
352
|
+
```typescript
|
|
353
|
+
import { BeethovnError, ValidationError } from '@beethovn/errors';
|
|
354
|
+
|
|
355
|
+
function handleError(error: BeethovnError) {
|
|
356
|
+
// TypeScript knows all properties
|
|
357
|
+
console.log(error.code); // ✓ string
|
|
358
|
+
console.log(error.statusCode); // ✓ number
|
|
359
|
+
console.log(error.metadata); // ✓ Record<string, unknown>
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Type narrowing with type guards
|
|
363
|
+
if (error instanceof ValidationError) {
|
|
364
|
+
// TypeScript knows this is ValidationError
|
|
365
|
+
console.log(error.metadata.field); // ✓ unknown
|
|
366
|
+
}
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
## License
|
|
370
|
+
|
|
371
|
+
MIT
|
|
372
|
+
|
|
373
|
+
## Contributing
|
|
374
|
+
|
|
375
|
+
See [CONTRIBUTING.md](../../CONTRIBUTING.md)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { BeethovnError } from '../base/BeethovnError.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base class for authentication and authorization errors
|
|
4
|
+
*
|
|
5
|
+
* Auth errors represent issues with authentication (identity verification)
|
|
6
|
+
* and authorization (permission checks). These are typically not recoverable.
|
|
7
|
+
*/
|
|
8
|
+
export declare abstract class AuthError extends BeethovnError {
|
|
9
|
+
constructor(message: string, code: string, statusCode?: number, metadata?: Record<string, unknown>);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Invalid credentials error
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const user = await verifyCredentials(email, password);
|
|
17
|
+
* if (!user) {
|
|
18
|
+
* throw new InvalidCredentialsError({ email });
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare class InvalidCredentialsError extends AuthError {
|
|
23
|
+
constructor(metadata?: Record<string, unknown>);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Token expired error
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* if (token.exp < Date.now()) {
|
|
31
|
+
* throw new TokenExpiredError({ userId: token.sub });
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class TokenExpiredError extends AuthError {
|
|
36
|
+
constructor(metadata?: Record<string, unknown>);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Invalid token error
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* try {
|
|
44
|
+
* verifyToken(token);
|
|
45
|
+
* } catch {
|
|
46
|
+
* throw new InvalidTokenError({ token: token.substring(0, 10) + '...' });
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare class InvalidTokenError extends AuthError {
|
|
51
|
+
constructor(metadata?: Record<string, unknown>);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Permission denied error
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* if (!user.permissions.includes('delete:product')) {
|
|
59
|
+
* throw new PermissionDeniedError('Product', 'delete', {
|
|
60
|
+
* userId: user.id,
|
|
61
|
+
* requiredPermission: 'delete:product',
|
|
62
|
+
* });
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare class PermissionDeniedError extends AuthError {
|
|
67
|
+
constructor(resource: string, action: string, metadata?: Record<string, unknown>);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Forbidden error - authenticated but not authorized
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* if (resource.tenantId !== user.tenantId) {
|
|
75
|
+
* throw new ForbiddenError('Cannot access resource from different tenant');
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare class ForbiddenError extends AuthError {
|
|
80
|
+
constructor(message?: string, metadata?: Record<string, unknown>);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Unauthenticated error - no valid authentication provided
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* if (!req.headers.authorization) {
|
|
88
|
+
* throw new UnauthenticatedError('No authentication token provided');
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare class UnauthenticatedError extends AuthError {
|
|
93
|
+
constructor(message?: string, metadata?: Record<string, unknown>);
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=AuthError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthError.d.ts","sourceRoot":"","sources":["../../src/auth/AuthError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD;;;;;GAKG;AACH,8BAAsB,SAAU,SAAQ,aAAa;gBAEjD,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,UAAU,GAAE,MAAY,EACxB,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM;CAIzC;AAED;;;;;;;;;;GAUG;AACH,qBAAa,uBAAwB,SAAQ,SAAS;gBACxC,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM;CAGnD;AAED;;;;;;;;;GASG;AACH,qBAAa,iBAAkB,SAAQ,SAAS;gBAClC,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM;CAGnD;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,iBAAkB,SAAQ,SAAS;gBAClC,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM;CAGnD;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,qBAAsB,SAAQ,SAAS;gBAEhD,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM;CASzC;AAED;;;;;;;;;GASG;AACH,qBAAa,cAAe,SAAQ,SAAS;gBAEzC,OAAO,GAAE,MAA2B,EACpC,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM;CAIzC;AAED;;;;;;;;;GASG;AACH,qBAAa,oBAAqB,SAAQ,SAAS;gBAE/C,OAAO,GAAE,MAAkC,EAC3C,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM;CAIzC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { BeethovnError } from '../base/BeethovnError.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base class for authentication and authorization errors
|
|
4
|
+
*
|
|
5
|
+
* Auth errors represent issues with authentication (identity verification)
|
|
6
|
+
* and authorization (permission checks). These are typically not recoverable.
|
|
7
|
+
*/
|
|
8
|
+
export class AuthError extends BeethovnError {
|
|
9
|
+
constructor(message, code, statusCode = 401, metadata = {}) {
|
|
10
|
+
super(message, code, statusCode, metadata, false);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Invalid credentials error
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const user = await verifyCredentials(email, password);
|
|
19
|
+
* if (!user) {
|
|
20
|
+
* throw new InvalidCredentialsError({ email });
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export class InvalidCredentialsError extends AuthError {
|
|
25
|
+
constructor(metadata = {}) {
|
|
26
|
+
super('Invalid credentials', 'INVALID_CREDENTIALS', 401, metadata);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Token expired error
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* if (token.exp < Date.now()) {
|
|
35
|
+
* throw new TokenExpiredError({ userId: token.sub });
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export class TokenExpiredError extends AuthError {
|
|
40
|
+
constructor(metadata = {}) {
|
|
41
|
+
super('Token has expired', 'TOKEN_EXPIRED', 401, metadata);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Invalid token error
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* try {
|
|
50
|
+
* verifyToken(token);
|
|
51
|
+
* } catch {
|
|
52
|
+
* throw new InvalidTokenError({ token: token.substring(0, 10) + '...' });
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export class InvalidTokenError extends AuthError {
|
|
57
|
+
constructor(metadata = {}) {
|
|
58
|
+
super('Invalid or malformed token', 'INVALID_TOKEN', 401, metadata);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Permission denied error
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* if (!user.permissions.includes('delete:product')) {
|
|
67
|
+
* throw new PermissionDeniedError('Product', 'delete', {
|
|
68
|
+
* userId: user.id,
|
|
69
|
+
* requiredPermission: 'delete:product',
|
|
70
|
+
* });
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export class PermissionDeniedError extends AuthError {
|
|
75
|
+
constructor(resource, action, metadata = {}) {
|
|
76
|
+
super(`Permission denied: Cannot ${action} ${resource}`, 'PERMISSION_DENIED', 403, { ...metadata, resource, action });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Forbidden error - authenticated but not authorized
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* if (resource.tenantId !== user.tenantId) {
|
|
85
|
+
* throw new ForbiddenError('Cannot access resource from different tenant');
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export class ForbiddenError extends AuthError {
|
|
90
|
+
constructor(message = 'Access forbidden', metadata = {}) {
|
|
91
|
+
super(message, 'FORBIDDEN', 403, metadata);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Unauthenticated error - no valid authentication provided
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* if (!req.headers.authorization) {
|
|
100
|
+
* throw new UnauthenticatedError('No authentication token provided');
|
|
101
|
+
* }
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export class UnauthenticatedError extends AuthError {
|
|
105
|
+
constructor(message = 'Authentication required', metadata = {}) {
|
|
106
|
+
super(message, 'UNAUTHENTICATED', 401, metadata);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=AuthError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthError.js","sourceRoot":"","sources":["../../src/auth/AuthError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD;;;;;GAKG;AACH,MAAM,OAAgB,SAAU,SAAQ,aAAa;IACnD,YACE,OAAe,EACf,IAAY,EACZ,aAAqB,GAAG,EACxB,WAAoC,EAAE;QAEtC,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;CACF;AAED;;;;;;;;;;GAUG;AACH,MAAM,OAAO,uBAAwB,SAAQ,SAAS;IACpD,YAAY,WAAoC,EAAE;QAChD,KAAK,CAAC,qBAAqB,EAAE,qBAAqB,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IACrE,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC9C,YAAY,WAAoC,EAAE;QAChD,KAAK,CAAC,mBAAmB,EAAE,eAAe,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IAC7D,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC9C,YAAY,WAAoC,EAAE;QAChD,KAAK,CAAC,4BAA4B,EAAE,eAAe,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IACtE,CAAC;CACF;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,qBAAsB,SAAQ,SAAS;IAClD,YACE,QAAgB,EAChB,MAAc,EACd,WAAoC,EAAE;QAEtC,KAAK,CACH,6BAA6B,MAAM,IAAI,QAAQ,EAAE,EACjD,mBAAmB,EACnB,GAAG,EACH,EAAE,GAAG,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,CAClC,CAAC;IACJ,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,OAAO,cAAe,SAAQ,SAAS;IAC3C,YACE,UAAkB,kBAAkB,EACpC,WAAoC,EAAE;QAEtC,KAAK,CAAC,OAAO,EAAE,WAAW,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IAC7C,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,OAAO,oBAAqB,SAAQ,SAAS;IACjD,YACE,UAAkB,yBAAyB,EAC3C,WAAoC,EAAE;QAEtC,KAAK,CAAC,OAAO,EAAE,iBAAiB,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IACnD,CAAC;CACF"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for all Beethovn errors
|
|
3
|
+
*
|
|
4
|
+
* Provides structured error information for logging, monitoring, and debugging.
|
|
5
|
+
* All custom errors in the Beethovn platform should extend this class.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* class MyCustomError extends BeethovnError {
|
|
10
|
+
* constructor(message: string) {
|
|
11
|
+
* super(message, 'MY_CUSTOM_ERROR', 400, { custom: 'metadata' });
|
|
12
|
+
* }
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare abstract class BeethovnError extends Error {
|
|
17
|
+
/** Unique error code for categorization and monitoring */
|
|
18
|
+
readonly code: string;
|
|
19
|
+
/** HTTP status code (for API errors) */
|
|
20
|
+
readonly statusCode: number;
|
|
21
|
+
/** Additional contextual metadata for debugging */
|
|
22
|
+
readonly metadata: Record<string, unknown>;
|
|
23
|
+
/** Timestamp when error occurred */
|
|
24
|
+
readonly timestamp: Date;
|
|
25
|
+
/** Is this error recoverable with retry logic? */
|
|
26
|
+
readonly recoverable: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Create a new BeethovnError
|
|
29
|
+
*
|
|
30
|
+
* @param message - Human-readable error message
|
|
31
|
+
* @param code - Unique error code (e.g., 'VALIDATION_ERROR')
|
|
32
|
+
* @param statusCode - HTTP status code (default: 500)
|
|
33
|
+
* @param metadata - Additional context for debugging
|
|
34
|
+
* @param recoverable - Can this error be retried? (default: false)
|
|
35
|
+
*/
|
|
36
|
+
constructor(message: string, code: string, statusCode?: number, metadata?: Record<string, unknown>, recoverable?: boolean);
|
|
37
|
+
/**
|
|
38
|
+
* Serialize error for logging and monitoring
|
|
39
|
+
*
|
|
40
|
+
* @returns Plain object representation of the error
|
|
41
|
+
*/
|
|
42
|
+
toJSON(): Record<string, unknown>;
|
|
43
|
+
/**
|
|
44
|
+
* Get a sanitized version of the error for client responses
|
|
45
|
+
* (Excludes stack traces and sensitive metadata)
|
|
46
|
+
*
|
|
47
|
+
* @returns Sanitized error object safe for API responses
|
|
48
|
+
*/
|
|
49
|
+
toClient(): Record<string, unknown>;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=BeethovnError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BeethovnError.d.ts","sourceRoot":"","sources":["../../src/base/BeethovnError.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,8BAAsB,aAAc,SAAQ,KAAK;IAC/C,0DAA0D;IAC1D,SAAgB,IAAI,EAAE,MAAM,CAAC;IAE7B,wCAAwC;IACxC,SAAgB,UAAU,EAAE,MAAM,CAAC;IAEnC,mDAAmD;IACnD,SAAgB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAElD,oCAAoC;IACpC,SAAgB,SAAS,EAAE,IAAI,CAAC;IAEhC,kDAAkD;IAClD,SAAgB,WAAW,EAAE,OAAO,CAAC;IAErC;;;;;;;;OAQG;gBAED,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,UAAU,GAAE,MAAY,EACxB,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,EACtC,WAAW,GAAE,OAAe;IAc9B;;;;OAIG;IACH,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAajC;;;;;OAKG;IACH,QAAQ,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAQpC"}
|