From 84ccb0049041534f111be65f7c7d4d6120069446 Mon Sep 17 00:00:00 2001 From: Stefanos Anagnostou Date: Tue, 17 Dec 2024 20:46:17 +0200 Subject: [PATCH] fix(shared): Improve error message when Publishable Key is missing (#4785) --- .changeset/long-birds-worry.md | 5 +++++ packages/shared/src/__tests__/keys.test.ts | 6 ++++++ packages/shared/src/keys.ts | 7 ++++++- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .changeset/long-birds-worry.md diff --git a/.changeset/long-birds-worry.md b/.changeset/long-birds-worry.md new file mode 100644 index 0000000000..737fcd3ace --- /dev/null +++ b/.changeset/long-birds-worry.md @@ -0,0 +1,5 @@ +--- +'@clerk/shared': patch +--- + +Improve error message when Publishable Key is missing when trying to parse it. diff --git a/packages/shared/src/__tests__/keys.test.ts b/packages/shared/src/__tests__/keys.test.ts index 25a588a7cf..775489434c 100644 --- a/packages/shared/src/__tests__/keys.test.ts +++ b/packages/shared/src/__tests__/keys.test.ts @@ -58,6 +58,12 @@ describe('parsePublishableKey(key)', () => { expect(() => parsePublishableKey('fake_pk', { fatal: true })).toThrowError('Publishable key not valid.'); }); + it('throws an error if the publishable key is missing, when fatal: true', () => { + expect(() => parsePublishableKey(undefined, { fatal: true })).toThrowError( + 'Publishable key is missing. Ensure that your publishable key is correctly configured. Double-check your environment configuration for your keys, or access them here: https://dashboard.clerk.com/last-active?path=api-keys', + ); + }); + it('applies the proxyUrl if provided', () => { expect(parsePublishableKey('pk_live_Y2xlcmsuY2xlcmsuZGV2JA==', { proxyUrl: 'example.com/__clerk' })).toEqual({ frontendApi: 'example.com/__clerk', diff --git a/packages/shared/src/keys.ts b/packages/shared/src/keys.ts index efeb0b4e11..f8f21489c0 100644 --- a/packages/shared/src/keys.ts +++ b/packages/shared/src/keys.ts @@ -39,7 +39,12 @@ export function parsePublishableKey( key = key || ''; if (!key || !isPublishableKey(key)) { - if (options.fatal) { + if (options.fatal && !key) { + throw new Error( + 'Publishable key is missing. Ensure that your publishable key is correctly configured. Double-check your environment configuration for your keys, or access them here: https://dashboard.clerk.com/last-active?path=api-keys', + ); + } + if (options.fatal && !isPublishableKey(key)) { throw new Error('Publishable key not valid.'); } return null;