-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors.ts
More file actions
275 lines (252 loc) · 7.25 KB
/
errors.ts
File metadata and controls
275 lines (252 loc) · 7.25 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Base error class for all ObjectStack adapter errors
*/
export class ObjectStackError extends Error {
/**
* Create a new ObjectStackError
*
* @param message - Human-readable error message
* @param code - Unique error code for programmatic handling
* @param statusCode - Optional HTTP status code
* @param details - Optional additional error details for debugging
*/
constructor(
message: string,
public code: string,
public statusCode?: number,
public details?: Record<string, unknown>
) {
super(message);
this.name = 'ObjectStackError';
// Maintains proper stack trace for where error was thrown (only in V8)
if ((Error as any).captureStackTrace) {
(Error as any).captureStackTrace(this, this.constructor);
}
}
/**
* Convert error to JSON for logging/debugging
*/
toJSON() {
return {
name: this.name,
message: this.message,
code: this.code,
statusCode: this.statusCode,
details: this.details,
stack: this.stack,
};
}
}
/**
* Error thrown when requested metadata/schema is not found
*/
export class MetadataNotFoundError extends ObjectStackError {
constructor(
objectName: string,
details?: Record<string, unknown>
) {
super(
`Metadata not found for object: ${objectName}`,
'METADATA_NOT_FOUND',
404,
{ objectName, ...details }
);
this.name = 'MetadataNotFoundError';
}
}
/**
* Error thrown when a bulk operation fails
*/
export class BulkOperationError extends ObjectStackError {
/**
* Create a new BulkOperationError
*
* @param operation - The bulk operation that failed (create, update, delete)
* @param successCount - Number of successful operations
* @param failureCount - Number of failed operations
* @param errors - Array of individual errors
* @param details - Additional error details
*/
constructor(
operation: 'create' | 'update' | 'delete',
public successCount: number,
public failureCount: number,
public errors: Array<{ index: number; error: unknown }>,
details?: Record<string, unknown>
) {
super(
`Bulk ${operation} operation failed: ${successCount} succeeded, ${failureCount} failed`,
'BULK_OPERATION_ERROR',
500,
{
operation,
successCount,
failureCount,
errors,
...details,
}
);
this.name = 'BulkOperationError';
}
/**
* Get a summary of the bulk operation failure
*/
getSummary() {
const total = this.successCount + this.failureCount;
const failureRate = total > 0 ? this.failureCount / total : 0;
return {
operation: this.details?.operation as string,
total: total,
successful: this.successCount,
failed: this.failureCount,
failureRate: failureRate,
errors: this.errors,
};
}
}
/**
* Error thrown when connection to ObjectStack server fails
*/
export class ConnectionError extends ObjectStackError {
constructor(
message: string,
public url?: string,
details?: Record<string, unknown>,
statusCode?: number
) {
super(
`Connection error: ${message}`,
'CONNECTION_ERROR',
statusCode || 503,
{ url, ...details }
);
this.name = 'ConnectionError';
}
}
/**
* Error thrown when authentication fails
*/
export class AuthenticationError extends ObjectStackError {
constructor(
message: string = 'Authentication failed',
details?: Record<string, unknown>,
statusCode?: number
) {
super(
message,
'AUTHENTICATION_ERROR',
statusCode || 401,
details
);
this.name = 'AuthenticationError';
}
}
/**
* Error thrown when data validation fails
*/
export class ValidationError extends ObjectStackError {
/**
* Create a new ValidationError
*
* @param message - Human-readable error message
* @param field - The field that failed validation (optional)
* @param validationErrors - Array of validation error details
* @param details - Additional error details
*/
constructor(
message: string,
public field?: string,
public validationErrors?: Array<{ field: string; message: string }>,
details?: Record<string, unknown>
) {
super(
message,
'VALIDATION_ERROR',
400,
{
field,
validationErrors,
...details,
}
);
this.name = 'ValidationError';
}
/**
* Get all validation errors as a formatted list
*/
getValidationErrors() {
return this.validationErrors || [];
}
}
/**
* Helper function to create an error from an HTTP response
*
* @param response - Response object or error from fetch/axios
* @param context - Additional context for debugging
* @returns Appropriate error instance
*/
export function createErrorFromResponse(response: Record<string, unknown>, context?: string): ObjectStackError {
const status = (response?.status as number) || (response?.statusCode as number) || 500;
const message = (response?.message as string) || (response?.statusText as string) || 'Unknown error';
const details = {
context,
response: {
status,
data: response?.data,
headers: response?.headers,
},
};
switch (status) {
case 401:
return new AuthenticationError(message, details, 401);
case 403:
return new AuthenticationError(message, details, 403);
case 404:
// Check if it's a metadata request based on context
if (context?.includes('metadata') || context?.includes('schema') || context?.includes('getObjectSchema')) {
const objectName = extractObjectName(context);
return new MetadataNotFoundError(objectName, details);
}
return new ObjectStackError(message, 'NOT_FOUND', 404, details);
case 400:
return new ValidationError(message, undefined, (response?.data as Record<string, unknown>)?.errors as Array<{ field: string; message: string }>, details);
case 503:
return new ConnectionError(message, (response?.config as Record<string, unknown>)?.url as string, details, 503);
case 504:
return new ConnectionError(message, (response?.config as Record<string, unknown>)?.url as string, details, 504);
default:
return new ObjectStackError(message, 'UNKNOWN_ERROR', status, details);
}
}
/**
* Helper to extract object name from context string
*/
function extractObjectName(context?: string): string {
if (!context) return 'unknown';
// Try to extract object name from patterns like "getObjectSchema(users)"
const match = context.match(/\(([^)]+)\)/);
return match ? match[1] : 'unknown';
}
/**
* Type guard to check if an error is an ObjectStackError
*/
export function isObjectStackError(error: unknown): error is ObjectStackError {
return error instanceof ObjectStackError;
}
/**
* Type guard to check if an error is a specific ObjectStack error type
*/
export function isErrorType<T extends ObjectStackError>(
error: unknown,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
errorClass: new (...args: any[]) => T
): error is T {
return error instanceof errorClass;
}