|
| 1 | +/** |
| 2 | + * Custom error class for API errors with optimized error handling |
| 3 | + */ |
| 4 | +export class APIError extends Error { |
| 5 | + public error_code: number | string; |
| 6 | + public status: number; |
| 7 | + public error_message: string; |
| 8 | + |
| 9 | + constructor(message: string, error_code: number | string, status: number) { |
| 10 | + super(message); |
| 11 | + this.name = 'APIError'; |
| 12 | + this.error_code = error_code; |
| 13 | + this.status = status; |
| 14 | + this.error_message = message; |
| 15 | + |
| 16 | + // Remove the stack trace completely to avoid showing internal error handling |
| 17 | + this.stack = undefined; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Creates an APIError from an Axios error response |
| 22 | + * @param err - The Axios error object |
| 23 | + * @returns Formatted APIError with meaningful information |
| 24 | + */ |
| 25 | + static fromAxiosError(err: any): APIError { |
| 26 | + if (err.response?.data) { |
| 27 | + return APIError.fromResponseData(err.response.data, err.response.status); |
| 28 | + } else if (err.message) { |
| 29 | + // For network errors or other non-HTTP errors |
| 30 | + return new APIError(err.message, err.code || 'NETWORK_ERROR', 0); |
| 31 | + } else { |
| 32 | + // Fallback for unknown errors |
| 33 | + return new APIError('Unknown error occurred', 'UNKNOWN_ERROR', 0); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates an APIError from response data |
| 39 | + * @param responseData - The response data from the API |
| 40 | + * @param status - The HTTP status code |
| 41 | + * @returns Formatted APIError |
| 42 | + */ |
| 43 | + static fromResponseData(responseData: any, status: number): APIError { |
| 44 | + // Extract error message with fallback chain |
| 45 | + const errorMessage = APIError.extractErrorMessage(responseData); |
| 46 | + |
| 47 | + // Extract error code with fallback chain |
| 48 | + const errorCode = APIError.extractErrorCode(responseData, status); |
| 49 | + |
| 50 | + return new APIError(errorMessage, errorCode, status); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Extracts error message from response data with multiple fallback options |
| 55 | + */ |
| 56 | + private static extractErrorMessage(responseData: any): string { |
| 57 | + if (responseData.error_message) return responseData.error_message; |
| 58 | + if (responseData.message) return responseData.message; |
| 59 | + if (responseData.error) return responseData.error; |
| 60 | + if (typeof responseData === 'string') return responseData; |
| 61 | + return 'Request failed'; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Extracts error code from response data with fallback to status |
| 66 | + */ |
| 67 | + private static extractErrorCode(responseData: any, status: number): number | string { |
| 68 | + if (responseData.error_code) return responseData.error_code; |
| 69 | + if (responseData.code) return responseData.code; |
| 70 | + return status; |
| 71 | + } |
| 72 | +} |
0 commit comments