|
1 | 1 | import type { IdempotencyRecord } from './persistence/IdempotencyRecord.js';
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * Base error for idempotency errors. |
| 5 | + * |
| 6 | + * Generally this error should not be thrown directly unless you are throwing a generic and unknown error. |
| 7 | + */ |
| 8 | +class IdempotencyUnknownError extends Error { |
| 9 | + public constructor(message?: string, options?: ErrorOptions) { |
| 10 | + super(message, options); |
| 11 | + this.name = 'IdempotencyUnknownError'; |
| 12 | + } |
| 13 | +} |
| 14 | + |
3 | 15 | /**
|
4 | 16 | * Item attempting to be inserted into persistence store already exists and is not expired
|
5 | 17 | */
|
6 |
| -class IdempotencyItemAlreadyExistsError extends Error { |
| 18 | +class IdempotencyItemAlreadyExistsError extends IdempotencyUnknownError { |
7 | 19 | public existingRecord?: IdempotencyRecord;
|
8 | 20 |
|
9 |
| - public constructor(message?: string, existingRecord?: IdempotencyRecord) { |
10 |
| - super(message); |
| 21 | + public constructor( |
| 22 | + message?: string, |
| 23 | + existingRecord?: IdempotencyRecord, |
| 24 | + options?: ErrorOptions |
| 25 | + ) { |
| 26 | + super(message, options); |
| 27 | + this.name = 'IdempotencyItemAlreadyExistsError'; |
11 | 28 | this.existingRecord = existingRecord;
|
12 | 29 | }
|
13 | 30 | }
|
14 | 31 |
|
15 | 32 | /**
|
16 | 33 | * Item does not exist in persistence store
|
17 | 34 | */
|
18 |
| -class IdempotencyItemNotFoundError extends Error {} |
| 35 | +class IdempotencyItemNotFoundError extends IdempotencyUnknownError { |
| 36 | + public constructor(message?: string, options?: ErrorOptions) { |
| 37 | + super(message, options); |
| 38 | + this.name = 'IdempotencyItemNotFoundError'; |
| 39 | + } |
| 40 | +} |
19 | 41 |
|
20 | 42 | /**
|
21 | 43 | * Execution with idempotency key is already in progress
|
22 | 44 | */
|
23 |
| -class IdempotencyAlreadyInProgressError extends Error {} |
| 45 | +class IdempotencyAlreadyInProgressError extends IdempotencyUnknownError { |
| 46 | + public existingRecord?: IdempotencyRecord; |
| 47 | + |
| 48 | + public constructor( |
| 49 | + message?: string, |
| 50 | + existingRecord?: IdempotencyRecord, |
| 51 | + options?: ErrorOptions |
| 52 | + ) { |
| 53 | + super(message, options); |
| 54 | + this.name = 'IdempotencyAlreadyInProgressError'; |
| 55 | + this.existingRecord = existingRecord; |
| 56 | + } |
| 57 | +} |
24 | 58 |
|
25 | 59 | /**
|
26 | 60 | * An invalid status was provided
|
27 | 61 | */
|
28 |
| -class IdempotencyInvalidStatusError extends Error {} |
| 62 | +class IdempotencyInvalidStatusError extends IdempotencyUnknownError { |
| 63 | + public constructor(message?: string, options?: ErrorOptions) { |
| 64 | + super(message, options); |
| 65 | + this.name = 'IdempotencyInvalidStatusError'; |
| 66 | + } |
| 67 | +} |
29 | 68 |
|
30 | 69 | /**
|
31 | 70 | * Payload does not match stored idempotency record
|
32 | 71 | */
|
33 |
| -class IdempotencyValidationError extends Error { |
| 72 | +class IdempotencyValidationError extends IdempotencyUnknownError { |
34 | 73 | public existingRecord?: IdempotencyRecord;
|
35 | 74 |
|
36 |
| - public constructor(message?: string, existingRecord?: IdempotencyRecord) { |
37 |
| - super(message); |
| 75 | + public constructor( |
| 76 | + message?: string, |
| 77 | + existingRecord?: IdempotencyRecord, |
| 78 | + options?: ErrorOptions |
| 79 | + ) { |
| 80 | + super(message, options); |
| 81 | + this.name = 'IdempotencyValidationError'; |
38 | 82 | this.existingRecord = existingRecord;
|
39 | 83 | }
|
40 | 84 | }
|
41 | 85 |
|
42 | 86 | /**
|
43 | 87 | * State is inconsistent across multiple requests to persistence store
|
44 | 88 | */
|
45 |
| -class IdempotencyInconsistentStateError extends Error {} |
| 89 | +class IdempotencyInconsistentStateError extends IdempotencyUnknownError { |
| 90 | + public constructor(message?: string, options?: ErrorOptions) { |
| 91 | + super(message, options); |
| 92 | + this.name = 'IdempotencyInconsistentStateError'; |
| 93 | + } |
| 94 | +} |
46 | 95 |
|
47 | 96 | /**
|
48 | 97 | * Unrecoverable error from the data store
|
49 | 98 | */
|
50 |
| -class IdempotencyPersistenceLayerError extends Error { |
| 99 | +class IdempotencyPersistenceLayerError extends IdempotencyUnknownError { |
51 | 100 | public readonly cause: Error | undefined;
|
52 | 101 |
|
53 |
| - public constructor(message: string, cause: Error) { |
54 |
| - const errorMessage = `${message}. This error was caused by: ${cause.message}.`; |
55 |
| - super(errorMessage); |
56 |
| - this.cause = cause; |
| 102 | + public constructor(message: string, options?: ErrorOptions) { |
| 103 | + super(message, options); |
| 104 | + this.name = 'IdempotencyPersistenceLayerError'; |
57 | 105 | }
|
58 | 106 | }
|
59 | 107 |
|
60 | 108 | /**
|
61 | 109 | * Payload does not contain an idempotent key
|
62 | 110 | */
|
63 |
| -class IdempotencyKeyError extends Error {} |
| 111 | +class IdempotencyKeyError extends IdempotencyUnknownError { |
| 112 | + public constructor(message?: string, options?: ErrorOptions) { |
| 113 | + super(message, options); |
| 114 | + this.name = 'IdempotencyKeyError'; |
| 115 | + } |
| 116 | +} |
64 | 117 |
|
65 | 118 | export {
|
| 119 | + IdempotencyUnknownError, |
66 | 120 | IdempotencyItemAlreadyExistsError,
|
67 | 121 | IdempotencyItemNotFoundError,
|
68 | 122 | IdempotencyAlreadyInProgressError,
|
|
0 commit comments