|
1 | 1 | import { BaseHttpClient } from '../../common/http/base-http.client'; |
2 | | -import { AxiosError } from 'axios'; |
| 2 | +import { ApiError } from '../../common/errors/api.errors'; |
3 | 3 | import { AzureApiError } from '../errors/azure.errors'; |
4 | 4 | import { LoggerFactory, Logger } from '../../../logger/logger'; |
5 | 5 |
|
| 6 | +function sanitizeErrorData(data: unknown): string { |
| 7 | + if (data == null) return 'no data'; |
| 8 | + if (typeof data === 'string') return data.slice(0, 2000); |
| 9 | + if (typeof data === 'object') { |
| 10 | + const { message, typeKey, errorCode, statusCode } = data as Record<string, unknown>; |
| 11 | + const parts = [ |
| 12 | + message != null ? `message=${message}` : null, |
| 13 | + typeKey != null ? `typeKey=${typeKey}` : null, |
| 14 | + errorCode != null ? `errorCode=${errorCode}` : null, |
| 15 | + statusCode != null ? `statusCode=${statusCode}` : null, |
| 16 | + ].filter(Boolean); |
| 17 | + return parts.length > 0 ? parts.join(', ') : '[response data omitted]'; |
| 18 | + } |
| 19 | + return String(data); |
| 20 | +} |
| 21 | + |
6 | 22 | export interface AzureHttpClientConfig { |
7 | 23 | organization: string; |
8 | 24 | host: string; |
@@ -43,21 +59,36 @@ export class AzureHttpClient extends BaseHttpClient { |
43 | 59 |
|
44 | 60 | this.client.interceptors.response.use( |
45 | 61 | response => response, |
46 | | - (error: AxiosError) => { |
47 | | - if (error.response) { |
48 | | - this.logger.error(`Azure DevOps API Error: ${error.response.status} ${error.response.statusText} - ${JSON.stringify(error.response.data)}`); |
| 62 | + (error) => { |
| 63 | + // Build the AzureApiError first, so wrapping always succeeds regardless of logging |
| 64 | + let azureError: AzureApiError; |
| 65 | + |
| 66 | + // BaseHttpClient already transforms AxiosError → ApiError |
| 67 | + // Map ApiError to Azure-specific errors first |
| 68 | + if (error instanceof ApiError) { |
| 69 | + azureError = new AzureApiError(error.message, error.status, error.data, error); |
| 70 | + try { |
| 71 | + this.logger.error(`Azure DevOps API Error: ${error.status} - ${sanitizeErrorData(error.data)}`); |
| 72 | + } catch { /* logging must not break error propagation */ } |
| 73 | + } else if (error.response) { |
| 74 | + // Fallback: Handle raw AxiosError if BaseHttpClient interceptor didn't catch it |
| 75 | + azureError = new AzureApiError(error.message, error.response.status, error.response.data, error); |
| 76 | + try { |
| 77 | + this.logger.error(`Azure DevOps API Error: ${error.response.status} ${error.response.statusText} - ${sanitizeErrorData(error.response.data)}`); |
| 78 | + } catch { /* logging must not break error propagation */ } |
49 | 79 | } else if (error.request) { |
50 | | - this.logger.error(`Azure DevOps API Error: No response received - ${JSON.stringify(error.request)}`); |
| 80 | + azureError = new AzureApiError('No response received from Azure DevOps', undefined, undefined, error); |
| 81 | + try { |
| 82 | + const { method, baseURL, url, timeout } = error.request?.config ?? error.config ?? {}; |
| 83 | + this.logger.error(`Azure DevOps API Error: No response received - ${method?.toUpperCase()} ${baseURL ?? ''}${url ?? ''} (timeout: ${timeout})`); |
| 84 | + } catch { /* logging must not break error propagation */ } |
51 | 85 | } else { |
52 | | - this.logger.error(`Azure DevOps API Error: Request setup failed - ${error}`); |
| 86 | + azureError = new AzureApiError('Request setup failed', undefined, undefined, error); |
| 87 | + try { |
| 88 | + this.logger.error(`Azure DevOps API Error: Request setup failed - ${error}`); |
| 89 | + } catch { /* logging must not break error propagation */ } |
53 | 90 | } |
54 | | - // Wrap AxiosError in a custom AzureApiError |
55 | | - const azureError = new AzureApiError( |
56 | | - error.message, |
57 | | - error.response?.status, |
58 | | - error.response?.data, |
59 | | - error |
60 | | - ); |
| 91 | + |
61 | 92 | return Promise.reject(azureError); |
62 | 93 | } |
63 | 94 | ); |
|
0 commit comments