-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
253 lines (222 loc) · 6.55 KB
/
Copy patherrors.ts
File metadata and controls
253 lines (222 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Strava API Error Classes
* Comprehensive error handling for Strava API operations
*/
// ============================================================================
// Error Response Interface
// ============================================================================
/**
* Represents an error response from the Strava API
*/
export interface StravaErrorResponse {
status: number;
data?: { message?: string; errors?: unknown };
headers: Headers;
context?: string;
}
// ============================================================================
// Error Code Type
// ============================================================================
/**
* Union type of all Strava error codes for exhaustiveness checking
*/
export type StravaErrorCode =
| "STRAVA_ERROR"
| "STRAVA_AUTH_ERROR"
| "STRAVA_AUTHORIZATION_ERROR"
| "STRAVA_NOT_FOUND"
| "STRAVA_RATE_LIMIT"
| "STRAVA_TOKEN_REFRESH_ERROR"
| "STRAVA_VALIDATION_ERROR"
| "STRAVA_NETWORK_ERROR"
| "STRAVA_API_ERROR"
| "STRAVA_HTTP_ERROR"
| "STRAVA_UNKNOWN_ERROR";
// ============================================================================
// Base Error Class
// ============================================================================
/**
* Base error class for all Strava API errors
*/
export class StravaError extends Error {
public readonly statusCode?: number;
public readonly code: StravaErrorCode;
public readonly context?: string;
constructor(message: string, code: StravaErrorCode = "STRAVA_ERROR", statusCode?: number) {
super(message);
this.name = "StravaError";
this.code = code;
this.statusCode = statusCode;
// Capture stack trace if available (V8 engines like Node.js)
const errorConstructor = Error as typeof Error & {
captureStackTrace?: (
target: object,
constructor: new (...args: unknown[]) => unknown
) => void;
};
if (typeof errorConstructor.captureStackTrace === "function") {
errorConstructor.captureStackTrace(this, this.constructor);
}
}
}
// ============================================================================
// Specific Error Classes
// ============================================================================
/**
* Authentication error (401)
*/
export class StravaAuthenticationError extends StravaError {
constructor(message: string = "Authentication failed") {
super(message, "STRAVA_AUTH_ERROR", 401);
}
}
/**
* Authorization error (403)
*/
export class StravaAuthorizationError extends StravaError {
constructor(message: string = "Access denied - insufficient permissions") {
super(message, "STRAVA_AUTHORIZATION_ERROR", 403);
}
}
/**
* Resource not found error (404)
*/
export class StravaNotFoundError extends StravaError {
constructor(message: string = "Resource not found") {
super(message, "STRAVA_NOT_FOUND", 404);
}
}
/**
* Rate limit exceeded error (429)
*/
export class StravaRateLimitError extends StravaError {
public readonly retryAfter?: number;
public readonly limit?: string;
public readonly usage?: string;
constructor(
message: string = "Rate limit exceeded",
retryAfter?: number,
limit?: string,
usage?: string
) {
super(message, "STRAVA_RATE_LIMIT", 429);
this.retryAfter = retryAfter;
this.limit = limit;
this.usage = usage;
}
}
/**
* Token refresh error
*/
export class StravaTokenRefreshError extends StravaError {
constructor(message: string = "Failed to refresh access token") {
super(message, "STRAVA_TOKEN_REFRESH_ERROR", 401);
}
}
/**
* Validation error (400)
*/
export class StravaValidationError extends StravaError {
constructor(message: string = "Invalid request parameters") {
super(message, "STRAVA_VALIDATION_ERROR", 400);
}
}
/**
* Network error
*/
export class StravaNetworkError extends StravaError {
constructor(message: string = "Network request failed") {
super(message, "STRAVA_NETWORK_ERROR");
}
}
/**
* API error (5xx)
*/
export class StravaApiError extends StravaError {
constructor(message: string = "Strava API error", statusCode: number = 500) {
super(message, "STRAVA_API_ERROR", statusCode);
}
}
// ============================================================================
// Error Parser
// ============================================================================
/**
* Parse error response into appropriate StravaError
*/
export function parseStravaError(
error: StravaErrorResponse | StravaError | Error | unknown
): StravaError {
// Already a StravaError
if (error instanceof StravaError) {
return error;
}
// Fetch error response
if (isStravaErrorResponse(error)) {
const { status, data, headers, context } = error;
const message = data?.message || `Request failed with status ${status}`;
// Rate limit error
if (status === 429) {
const retryAfterHeader = headers.get("retry-after");
return new StravaRateLimitError(
message,
retryAfterHeader ? parseInt(retryAfterHeader) : undefined,
headers.get("x-ratelimit-limit") ?? undefined,
headers.get("x-ratelimit-usage") ?? undefined
);
}
// Authentication error
if (status === 401) {
return new StravaAuthenticationError(message);
}
// Authorization error
if (status === 403) {
return new StravaAuthorizationError(message);
}
// Not found error
if (status === 404) {
return new StravaNotFoundError(message);
}
// Validation error
if (status === 400) {
return new StravaValidationError(message);
}
// Server errors
if (status >= 500) {
return new StravaApiError(message, status);
}
// Other HTTP errors
return new StravaError(
context ? `${context}: ${message}` : message,
"STRAVA_HTTP_ERROR",
status
);
}
// Standard Error
if (error instanceof Error) {
return new StravaError(error.message, "STRAVA_ERROR");
}
// Unknown error type
return new StravaError(String(error), "STRAVA_UNKNOWN_ERROR");
}
/**
* Type guard for StravaErrorResponse
*/
function isStravaErrorResponse(error: unknown): error is StravaErrorResponse {
return (
typeof error === "object" &&
error !== null &&
"status" in error &&
typeof (error as StravaErrorResponse).status === "number" &&
"headers" in error &&
(error as StravaErrorResponse).headers instanceof Headers
);
}
/**
* Check if error is a specific type
*/
export function isStravaErrorType<T extends StravaError>(
error: unknown,
ErrorClass: new (...args: unknown[]) => T
): error is T {
return error instanceof ErrorClass;
}