pmxtjs 2.20.2 → 2.21.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/pmxt/errors.ts ADDED
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Typed error classes for pmxt.
3
+ *
4
+ * These mirror the error hierarchy in the core TypeScript library,
5
+ * enabling users to catch specific error types.
6
+ */
7
+
8
+ export class PmxtError extends Error {
9
+ public readonly code: string;
10
+ public readonly retryable: boolean;
11
+ public readonly exchange?: string;
12
+
13
+ constructor(
14
+ message: string,
15
+ code: string = "UNKNOWN_ERROR",
16
+ retryable: boolean = false,
17
+ exchange?: string
18
+ ) {
19
+ super(message);
20
+ this.name = this.constructor.name;
21
+ this.code = code;
22
+ this.retryable = retryable;
23
+ this.exchange = exchange;
24
+
25
+ if (Error.captureStackTrace) {
26
+ Error.captureStackTrace(this, this.constructor);
27
+ }
28
+ }
29
+ }
30
+
31
+ // 4xx Client Errors
32
+
33
+ export class BadRequest extends PmxtError {
34
+ constructor(message: string, exchange?: string) {
35
+ super(message, "BAD_REQUEST", false, exchange);
36
+ }
37
+ }
38
+
39
+ export class AuthenticationError extends PmxtError {
40
+ constructor(message: string, exchange?: string) {
41
+ super(message, "AUTHENTICATION_ERROR", false, exchange);
42
+ }
43
+ }
44
+
45
+ export class PermissionDenied extends PmxtError {
46
+ constructor(message: string, exchange?: string) {
47
+ super(message, "PERMISSION_DENIED", false, exchange);
48
+ }
49
+ }
50
+
51
+ export class NotFoundError extends PmxtError {
52
+ constructor(message: string, exchange?: string) {
53
+ super(message, "NOT_FOUND", false, exchange);
54
+ }
55
+ }
56
+
57
+ export class OrderNotFound extends NotFoundError {
58
+ constructor(orderId: string, exchange?: string) {
59
+ super(`Order not found: ${orderId}`, exchange);
60
+ (this as any).code = "ORDER_NOT_FOUND";
61
+ }
62
+ }
63
+
64
+ export class MarketNotFound extends NotFoundError {
65
+ constructor(marketId: string, exchange?: string) {
66
+ super(`Market not found: ${marketId}`, exchange);
67
+ (this as any).code = "MARKET_NOT_FOUND";
68
+ }
69
+ }
70
+
71
+ export class EventNotFound extends NotFoundError {
72
+ constructor(identifier: string, exchange?: string) {
73
+ super(`Event not found: ${identifier}`, exchange);
74
+ (this as any).code = "EVENT_NOT_FOUND";
75
+ }
76
+ }
77
+
78
+ export class RateLimitExceeded extends PmxtError {
79
+ public readonly retryAfter?: number;
80
+
81
+ constructor(message: string, retryAfter?: number, exchange?: string) {
82
+ super(message, "RATE_LIMIT_EXCEEDED", true, exchange);
83
+ this.retryAfter = retryAfter;
84
+ }
85
+ }
86
+
87
+ export class InvalidOrder extends PmxtError {
88
+ constructor(message: string, exchange?: string) {
89
+ super(message, "INVALID_ORDER", false, exchange);
90
+ }
91
+ }
92
+
93
+ export class InsufficientFunds extends PmxtError {
94
+ constructor(message: string, exchange?: string) {
95
+ super(message, "INSUFFICIENT_FUNDS", false, exchange);
96
+ }
97
+ }
98
+
99
+ export class ValidationError extends PmxtError {
100
+ public readonly field?: string;
101
+
102
+ constructor(message: string, field?: string, exchange?: string) {
103
+ super(message, "VALIDATION_ERROR", false, exchange);
104
+ this.field = field;
105
+ }
106
+ }
107
+
108
+ // 5xx Server/Network Errors
109
+
110
+ export class NetworkError extends PmxtError {
111
+ constructor(message: string, exchange?: string) {
112
+ super(message, "NETWORK_ERROR", true, exchange);
113
+ }
114
+ }
115
+
116
+ export class ExchangeNotAvailable extends PmxtError {
117
+ constructor(message: string, exchange?: string) {
118
+ super(message, "EXCHANGE_NOT_AVAILABLE", true, exchange);
119
+ }
120
+ }
121
+
122
+ // Error code to class mapping
123
+ const ERROR_CODE_MAP: Record<string, new (...args: any[]) => PmxtError> = {
124
+ BAD_REQUEST: BadRequest,
125
+ AUTHENTICATION_ERROR: AuthenticationError,
126
+ PERMISSION_DENIED: PermissionDenied,
127
+ NOT_FOUND: NotFoundError,
128
+ ORDER_NOT_FOUND: OrderNotFound,
129
+ MARKET_NOT_FOUND: MarketNotFound,
130
+ EVENT_NOT_FOUND: EventNotFound,
131
+ RATE_LIMIT_EXCEEDED: RateLimitExceeded,
132
+ INVALID_ORDER: InvalidOrder,
133
+ INSUFFICIENT_FUNDS: InsufficientFunds,
134
+ VALIDATION_ERROR: ValidationError,
135
+ NETWORK_ERROR: NetworkError,
136
+ EXCHANGE_NOT_AVAILABLE: ExchangeNotAvailable,
137
+ };
138
+
139
+ /** Convert a server error response object into a typed PmxtError. */
140
+ export function fromServerError(errorData: any): PmxtError {
141
+ if (typeof errorData === "string") {
142
+ return new PmxtError(errorData);
143
+ }
144
+
145
+ const message = errorData.message || "Unknown error";
146
+ const code = errorData.code || "UNKNOWN_ERROR";
147
+ const retryable = errorData.retryable || false;
148
+ const exchange = errorData.exchange;
149
+
150
+ const ErrorClass = ERROR_CODE_MAP[code];
151
+
152
+ if (ErrorClass === RateLimitExceeded) {
153
+ return new RateLimitExceeded(message, errorData.retryAfter, exchange);
154
+ }
155
+ if (ErrorClass === ValidationError) {
156
+ return new ValidationError(message, errorData.field, exchange);
157
+ }
158
+ if (ErrorClass) {
159
+ return new ErrorClass(message, exchange);
160
+ }
161
+
162
+ return new PmxtError(message, code, retryable, exchange);
163
+ }