* Main entry point for error mapping
(error: unknown)
| 82 | * Main entry point for error mapping |
| 83 | */ |
| 84 | mapError(error: unknown): BaseError { |
| 85 | // Already a BaseError, just add exchange context if missing |
| 86 | if (error instanceof BaseError) { |
| 87 | if (!error.exchange && this.exchangeName) { |
| 88 | return new (error.constructor as new (...args: unknown[]) => BaseError)( |
| 89 | error.message, |
| 90 | this.exchangeName |
| 91 | ); |
| 92 | } |
| 93 | return error; |
| 94 | } |
| 95 | |
| 96 | // Handle axios errors |
| 97 | if (axios.isAxiosError(error)) { |
| 98 | return this.mapAxiosError(error); |
| 99 | } |
| 100 | |
| 101 | // Handle plain objects with status/data (e.g., Polymarket clob-client) |
| 102 | if (isPlainErrorObject(error)) { |
| 103 | const message = this.extractErrorMessage(error); |
| 104 | return this.mapByStatusCode(error.status, message, error.data, error); |
| 105 | } |
| 106 | |
| 107 | // Handle network errors |
| 108 | if (isNodeError(error)) { |
| 109 | if (error.code === 'ECONNREFUSED' || error.code === 'ENOTFOUND' || error.code === 'ETIMEDOUT') { |
| 110 | return new NetworkError( |
| 111 | `Network error: ${error.message}`, |
| 112 | this.exchangeName |
| 113 | ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Handle Error instances with attached HTTP metadata (common in third-party SDKs) |
| 118 | if (error instanceof Error) { |
| 119 | const err = error as ErrorWithHttpMetadata; |
| 120 | const status = err.status ?? err.statusCode ?? err.response?.status ?? err.response?.statusCode; |
| 121 | if (typeof status === 'number') { |
| 122 | const message = this.extractErrorMessage(error); |
| 123 | const data = err.data ?? err.response?.data ?? err.response?.body; |
| 124 | return this.mapByStatusCode(status, message, data, err.response); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Generic error fallback |
| 129 | const message = this.extractErrorMessage(error); |
| 130 | return new BadRequest(message, this.exchangeName); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Maps axios HTTP errors to appropriate error classes |
nothing calls this directly
no test coverage detected