@crosspost/sdk 0.1.2

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.
@@ -0,0 +1,77 @@
1
+ import { ApiError, ApiErrorCode, Platform, PlatformError } from '@crosspost/types';
2
+
3
+ /**
4
+ * Handles error responses from the API and converts them to appropriate error objects.
5
+ *
6
+ * @param data The error response data
7
+ * @param status The HTTP status code
8
+ * @returns An ApiError or PlatformError instance
9
+ */
10
+ export function handleErrorResponse(data: any, status: number): ApiError | PlatformError {
11
+ // Safely access nested error properties
12
+ const errorData = data?.error || {};
13
+ const message = errorData?.message || data?.message || 'An API error occurred';
14
+
15
+ // Ensure code is a valid ApiErrorCode or default
16
+ const codeString = errorData?.code || data?.code || ApiErrorCode.UNKNOWN_ERROR;
17
+ const code = Object.values(ApiErrorCode).includes(codeString as ApiErrorCode)
18
+ ? codeString as ApiErrorCode
19
+ : ApiErrorCode.UNKNOWN_ERROR;
20
+
21
+ const details = errorData?.details || data?.details || {};
22
+ const recoverable = errorData?.recoverable ?? data?.recoverable ?? false;
23
+ const platform = errorData?.platform || data?.platform;
24
+
25
+ // Add original response data to details if not already present
26
+ const enhancedDetails = { ...details };
27
+ if (typeof enhancedDetails === 'object' && !enhancedDetails.originalResponse) {
28
+ enhancedDetails.originalResponse = data; // Include the raw error payload for debugging
29
+ }
30
+
31
+ if (platform && Object.values(Platform).includes(platform as Platform)) {
32
+ // If platform is specified and valid, it's a PlatformError
33
+ return new PlatformError(
34
+ message,
35
+ platform as Platform,
36
+ code, // Use the parsed code
37
+ status as any, // Cast status
38
+ enhancedDetails,
39
+ recoverable,
40
+ );
41
+ } else {
42
+ // Otherwise, it's a general ApiError
43
+ return new ApiError(
44
+ message,
45
+ code, // Use the parsed code
46
+ status as any, // Cast status
47
+ enhancedDetails,
48
+ recoverable,
49
+ );
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Creates a network error with appropriate details
55
+ *
56
+ * @param error The original error
57
+ * @param url The request URL
58
+ * @param timeout The request timeout
59
+ * @returns An ApiError instance
60
+ */
61
+ export function createNetworkError(error: unknown, url: string, timeout: number): ApiError {
62
+ if (error instanceof DOMException && error.name === 'AbortError') {
63
+ return new ApiError(
64
+ `Request timed out after ${timeout}ms`,
65
+ ApiErrorCode.NETWORK_ERROR,
66
+ 408,
67
+ { url },
68
+ );
69
+ }
70
+
71
+ return new ApiError(
72
+ error instanceof Error ? error.message : 'An unexpected error occurred during the request',
73
+ ApiErrorCode.INTERNAL_ERROR,
74
+ 500,
75
+ { originalError: String(error), url },
76
+ );
77
+ }