Skip to content

Commit 71b4b9c

Browse files
authored
fix(backend): Add missing details to backend API errors (#2421)
This was caused by API errors beeing parsed twice. Once in the response error handling and again when initializing a new ClerkAPIResponseError.
1 parent 65332d7 commit 71b4b9c

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

.changeset/lucky-snails-help.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/backend': patch
3+
---
4+
5+
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.

packages/backend/src/api/factory.test.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ export default (QUnit: QUnit) => {
150150
});
151151

152152
test('executes a failed backend API request and parses the error response', async assert => {
153-
const mockErrorPayload = { code: 'whatever_error', message: 'whatever error', meta: {} };
153+
const mockErrorPayload = {
154+
code: 'whatever_error',
155+
message: 'whatever error',
156+
long_message: 'some long message',
157+
meta: {
158+
param_name: 'whatever_param',
159+
},
160+
};
154161
const traceId = 'trace_id_123';
155162
fakeFetch = sinon.stub(runtime, 'fetch');
156163
fakeFetch.onCall(0).returns(jsonNotOk({ errors: [mockErrorPayload], clerk_trace_id: traceId }));
@@ -162,6 +169,9 @@ export default (QUnit: QUnit) => {
162169
assert.equal(e.clerkError, true);
163170
assert.equal(e.status, 422);
164171
assert.equal(e.errors[0].code, 'whatever_error');
172+
assert.equal(e.errors[0].message, 'whatever error');
173+
assert.equal(e.errors[0].longMessage, 'some long message');
174+
assert.equal(e.errors[0].meta.paramName, 'whatever_param');
165175
}
166176

167177
assert.ok(

packages/backend/src/api/request.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,22 @@ type LegacyRequestFunction = <T>(requestOptions: ClerkBackendApiRequestOptions)
5151
* TODO: Simply remove this wrapper and the ClerkAPIResponseError before the v5 release.
5252
*/
5353
const withLegacyReturn =
54-
(cb: any): LegacyRequestFunction =>
54+
(cb: (...args: any) => Promise<ClerkBackendApiResponse<any>>): LegacyRequestFunction =>
5555
async (...args) => {
56-
// @ts-ignore
57-
const { data, errors, status, statusText, clerkTraceId } = await cb<T>(...args);
58-
if (errors === null) {
59-
return data;
56+
const response = await cb(...args);
57+
if (response.errors === null) {
58+
return response.data;
6059
} else {
61-
throw new ClerkAPIResponseError(statusText || '', {
62-
data: errors,
60+
const { errors, clerkTraceId } = response;
61+
// TODO: To be removed with withLegacyReturn
62+
const { status, statusText } = response as any;
63+
const error = new ClerkAPIResponseError(statusText || '', {
64+
data: [],
6365
status: status || '',
6466
clerkTraceId,
6567
});
68+
error.errors = errors;
69+
throw error;
6670
}
6771
};
6872

0 commit comments

Comments
 (0)