Skip to content

Commit

Permalink
chore(clerk-js): Use fapi error long message instead of statusText wh…
Browse files Browse the repository at this point in the history
…en throwing API errors (#4511)
  • Loading branch information
panteliselef authored Nov 11, 2024
1 parent 18f4043 commit 152019b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-numbers-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Use fapi error long message instead of statusText when throwing API errors.
34 changes: 32 additions & 2 deletions packages/clerk-js/src/core/resources/Base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isValidBrowserOnline } from '@clerk/shared/browser';
import { isProductionFromPublishableKey } from '@clerk/shared/keys';
import type { ClerkAPIErrorJSON, ClerkResourceJSON, ClerkResourceReloadParams, DeletedObjectJSON } from '@clerk/types';

import { clerkMissingFapiClientInResources } from '../errors';
Expand All @@ -15,6 +16,30 @@ export type BaseMutateParams = {
path?: string;
};

function assertProductionKeysOnDev(statusCode: number, payloadErrors?: ClerkAPIErrorJSON[]) {
if (!payloadErrors) {
return;
}

if (!payloadErrors[0]) {
return;
}

const safeError = payloadErrors[0];
const safeErrorMessage = safeError.long_message;

if (safeError.code === 'origin_invalid' && isProductionFromPublishableKey(BaseResource.clerk.publishableKey)) {
const prodDomain = BaseResource.clerk.frontendApi.replace('clerk.', '');
throw new ClerkAPIResponseError(
`Clerk: Production Keys are only allowed for domain "${prodDomain}". \nAPI Error: ${safeErrorMessage}`,
{
data: payloadErrors,
status: statusCode,
},
);
}
}

export abstract class BaseResource {
static clerk: Clerk;
id?: string;
Expand Down Expand Up @@ -67,8 +92,13 @@ export abstract class BaseResource {
}

if (status >= 400) {
throw new ClerkAPIResponseError(statusText, {
data: payload?.errors as ClerkAPIErrorJSON[],
const errors = payload?.errors as ClerkAPIErrorJSON[];
const safeErrorMessage = errors?.[0]?.long_message;

assertProductionKeysOnDev(status, errors);

throw new ClerkAPIResponseError(safeErrorMessage || statusText, {
data: errors,
status: status,
});
}
Expand Down

0 comments on commit 152019b

Please sign in to comment.