Skip to content

Commit

Permalink
chore(shared): Improve formatting of ClerkRuntimeError message (#4574)
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored Nov 15, 2024
1 parent 6926e23 commit b28c5e8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-kids-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/shared': patch
---

Improve formatting of ClerkRuntimeError message to include error code.
32 changes: 31 additions & 1 deletion packages/shared/src/__tests__/error.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ErrorThrowerOptions } from '../error';
import { buildErrorThrower } from '../error';
import { buildErrorThrower, ClerkRuntimeError, isClerkRuntimeError } from '../error';

describe('ErrorThrower', () => {
const errorThrower = buildErrorThrower({ packageName: '@clerk/test-package' });
Expand Down Expand Up @@ -32,3 +32,33 @@ describe('ErrorThrower', () => {
).toThrow('@clerk/another-test-package: This is a custom error message for key=whatever and an unknown ');
});
});

describe('ClerkRuntimeError', () => {
const clerkRuntimeError = new ClerkRuntimeError('test', { code: 'test_code' });

it('throws the correct error message', () => {
expect(() => {
throw clerkRuntimeError;
}).toThrow(/^🔒 Clerk:\ntest\n\n\(Code: "test_code"\)/);
});

it('throws the correct error message without duplicate prefixes', () => {
expect(() => {
throw new ClerkRuntimeError('🔒 Clerk: test', { code: 'test_code' });
}).toThrow(/^🔒 Clerk:\ntest\n\n\(Code: "test_code"\)/);
});

it('properties are populated correctly', () => {
expect(clerkRuntimeError.name).toEqual('ClerkRuntimeError');
expect(clerkRuntimeError.code).toEqual('test_code');
expect(clerkRuntimeError.message).toMatch(/🔒 Clerk:\ntest\n\n\(Code: "test_code"\)/);
expect(clerkRuntimeError.clerkRuntimeError).toBe(true);
expect(clerkRuntimeError.toString()).toMatch(
/^\[ClerkRuntimeError\]\nMessage:🔒 Clerk:\ntest\n\n\(Code: "test_code"\)/,
);
});

it('helper recognises error', () => {
expect(isClerkRuntimeError(clerkRuntimeError)).toEqual(true);
});
});
9 changes: 7 additions & 2 deletions packages/shared/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,18 @@ export class ClerkRuntimeError extends Error {
code: string;

constructor(message: string, { code }: { code: string }) {
super(message);
const prefix = '🔒 Clerk:';
const regex = new RegExp(prefix.replace(' ', '\\s*'), 'i');
const sanitized = message.replace(regex, '');
const _message = `${prefix}\n${sanitized.trim()}\n\n(Code: "${code}")\n`;
super(_message);

Object.setPrototypeOf(this, ClerkRuntimeError.prototype);

this.code = code;
this.message = message;
this.message = _message;
this.clerkRuntimeError = true;
this.name = 'ClerkRuntimeError';
}

/**
Expand Down

0 comments on commit b28c5e8

Please sign in to comment.