Skip to content
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/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, {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. statusText was empty most of the time (not sure why, status was populated)
  2. We do have a custom toString() for ClerkAPIResponseError but that is not guaranteed to be called.

data: errors,
status: status,
});
}
Expand Down
Loading