Skip to content

Fix backend error message details #2421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
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