@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.
Files changed (35) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +375 -0
  3. package/dist/auth/AuthError.d.ts +95 -0
  4. package/dist/auth/AuthError.d.ts.map +1 -0
  5. package/dist/auth/AuthError.js +109 -0
  6. package/dist/auth/AuthError.js.map +1 -0
  7. package/dist/base/BeethovnError.d.ts +51 -0
  8. package/dist/base/BeethovnError.d.ts.map +1 -0
  9. package/dist/base/BeethovnError.js +79 -0
  10. package/dist/base/BeethovnError.js.map +1 -0
  11. package/dist/domain/DomainError.d.ts +88 -0
  12. package/dist/domain/DomainError.d.ts.map +1 -0
  13. package/dist/domain/DomainError.js +100 -0
  14. package/dist/domain/DomainError.js.map +1 -0
  15. package/dist/index.d.ts +15 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +21 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/infrastructure/InfrastructureError.d.ts +98 -0
  20. package/dist/infrastructure/InfrastructureError.d.ts.map +1 -0
  21. package/dist/infrastructure/InfrastructureError.js +144 -0
  22. package/dist/infrastructure/InfrastructureError.js.map +1 -0
  23. package/dist/integration/IntegrationError.d.ts +71 -0
  24. package/dist/integration/IntegrationError.d.ts.map +1 -0
  25. package/dist/integration/IntegrationError.js +111 -0
  26. package/dist/integration/IntegrationError.js.map +1 -0
  27. package/dist/utils/errorFactory.d.ts +96 -0
  28. package/dist/utils/errorFactory.d.ts.map +1 -0
  29. package/dist/utils/errorFactory.js +116 -0
  30. package/dist/utils/errorFactory.js.map +1 -0
  31. package/dist/utils/errorGuards.d.ts +133 -0
  32. package/dist/utils/errorGuards.d.ts.map +1 -0
  33. package/dist/utils/errorGuards.js +152 -0
  34. package/dist/utils/errorGuards.js.map +1 -0
  35. package/package.json +38 -0
