Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
11 changes: 9 additions & 2 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -219,15 +226,15 @@ export default class APIClient {
error = new NylasOAuthError(
camelCaseError,
response.status,
requestId,
errorRequestId,
flowId,
headers
);
} else {
error = new NylasApiError(
camelCaseError,
response.status,
requestId,
errorRequestId,
flowId,
headers
);
Expand Down
2 changes: 1 addition & 1 deletion src/models/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions tests/apiClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading