Skip to content

Commit

Permalink
fix(backend): Add missing details to backend API errors (#2421)
Browse files Browse the repository at this point in the history
This was caused by API errors beeing parsed twice. Once in the response
error handling and again when initializing a new ClerkAPIResponseError.
  • Loading branch information
Nikpolik authored Jan 8, 2024
1 parent 65332d7 commit 71b4b9c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-snails-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': patch
---

Fixed a bug where backend API responses where missing error details. This was caused by parsing the errors twice once in the response error handling code and again when initializing the ClerkAPIResponseError.
12 changes: 11 additions & 1 deletion packages/backend/src/api/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ export default (QUnit: QUnit) => {
});

test('executes a failed backend API request and parses the error response', async assert => {
const mockErrorPayload = { code: 'whatever_error', message: 'whatever error', meta: {} };
const mockErrorPayload = {
code: 'whatever_error',
message: 'whatever error',
long_message: 'some long message',
meta: {
param_name: 'whatever_param',
},
};
const traceId = 'trace_id_123';
fakeFetch = sinon.stub(runtime, 'fetch');
fakeFetch.onCall(0).returns(jsonNotOk({ errors: [mockErrorPayload], clerk_trace_id: traceId }));
Expand All @@ -162,6 +169,9 @@ export default (QUnit: QUnit) => {
assert.equal(e.clerkError, true);
assert.equal(e.status, 422);
assert.equal(e.errors[0].code, 'whatever_error');
assert.equal(e.errors[0].message, 'whatever error');
assert.equal(e.errors[0].longMessage, 'some long message');
assert.equal(e.errors[0].meta.paramName, 'whatever_param');
}

assert.ok(
Expand Down
18 changes: 11 additions & 7 deletions packages/backend/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ type LegacyRequestFunction = <T>(requestOptions: ClerkBackendApiRequestOptions)
* TODO: Simply remove this wrapper and the ClerkAPIResponseError before the v5 release.
*/
const withLegacyReturn =
(cb: any): LegacyRequestFunction =>
(cb: (...args: any) => Promise<ClerkBackendApiResponse<any>>): LegacyRequestFunction =>
async (...args) => {
// @ts-ignore
const { data, errors, status, statusText, clerkTraceId } = await cb<T>(...args);
if (errors === null) {
return data;
const response = await cb(...args);
if (response.errors === null) {
return response.data;
} else {
throw new ClerkAPIResponseError(statusText || '', {
data: errors,
const { errors, clerkTraceId } = response;
// TODO: To be removed with withLegacyReturn
const { status, statusText } = response as any;
const error = new ClerkAPIResponseError(statusText || '', {
data: [],
status: status || '',
clerkTraceId,
});
error.errors = errors;
throw error;
}
};

Expand Down

0 comments on commit 71b4b9c

Please sign in to comment.