Skip to content

Commit

Permalink
fix(backend): Parse api errors that don't throw exception (#2423)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikpolik authored Dec 20, 2023
1 parent a97d74d commit bad4de1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-deers-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': patch
---

Fixed an issue where errors returned from backend api requests are not converted to camelCase.
10 changes: 9 additions & 1 deletion packages/backend/src/api/__tests__/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ 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: 'some param' },
};
const traceId = 'trace_id_123';
fakeFetch = sinon.stub(runtime, 'fetch');
fakeFetch.onCall(0).returns(jsonNotOk({ errors: [mockErrorPayload], clerk_trace_id: traceId }));
Expand All @@ -199,6 +204,9 @@ export default (QUnit: QUnit) => {
assert.equal(response.status, 422);
assert.equal(response.statusText, '422');
assert.equal(response.errors[0].code, 'whatever_error');
assert.equal(response.errors[0].message, 'whatever error');
assert.equal(response.errors[0].longMessage, 'some long message');
assert.equal(response.errors[0].meta.paramName, 'some param');

assert.ok(
fakeFetch.calledOnceWith('https://api.clerk.test/v1/users/user_deadbeef', {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function buildRequest(options: BuildRequestOptions) {
if (!res.ok) {
return {
data: null,
errors: responseBody?.errors || responseBody,
errors: parseErrors(responseBody),
status: res?.status,
statusText: res?.statusText,
clerkTraceId: getTraceId(responseBody, res?.headers),
Expand Down

0 comments on commit bad4de1

Please sign in to comment.