|
1 | 1 | import { Response } from 'node-fetch';
|
2 | 2 |
|
3 |
| -class CoinbaseError extends Error { |
4 |
| - statusCode: number; |
5 |
| - response: Response; |
| 3 | +// Define specific error types for different scenarios |
| 4 | +export enum CoinbaseErrorType { |
| 5 | + AUTHENTICATION = 'AUTHENTICATION', |
| 6 | + PERMISSION = 'PERMISSION', |
| 7 | + VALIDATION = 'VALIDATION', |
| 8 | + RATE_LIMIT = 'RATE_LIMIT', |
| 9 | + SERVER_ERROR = 'SERVER_ERROR', |
| 10 | + NETWORK_ERROR = 'NETWORK_ERROR', |
| 11 | + UNKNOWN = 'UNKNOWN' |
| 12 | +} |
| 13 | + |
| 14 | +export interface CoinbaseErrorDetails { |
| 15 | + type: CoinbaseErrorType; |
| 16 | + message: string; |
| 17 | + details?: Record<string, any>; |
| 18 | + suggestion?: string; |
| 19 | +} |
6 | 20 |
|
7 |
| - constructor(message: string, statusCode: number, response: Response) { |
8 |
| - super(message); |
| 21 | +export class CoinbaseError extends Error { |
| 22 | + readonly statusCode: number; |
| 23 | + readonly response: Response; |
| 24 | + readonly type: CoinbaseErrorType; |
| 25 | + readonly details?: Record<string, any>; |
| 26 | + readonly suggestion?: string; |
| 27 | + |
| 28 | + constructor(errorDetails: CoinbaseErrorDetails, statusCode: number, response: Response) { |
| 29 | + super(errorDetails.message); |
9 | 30 | this.name = 'CoinbaseError';
|
10 | 31 | this.statusCode = statusCode;
|
11 | 32 | this.response = response;
|
| 33 | + this.type = errorDetails.type; |
| 34 | + this.details = errorDetails.details; |
| 35 | + this.suggestion = errorDetails.suggestion; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function parseErrorResponse(responseText: string): Record<string, any> { |
| 40 | + try { |
| 41 | + return JSON.parse(responseText); |
| 42 | + } catch { |
| 43 | + return {}; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function getErrorDetails(response: Response, responseText: string): CoinbaseErrorDetails { |
| 48 | + const parsedError = parseErrorResponse(responseText); |
| 49 | + const status = response.status; |
| 50 | + |
| 51 | + // Authentication errors |
| 52 | + if (status === 401) { |
| 53 | + return { |
| 54 | + type: CoinbaseErrorType.AUTHENTICATION, |
| 55 | + message: 'Invalid API credentials', |
| 56 | + suggestion: 'Please verify your API key and secret are correct and not expired.' |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + // Permission errors |
| 61 | + if (status === 403) { |
| 62 | + if (responseText.includes('"error_details":"Missing required scopes"')) { |
| 63 | + return { |
| 64 | + type: CoinbaseErrorType.PERMISSION, |
| 65 | + message: 'Missing required API permissions', |
| 66 | + suggestion: 'Please verify your API key has the necessary permissions enabled in your Coinbase account settings.' |
| 67 | + }; |
| 68 | + } |
| 69 | + return { |
| 70 | + type: CoinbaseErrorType.PERMISSION, |
| 71 | + message: 'Access denied', |
| 72 | + suggestion: 'Please check if you have the necessary permissions to perform this action.' |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + // Validation errors |
| 77 | + if (status === 400) { |
| 78 | + return { |
| 79 | + type: CoinbaseErrorType.VALIDATION, |
| 80 | + message: parsedError.message || 'Invalid request parameters', |
| 81 | + details: parsedError, |
| 82 | + suggestion: 'Please verify all required parameters are provided and have valid values.' |
| 83 | + }; |
12 | 84 | }
|
| 85 | + |
| 86 | + // Rate limit errors |
| 87 | + if (status === 429) { |
| 88 | + return { |
| 89 | + type: CoinbaseErrorType.RATE_LIMIT, |
| 90 | + message: 'Rate limit exceeded', |
| 91 | + suggestion: 'Please reduce your request frequency or wait before trying again.' |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + // Server errors |
| 96 | + if (status >= 500) { |
| 97 | + return { |
| 98 | + type: CoinbaseErrorType.SERVER_ERROR, |
| 99 | + message: 'Coinbase service error', |
| 100 | + suggestion: 'This is a temporary issue with Coinbase. Please try again later.' |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + // Default unknown error |
| 105 | + return { |
| 106 | + type: CoinbaseErrorType.UNKNOWN, |
| 107 | + message: `Unexpected error: ${response.statusText}`, |
| 108 | + details: parsedError, |
| 109 | + suggestion: 'If this persists, please contact team with the error details.' |
| 110 | + }; |
13 | 111 | }
|
14 | 112 |
|
15 | 113 | export function handleException(
|
16 | 114 | response: Response,
|
17 | 115 | responseText: string,
|
18 | 116 | reason: string
|
19 | 117 | ) {
|
20 |
| - let message: string | undefined; |
21 |
| - |
22 |
| - if ( |
23 |
| - (400 <= response.status && response.status <= 499) || |
24 |
| - (500 <= response.status && response.status <= 599) |
25 |
| - ) { |
26 |
| - if ( |
27 |
| - response.status == 403 && |
28 |
| - responseText.includes('"error_details":"Missing required scopes"') |
29 |
| - ) { |
30 |
| - message = `${response.status} Coinbase Error: Missing Required Scopes. Please verify your API keys include the necessary permissions.`; |
31 |
| - } else |
32 |
| - message = `${response.status} Coinbase Error: ${reason} ${responseText}`; |
33 |
| - |
34 |
| - throw new CoinbaseError(message, response.status, response); |
| 118 | + if ((400 <= response.status && response.status <= 499) || |
| 119 | + (500 <= response.status && response.status <= 599)) { |
| 120 | + const errorDetails = getErrorDetails(response, responseText); |
| 121 | + throw new CoinbaseError(errorDetails, response.status, response); |
35 | 122 | }
|
36 | 123 | }
|
0 commit comments