diff --git a/CHANGELOG.md b/CHANGELOG.md index 20027724..7de735d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Planned version: **8.5.1**. - `certificateExpiresAt` and `thumbprint` on `Credential`, plus `CredentialType.CONNECTORCERTIFICATE` and `CredentialType.ADMINCONSENTCERTIFICATE`, for Dashboard-generated certificate credentials (NYL-515). ### Fixed +- Populate `requestId` on API errors from the response body `request_id` when the `x-request-id` header is absent, e.g. on `api.provider_error` responses (CUST-5800). - Preserve upload-session HTTP header map keys while decoding API responses (NYL-476). - Patch vulnerable SDK tooling and example HTTP dependencies while preserving TypeDoc compatibility (NYL-471). - Redact request bodies, provider errors, and stack traces from AWS Lambda example errors and logs (NYL-473). diff --git a/src/apiClient.ts b/src/apiClient.ts index 8eedf878..d80df6b5 100644 --- a/src/apiClient.ts +++ b/src/apiClient.ts @@ -209,6 +209,13 @@ export default class APIClient { try { const parsedError = JSON.parse(text); const camelCaseError = objKeysToCamelCase(parsedError); + // Prefer the header, but fall back to the request_id in the body so + // it is never dropped when the header is absent (e.g. provider errors). + const errorRequestId = + requestId ?? + (typeof camelCaseError?.requestId === 'string' + ? camelCaseError.requestId + : undefined); // Check if the request is an authentication request const isAuthRequest = @@ -219,7 +226,7 @@ export default class APIClient { error = new NylasOAuthError( camelCaseError, response.status, - requestId, + errorRequestId, flowId, headers ); @@ -227,7 +234,7 @@ export default class APIClient { error = new NylasApiError( camelCaseError, response.status, - requestId, + errorRequestId, flowId, headers ); diff --git a/src/models/error.ts b/src/models/error.ts index d14a5979..12412c28 100644 --- a/src/models/error.ts +++ b/src/models/error.ts @@ -51,7 +51,7 @@ export class NylasApiError ) { super(apiError.error.message); this.type = apiError.error.type; - this.requestId = requestId; + this.requestId = requestId ?? apiError.requestId; this.flowId = flowId; this.headers = headers; this.providerError = apiError.error.providerError; diff --git a/tests/apiClient.spec.ts b/tests/apiClient.spec.ts index 1d12d65a..a9d073de 100644 --- a/tests/apiClient.spec.ts +++ b/tests/apiClient.spec.ts @@ -767,6 +767,46 @@ describe('APIClient', () => { } }); + it('should set requestId from the response body on provider_error when the header is absent', async () => { + const payload = { + request_id: 'body-provider-req-id', + error: { + type: 'api.provider_error', + message: 'object was not found on the provider', + provider_error: { error: { code: 404 } }, + }, + }; + fetchMock.mockImplementation(() => + Promise.resolve(mockResponse(JSON.stringify(payload), 404)) + ); + + await expect( + client.request({ + path: '/grants/grant-id/messages/send', + method: 'POST', + }) + ).rejects.toMatchObject({ + constructor: NylasApiError, + type: 'api.provider_error', + statusCode: 404, + requestId: 'body-provider-req-id', + }); + }); + + it('should prefer the x-request-id header over the body request_id', async () => { + const payload = { + request_id: 'body-req-id', + error: { type: 'api.provider_error', message: 'provider error' }, + }; + const mockResp = mockResponse(JSON.stringify(payload), 404); + mockResp.headers.set('x-request-id', 'header-req-id'); + fetchMock.mockImplementation(() => Promise.resolve(mockResp)); + + await expect( + client.request({ path: '/events', method: 'POST' }) + ).rejects.toMatchObject({ requestId: 'header-req-id' }); + }); + it('should respect override timeout when provided in seconds (value < 1000)', async () => { const overrideTimeout = 2; // 2 second timeout