|
8 | 8 | export class S2Error extends Error { |
9 | 9 | public readonly code?: string; |
10 | 10 | public readonly status?: number; |
11 | | - public readonly data?: Record<string, unknown>; |
12 | 11 |
|
13 | 12 | constructor({ |
14 | 13 | message, |
15 | 14 | code, |
16 | 15 | status, |
17 | | - data, |
18 | 16 | }: { |
19 | | - /** Human-readable error message. */ |
20 | 17 | message: string; |
21 | 18 | code?: string; |
22 | 19 | status?: number; |
23 | | - /** Additional error details when available. */ |
24 | | - data?: Record<string, unknown>; |
25 | 20 | }) { |
26 | | - // Include full data in the error message for better visibility |
27 | | - const dataStr = data ? `\nData: ${JSON.stringify(data, null, 2)}` : ""; |
28 | | - super(`${message}${dataStr}`); |
| 21 | + super(message); |
29 | 22 | this.code = code; |
30 | 23 | this.status = status; |
31 | | - this.data = data; |
32 | 24 | this.name = "S2Error"; |
33 | 25 | } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Thrown when an append operation fails due to a sequence number mismatch. |
| 30 | + * |
| 31 | + * This occurs when you specify a `matchSeqNum` condition in your append request, |
| 32 | + * but the current tail sequence number of the stream doesn't match. |
| 33 | + * |
| 34 | + * The `expectedSeqNum` property contains the actual next sequence number |
| 35 | + * that should be used for a successful append. |
| 36 | + */ |
| 37 | +export class SeqNumMismatchError extends S2Error { |
| 38 | + /** The expected next sequence number for the stream. */ |
| 39 | + public readonly expectedSeqNum: number; |
34 | 40 |
|
35 | | - public toString() { |
36 | | - return `${this.message} (code: ${this.code}, status: ${this.status}, data: ${JSON.stringify(this.data, null, 2)})`; |
| 41 | + constructor({ |
| 42 | + message, |
| 43 | + code, |
| 44 | + status, |
| 45 | + expectedSeqNum, |
| 46 | + }: { |
| 47 | + message: string; |
| 48 | + code?: string; |
| 49 | + status?: number; |
| 50 | + expectedSeqNum: number; |
| 51 | + }) { |
| 52 | + super({ |
| 53 | + message: `${message}\nExpected sequence number: ${expectedSeqNum}`, |
| 54 | + code, |
| 55 | + status, |
| 56 | + }); |
| 57 | + this.name = "SeqNumMismatchError"; |
| 58 | + this.expectedSeqNum = expectedSeqNum; |
37 | 59 | } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Thrown when an append operation fails due to a fencing token mismatch. |
| 64 | + * |
| 65 | + * This occurs when you specify a `fencingToken` condition in your append request, |
| 66 | + * but the current fencing token of the stream doesn't match. |
| 67 | + * |
| 68 | + * The `expectedFencingToken` property contains the actual fencing token |
| 69 | + * that should be used for a successful append. |
| 70 | + */ |
| 71 | +export class FencingTokenMismatchError extends S2Error { |
| 72 | + /** The expected fencing token for the stream. */ |
| 73 | + public readonly expectedFencingToken: string; |
38 | 74 |
|
39 | | - public toJSON() { |
40 | | - return { |
41 | | - message: this.message, |
42 | | - code: this.code, |
43 | | - status: this.status, |
44 | | - data: this.data, |
45 | | - }; |
| 75 | + constructor({ |
| 76 | + message, |
| 77 | + code, |
| 78 | + status, |
| 79 | + expectedFencingToken, |
| 80 | + }: { |
| 81 | + message: string; |
| 82 | + code?: string; |
| 83 | + status?: number; |
| 84 | + expectedFencingToken: string; |
| 85 | + }) { |
| 86 | + super({ |
| 87 | + message: `${message}\nExpected fencing token: ${expectedFencingToken}`, |
| 88 | + code, |
| 89 | + status, |
| 90 | + }); |
| 91 | + this.name = "FencingTokenMismatchError"; |
| 92 | + this.expectedFencingToken = expectedFencingToken; |
46 | 93 | } |
| 94 | +} |
47 | 95 |
|
48 | | - public [Symbol.for("nodejs.util.inspect.custom")]() { |
49 | | - return { |
50 | | - name: "S2Error", |
51 | | - message: this.message, |
52 | | - code: this.code, |
53 | | - status: this.status, |
54 | | - data: this.data, |
55 | | - stack: this.stack, |
56 | | - }; |
| 96 | +/** |
| 97 | + * Thrown when a read operation fails because the requested position is beyond the stream tail. |
| 98 | + * |
| 99 | + * This occurs when you specify a `startSeqNum` that is greater than the current tail |
| 100 | + * of the stream (HTTP 416 Range Not Satisfiable). |
| 101 | + * |
| 102 | + * To handle this gracefully, you can set `clamp: true` in your read options to |
| 103 | + * automatically start from the tail instead of throwing an error. |
| 104 | + */ |
| 105 | +export class RangeNotSatisfiableError extends S2Error { |
| 106 | + constructor({ |
| 107 | + message = "Range not satisfiable: requested position is beyond the stream tail. Use 'clamp: true' to start from the tail instead.", |
| 108 | + code, |
| 109 | + status = 416, |
| 110 | + }: { |
| 111 | + message?: string; |
| 112 | + code?: string; |
| 113 | + status?: number; |
| 114 | + } = {}) { |
| 115 | + super({ |
| 116 | + message, |
| 117 | + code, |
| 118 | + status, |
| 119 | + }); |
| 120 | + this.name = "RangeNotSatisfiableError"; |
57 | 121 | } |
58 | 122 | } |
0 commit comments