forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-utils.ts
More file actions
64 lines (57 loc) · 2.32 KB
/
Copy patherror-utils.ts
File metadata and controls
64 lines (57 loc) · 2.32 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
import {
CopilotKitLowLevelError,
CopilotKitErrorCode,
} from "@copilotkit/shared";
/**
* Converts service adapter errors to structured CopilotKitError format using HTTP status codes.
* This provides consistent error classification across all service adapters.
*/
export function convertServiceAdapterError(
error: any,
adapterName: string,
): CopilotKitLowLevelError {
const errorName = error?.constructor?.name || error.name;
const errorMessage = error?.message || String(error);
const statusCode = error.status || error.statusCode || error.response?.status;
const responseData = error.error || error.response?.data || error.data;
// Create the base error with the constructor signature
const structuredError = new CopilotKitLowLevelError({
error: error instanceof Error ? error : new Error(errorMessage),
url: `${adapterName} service adapter`,
message: `${adapterName} API error: ${errorMessage}`,
});
// Add additional properties after construction
if (statusCode) {
(structuredError as any).statusCode = statusCode;
}
if (responseData) {
(structuredError as any).responseData = responseData;
}
if (errorName) {
(structuredError as any).originalErrorType = errorName;
}
// Classify error based on HTTP status codes (reliable and provider-agnostic)
let newCode: CopilotKitErrorCode;
if (statusCode === 401) {
// 401 = Authentication/API key issues
newCode = CopilotKitErrorCode.AUTHENTICATION_ERROR;
} else if (statusCode >= 400 && statusCode < 500) {
// 4xx = Client errors (bad request, invalid params, etc.) - these are configuration issues
newCode = CopilotKitErrorCode.CONFIGURATION_ERROR;
} else if (statusCode >= 500) {
// 5xx = Server errors - keep as NETWORK_ERROR since it's infrastructure related
newCode = CopilotKitErrorCode.NETWORK_ERROR;
} else if (statusCode) {
// Any other HTTP status with an error - likely configuration
newCode = CopilotKitErrorCode.CONFIGURATION_ERROR;
} else {
// No status code - likely a genuine network/connection error
newCode = CopilotKitErrorCode.NETWORK_ERROR;
}
// Update both the instance property and the extensions
(structuredError as any).code = newCode;
if ((structuredError as any).extensions) {
(structuredError as any).extensions.code = newCode;
}
return structuredError;
}