@@ -0,0 +1,111 @@
1
+ import { BeethovnError } from '../base/BeethovnError.js';
2
+ /**
3
+ * Base class for integration errors
4
+ *
5
+ * Integration errors represent failures in event publishing, message queues,
6
+ * and other inter-service communication mechanisms.
7
+ */
8
+ export class IntegrationError extends BeethovnError {
9
+ constructor(message, code, metadata = {}, recoverable = true) {
10
+ super(message, code, 503, metadata, recoverable);
11
+ }
12
+ }
13
+ /**
14
+ * Event publishing error
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * try {
19
+ * await eventBus.publish(event);
20
+ * } catch (error: unknown) {
21
+ * throw new EventPublishError(event.type, error, { eventId: event.id });
22
+ * }
23
+ * ```
24
+ */
25
+ export class EventPublishError extends IntegrationError {
26
+ constructor(eventType, originalError, metadata = {}) {
27
+ const errorMessage = originalError instanceof Error
28
+ ? originalError.message
29
+ : String(originalError);
30
+ super(`Failed to publish event: ${eventType}`, 'EVENT_PUBLISH_ERROR', {
31
+ ...metadata,
32
+ eventType,
33
+ originalError: errorMessage,
34
+ }, true);
35
+ }
36
+ }
37
+ /**
38
+ * Message queue error
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * try {
43
+ * await messageQueue.send(message);
44
+ * } catch (error: unknown) {
45
+ * throw new MessageQueueError('send', 'payment-queue', error);
46
+ * }
47
+ * ```
48
+ */
49
+ export class MessageQueueError extends IntegrationError {
50
+ constructor(operation, queue, originalError, metadata = {}) {
51
+ const errorMessage = originalError instanceof Error
52
+ ? originalError.message
53
+ : String(originalError);
54
+ super(`Message queue ${operation} failed on ${queue}: ${errorMessage}`, 'MESSAGE_QUEUE_ERROR', {
55
+ ...metadata,
56
+ operation,
57
+ queue,
58
+ originalError: errorMessage,
59
+ }, true);
60
+ }
61
+ }
62
+ /**
63
+ * WebSocket connection error
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * try {
68
+ * await ws.connect();
69
+ * } catch (error: unknown) {
70
+ * throw new WebSocketError('connect', error, { url });
71
+ * }
72
+ * ```
73
+ */
74
+ export class WebSocketError extends IntegrationError {
75
+ constructor(operation, originalError, metadata = {}) {
76
+ const errorMessage = originalError instanceof Error
77
+ ? originalError.message
78
+ : String(originalError);
79
+ super(`WebSocket ${operation} failed: ${errorMessage}`, 'WEBSOCKET_ERROR', {
80
+ ...metadata,
81
+ operation,
82
+ originalError: errorMessage,
83
+ }, true);
84
+ }
85
+ }
86
+ /**
87
+ * Event subscription error
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * try {
92
+ * eventBus.subscribe('OrderCreated', handler);
93
+ * } catch (error: unknown) {
94
+ * throw new EventSubscriptionError('OrderCreated', error);
95
+ * }
96
+ * ```
97
+ */
98
+ export class EventSubscriptionError extends IntegrationError {
99
+ constructor(eventType, originalError, metadata = {}) {
100
+ const errorMessage = originalError instanceof Error
101
+ ? originalError.message
102
+ : String(originalError);
103
+ super(`Failed to subscribe to event: ${eventType}`, 'EVENT_SUBSCRIPTION_ERROR', {
104
+ ...metadata,
105
+ eventType,
106
+ originalError: errorMessage,
107
+ }, false // Subscriptions should fail fast, not retry
108
+ );
109
+ }
110
+ }
111
+ //# sourceMappingURL=IntegrationError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IntegrationError.js","sourceRoot":"","sources":["../../src/integration/IntegrationError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD;;;;;GAKG;AACH,MAAM,OAAgB,gBAAiB,SAAQ,aAAa;IAC1D,YACE,OAAe,EACf,IAAY,EACZ,WAAoC,EAAE,EACtC,cAAuB,IAAI;QAE3B,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;IACnD,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,iBAAkB,SAAQ,gBAAgB;IACrD,YACE,SAAiB,EACjB,aAAuB,EACvB,WAAoC,EAAE;QAEtC,MAAM,YAAY,GAAG,aAAa,YAAY,KAAK;YACjD,CAAC,CAAC,aAAa,CAAC,OAAO;YACvB,CAAC,CAAC,MAAM,CA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C,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;QAE1B,KAAK,CACH,iCAAiC,SAAS,EAAE,EAC5C,0BAA0B,EAC1B;YACE,GAAG,QAAQ;YACX,SAAS;YACT,aAAa,EAAE,YAAY;SAC5B,EACD,KAAK,CAAC,4CAA4C;SACnD,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,96 @@
1
+ import { BeethovnError } from '../base/BeethovnError.js';
2
+ import { ValidationError } from '../domain/DomainError.js';
3
+ import { DatabaseError, NetworkError } from '../infrastructure/InfrastructureError.js';
4
+ /**
5
+ * Factory for creating typed errors from unknown errors
6
+ *
7
+ * Provides consistent error creation patterns and converts
8
+ * unknown errors into structured BeethovnError instances.
9
+ */
10
+ export declare class ErrorFactory {
11
+ /**
12
+ * Create appropriate error from unknown error
13
+ *
14
+ * @param error - The unknown error to convert
15
+ * @param context - Optional context string for debugging
16
+ * @returns Structured BeethovnError instance
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * try {
21
+ * await operation();
22
+ * } catch (error: unknown) {
23
+ * throw ErrorFactory.fromUnknown(error, 'processPayment');
24
+ * }
25
+ * ```
26
+ */
27
+ static fromUnknown(error: unknown, context?: string): BeethovnError;
28
+ /**
29
+ * Wrap database errors
30
+ *
31
+ * @param operation - The database operation that failed
32
+ * @param error - The original error
33
+ * @param metadata - Additional context
34
+ * @returns DatabaseError instance
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * try {
39
+ * await db.query(sql, params);
40
+ * } catch (error: unknown) {
41
+ * throw ErrorFactory.database('saveUser', error, { userId });
42
+ * }
43
+ * ```
44
+ */
45
+ static database(operation: string, error: unknown, metadata?: Record<string, unknown>): DatabaseError;
46
+ /**
47
+ * Create validation error with field
48
+ *
49
+ * @param message - Error message
50
+ * @param field - Field that failed validation
51
+ * @param metadata - Additional context
52
+ * @returns ValidationError instance
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * if (!email.includes('@')) {
57
+ * throw ErrorFactory.validation('Invalid email format', 'email');
58
+ * }
59
+ * ```
60
+ */
61
+ static validation(message: string, field?: string, metadata?: Record<string, unknown>): ValidationError;
62
+ /**
63
+ * Create network error
64
+ *
65
+ * @param message - Error message
66
+ * @param metadata - Additional context (url, method, etc.)
67
+ * @returns NetworkError instance
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * try {
72
+ * await fetch(url);
73
+ * } catch (error: unknown) {
74
+ * throw ErrorFactory.network('Failed to fetch data', { url, method: 'GET' });
75
+ * }
76
+ * ```
77
+ */
78
+ static network(message: string, metadata?: Record<string, unknown>): NetworkError;
79
+ /**
80
+ * Convert multiple errors into a single aggregated error
81
+ *
82
+ * @param errors - Array of errors to aggregate
83
+ * @param context - Context for the aggregated error
84
+ * @returns Single BeethovnError with all errors in metadata
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const errors = validateUser(data);
89
+ * if (errors.length > 0) {
90
+ * throw ErrorFactory.aggregate(errors, 'User validation');
91
+ * }
92
+ * ```
93
+ */
94
+ static aggregate(errors: BeethovnError[], context: string): BeethovnError;
95
+ }
96
+ //# sourceMappingURL=errorFactory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errorFactory.d.ts","sourceRoot":"","sources":["../../src/utils/errorFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,YAAY,EAAgB,MAAM,0CAA0C,CAAC;AAErG;;;;;GAKG;AACH,qBAAa,YAAY;IACvB;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,aAAa;IAUnE;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,QAAQ,CACb,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,OAAO,EACd,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACrC,aAAa;IAIhB;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,UAAU,CACf,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,MAAM,EACd,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACrC,eAAe;IAIlB;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,OAAO,CACZ,OAAO,EAAE,MAAM,EACf,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACrC,YAAY;IAIf;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,SAAS,CACd,MAAM,EAAE,aAAa,EAAE,EACvB,OAAO,EAAE,MAAM,GACd,aAAa;CAYjB"}
@@ -0,0 +1,116 @@
1
+ import { BeethovnError } from '../base/BeethovnError.js';
2
+ import { ValidationError } from '../domain/DomainError.js';
3
+ import { DatabaseError, NetworkError, UnknownError } from '../infrastructure/InfrastructureError.js';
4
+ /**
5
+ * Factory for creating typed errors from unknown errors
6
+ *
7
+ * Provides consistent error creation patterns and converts
8
+ * unknown errors into structured BeethovnError instances.
9
+ */
10
+ export class ErrorFactory {
11
+ /**
12
+ * Create appropriate error from unknown error
13
+ *
14
+ * @param error - The unknown error to convert
15
+ * @param context - Optional context string for debugging
16
+ * @returns Structured BeethovnError instance
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * try {
21
+ * await operation();
22
+ * } catch (error: unknown) {
23
+ * throw ErrorFactory.fromUnknown(error, 'processPayment');
24
+ * }
25
+ * ```
26
+ */
27
+ static fromUnknown(error, context) {
28
+ // Already a BeethovnError - return as is
29
+ if (error instanceof BeethovnError) {
30
+ return error;
31
+ }
32
+ // Any other error type - wrap in UnknownError
33
+ return new UnknownError(error, context);
34
+ }
35
+ /**
36
+ * Wrap database errors
37
+ *
38
+ * @param operation - The database operation that failed
39
+ * @param error - The original error
40
+ * @param metadata - Additional context
41
+ * @returns DatabaseError instance
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * try {
46
+ * await db.query(sql, params);
47
+ * } catch (error: unknown) {
48
+ * throw ErrorFactory.database('saveUser', error, { userId });
49
+ * }
50
+ * ```
51
+ */
52
+ static database(operation, error, metadata = {}) {
53
+ return new DatabaseError(operation, error, metadata);
54
+ }
55
+ /**
56
+ * Create validation error with field
57
+ *
58
+ * @param message - Error message
59
+ * @param field - Field that failed validation
60
+ * @param metadata - Additional context
61
+ * @returns ValidationError instance
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * if (!email.includes('@')) {
66
+ * throw ErrorFactory.validation('Invalid email format', 'email');
67
+ * }
68
+ * ```
69
+ */
70
+ static validation(message, field, metadata = {}) {
71
+ return new ValidationError(message, field, metadata);
72
+ }
73
+ /**
74
+ * Create network error
75
+ *
76
+ * @param message - Error message
77
+ * @param metadata - Additional context (url, method, etc.)
78
+ * @returns NetworkError instance
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * try {
83
+ * await fetch(url);
84
+ * } catch (error: unknown) {
85
+ * throw ErrorFactory.network('Failed to fetch data', { url, method: 'GET' });
86
+ * }
87
+ * ```
88
+ */
89
+ static network(message, metadata = {}) {
90
+ return new NetworkError(message, metadata);
91
+ }
92
+ /**
93
+ * Convert multiple errors into a single aggregated error
94
+ *
95
+ * @param errors - Array of errors to aggregate
96
+ * @param context - Context for the aggregated error
97
+ * @returns Single BeethovnError with all errors in metadata
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const errors = validateUser(data);
102
+ * if (errors.length > 0) {
103
+ * throw ErrorFactory.aggregate(errors, 'User validation');
104
+ * }
105
+ * ```
106
+ */
107
+ static aggregate(errors, context) {
108
+ const messages = errors.map(e => e.message).join('; ');
109
+ return new ValidationError(`${context} failed: ${messages}`, undefined, {
110
+ context,
111
+ errorCount: errors.length,
112
+ errors: errors.map(e => e.toJSON()),
113
+ });
114
+ }
115
+ }
116
+ //# sourceMappingURL=errorFactory.js.map
@@ -0,0 +1 @@
1
+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AAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACvD,OAAO,IAAI,eAAe,CACxB,GAAG,OAAO,YAAY,QAAQ,EAAE,EAChC,SAAS,EACT;YACE,OAAO;YACP,UAAU,EAAE,MAAM,CAAC,MAAM;YACzB,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;SACpC,CACF,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,133 @@
1
+ import { BeethovnError } from '../base/BeethovnError.js';
2
+ import { DomainError } from '../domain/DomainError.js';
3
+ import { InfrastructureError } from '../infrastructure/InfrastructureError.js';
4
+ import { AuthError } from '../auth/AuthError.js';
5
+ import { IntegrationError } from '../integration/IntegrationError.js';
6
+ /**
7
+ * Type guards for error classification
8
+ *
9
+ * Use these to determine error types in catch blocks and
10
+ * implement type-safe error handling logic.
11
+ */
12
+ /**
13
+ * Check if error is a BeethovnError
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * catch (error: unknown) {
18
+ * if (isBeethovnError(error)) {
19
+ * logger.error('Beethovn error', error.toJSON());
20
+ * }
21
+ * }
22
+ * ```
23
+ */
24
+ export declare function isBeethovnError(error: unknown): error is BeethovnError;
25
+ /**
26
+ * Check if error is a domain error (business logic)
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * catch (error: unknown) {
31
+ * if (isDomainError(error)) {
32
+ * // User-fixable error - return helpful message
33
+ * return { error: error.toClient() };
34
+ * }
35
+ * }
36
+ * ```
37
+ */
38
+ export declare function isDomainError(error: unknown): error is DomainError;
39
+ /**
40
+ * Check if error is an infrastructure error (technical)
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * catch (error: unknown) {
45
+ * if (isInfrastructureError(error)) {
46
+ * // Technical error - might be recoverable
47
+ * if (error.recoverable) {
48
+ * await retry(operation);
49
+ * }
50
+ * }
51
+ * }
52
+ * ```
53
+ */
54
+ export declare function isInfrastructureError(error: unknown): error is InfrastructureError;
55
+ /**
56
+ * Check if error is an auth error
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * catch (error: unknown) {
61
+ * if (isAuthError(error)) {
62
+ * // Auth error - redirect to login or show 401/403
63
+ * res.status(error.statusCode).json(error.toClient());
64
+ * }
65
+ * }
66
+ * ```
67
+ */
68
+ export declare function isAuthError(error: unknown): error is AuthError;
69
+ /**
70
+ * Check if error is an integration error
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * catch (error: unknown) {
75
+ * if (isIntegrationError(error)) {
76
+ * // Integration error - log and maybe retry
77
+ * logger.warn('Integration failed', error.toJSON());
78
+ * }
79
+ * }
80
+ * ```
81
+ */
82
+ export declare function isIntegrationError(error: unknown): error is IntegrationError;
83
+ /**
84
+ * Check if error is recoverable (can be retried)
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * catch (error: unknown) {
89
+ * if (isRecoverableError(error)) {
90
+ * // Retry with exponential backoff
91
+ * await retryWithBackoff(operation);
92
+ * } else {
93
+ * // Non-recoverable - fail fast
94
+ * throw error;
95
+ * }
96
+ * }
97
+ * ```
98
+ */
99
+ export declare function isRecoverableError(error: unknown): error is BeethovnError;
100
+ /**
101
+ * Check if error should be exposed to client
102
+ *
103
+ * Domain errors are safe to expose, infrastructure errors are not.
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * catch (error: unknown) {
108
+ * if (isClientSafeError(error)) {
109
+ * res.status(error.statusCode).json(error.toClient());
110
+ * } else {
111
+ * res.status(500).json({ error: 'Internal server error' });
112
+ * }
113
+ * }
114
+ * ```
115
+ */
116
+ export declare function isClientSafeError(error: unknown): error is BeethovnError;
117
+ /**
118
+ * Get HTTP status code from error
119
+ *
120
+ * @param error - Error to get status code from
121
+ * @param defaultStatus - Default status if error is not a BeethovnError
122
+ * @returns HTTP status code
123
+ *
124
+ * @example
125
+ * ```typescript
126
+ * catch (error: unknown) {
127
+ * const statusCode = getErrorStatusCode(error);
128
+ * res.status(statusCode).json({ error: 'Something went wrong' });
129
+ * }
130
+ * ```
131
+ */
132
+ export declare function getErrorStatusCode(error: unknown, defaultStatus?: number): number;
133
+ //# sourceMappingURL=errorGuards.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errorGuards.d.ts","sourceRoot":"","sources":["../../src/utils/errorGuards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAC/E,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAEtE;;;;;GAKG;AAEH;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAEtE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAElE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,mBAAmB,CAElF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAE9D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,gBAAgB,CAE5E;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAEzE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAExE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,aAAa,GAAE,MAAY,GAAG,MAAM,CAKtF"}
@@ -0,0 +1,152 @@
1
+ import { BeethovnError } from '../base/BeethovnError.js';
2
+ import { DomainError } from '../domain/DomainError.js';
3
+ import { InfrastructureError } from '../infrastructure/InfrastructureError.js';
4
+ import { AuthError } from '../auth/AuthError.js';
5
+ import { IntegrationError } from '../integration/IntegrationError.js';
6
+ /**
7
+ * Type guards for error classification
8
+ *
9
+ * Use these to determine error types in catch blocks and
10
+ * implement type-safe error handling logic.
11
+ */
12
+ /**
13
+ * Check if error is a BeethovnError
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * catch (error: unknown) {
18
+ * if (isBeethovnError(error)) {
19
+ * logger.error('Beethovn error', error.toJSON());
20
+ * }
21
+ * }
22
+ * ```
23
+ */
24
+ export function isBeethovnError(error) {
25
+ return error instanceof BeethovnError;
26
+ }
27
+ /**
28
+ * Check if error is a domain error (business logic)
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * catch (error: unknown) {
33
+ * if (isDomainError(error)) {
34
+ * // User-fixable error - return helpful message
35
+ * return { error: error.toClient() };
36
+ * }
37
+ * }
38
+ * ```
39
+ */
40
+ export function isDomainError(error) {
41
+ return error instanceof DomainError;
42
+ }
43
+ /**
44
+ * Check if error is an infrastructure error (technical)
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * catch (error: unknown) {
49
+ * if (isInfrastructureError(error)) {
50
+ * // Technical error - might be recoverable
51
+ * if (error.recoverable) {
52
+ * await retry(operation);
53
+ * }
54
+ * }
55
+ * }
56
+ * ```
57
+ */
58
+ export function isInfrastructureError(error) {
59
+ return error instanceof InfrastructureError;
60
+ }
61
+ /**
62
+ * Check if error is an auth error
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * catch (error: unknown) {
67
+ * if (isAuthError(error)) {
68
+ * // Auth error - redirect to login or show 401/403
69
+ * res.status(error.statusCode).json(error.toClient());
70
+ * }
71
+ * }
72
+ * ```
73
+ */
74
+ export function isAuthError(error) {
75
+ return error instanceof AuthError;
76
+ }
77
+ /**
78
+ * Check if error is an integration error
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * catch (error: unknown) {
83
+ * if (isIntegrationError(error)) {
84
+ * // Integration error - log and maybe retry
85
+ * logger.warn('Integration failed', error.toJSON());
86
+ * }
87
+ * }
88
+ * ```
89
+ */
90
+ export function isIntegrationError(error) {
91
+ return error instanceof IntegrationError;
92
+ }
93
+ /**
94
+ * Check if error is recoverable (can be retried)
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * catch (error: unknown) {
99
+ * if (isRecoverableError(error)) {
100
+ * // Retry with exponential backoff
101
+ * await retryWithBackoff(operation);
102
+ * } else {
103
+ * // Non-recoverable - fail fast
104
+ * throw error;
105
+ * }
106
+ * }
107
+ * ```
108
+ */
109
+ export function isRecoverableError(error) {
110
+ return isBeethovnError(error) && error.recoverable;
111
+ }
112
+ /**
113
+ * Check if error should be exposed to client
114
+ *
115
+ * Domain errors are safe to expose, infrastructure errors are not.
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * catch (error: unknown) {
120
+ * if (isClientSafeError(error)) {
121
+ * res.status(error.statusCode).json(error.toClient());
122
+ * } else {
123
+ * res.status(500).json({ error: 'Internal server error' });
124
+ * }
125
+ * }
126
+ * ```
127
+ */
128
+ export function isClientSafeError(error) {
129
+ return isDomainError(error) || isAuthError(error);
130
+ }
131
+ /**
132
+ * Get HTTP status code from error
133
+ *
134
+ * @param error - Error to get status code from
135
+ * @param defaultStatus - Default status if error is not a BeethovnError
136
+ * @returns HTTP status code
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * catch (error: unknown) {
141
+ * const statusCode = getErrorStatusCode(error);
142
+ * res.status(statusCode).json({ error: 'Something went wrong' });
143
+ * }
144
+ * ```
145
+ */
146
+ export function getErrorStatusCode(error, defaultStatus = 500) {
147
+ if (isBeethovnError(error)) {
148
+ return error.statusCode;
149
+ }
150
+ return defaultStatus;
151
+ }
152
+ //# sourceMappingURL=errorGuards.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errorGuards.js","sourceRoot":"","sources":["../../src/utils/errorGuards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAC/E,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAEtE;;;;;GAKG;AAEH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,OAAO,KAAK,YAAY,aAAa,CAAC;AACxC,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,OAAO,KAAK,YAAY,WAAW,CAAC;AACtC,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,qBAAqB,CAAC,KAAc;IAClD,OAAO,KAAK,YAAY,mBAAmB,CAAC;AAC9C,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,OAAO,KAAK,YAAY,SAAS,CAAC;AACpC,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAc;IAC/C,OAAO,KAAK,YAAY,gBAAgB,CAAC;AAC3C,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAc;IAC/C,OAAO,eAAe,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,WAAW,CAAC;AACrD,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,OAAO,aAAa,CAAC,KAAK,CAAC,IAAI,WAAW,CAAC,KAAK,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAc,EAAE,gBAAwB,GAAG;IAC5E,IAAI,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC,UAAU,CAAC;IAC1B,CAAC;IACD,OAAO,aAAa,CAAC;AACvB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@beethovn/errors",
3
+ "version": "1.0.0",
4
+ "description": "Standardized error types for the Beethovn platform",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "keywords": [
15
+ "errors",
16
+ "error-handling",
17
+ "typescript",
18
+ "beethovn"
19
+ ],
20
+ "author": "Beethovn Team",
21
+ "license": "MIT",
22
+ "devDependencies": {
23
+ "@types/node": "^20.0.0",
24
+ "typescript": "^5.3.3",
25
+ "vitest": "^1.0.0"
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "README.md"
30
+ ],
31
+ "scripts": {
32
+ "build": "tsc",
33
+ "test": "vitest run",
34
+ "test:watch": "vitest",
35
+ "lint": "eslint src --ext .ts",
36
+ "type-check": "tsc --noEmit"
37
+ }
38
+ }