@@ -34,25 +34,20 @@ export interface ClerkErrorParams {
3434const __DEV__ = true ;
3535
3636export class ClerkError extends Error {
37+ static name = 'ClerkError' ;
3738 readonly clerkError = true as const ;
38- readonly name : string = 'ClerkError' ;
3939 readonly code : string ;
4040 readonly longMessage : string | undefined ;
4141 readonly docsUrl : string | undefined ;
4242 readonly cause : Error | undefined ;
4343
44- constructor ( opts : ClerkErrorParams ) {
45- const formatMessage = ( msg : string , code : string , docsUrl : string | undefined ) => {
46- msg = `${ this . name } : ${ msg . trim ( ) } \n\n(code="${ code } ")\n\n` ;
47- if ( __DEV__ ) {
48- msg += `\n\nDocs: ${ docsUrl } ` ;
49- }
50- return msg ;
51- } ;
44+ get name ( ) {
45+ return this . constructor . name ;
46+ }
5247
53- super ( formatMessage ( opts . message , opts . code , opts . docsUrl ) , { cause : opts . cause } ) ;
48+ constructor ( opts : ClerkErrorParams ) {
49+ super ( new . target . formatMessage ( new . target . name , opts . message , opts . code , opts . docsUrl ) , { cause : opts . cause } ) ;
5450 Object . setPrototypeOf ( this , ClerkError . prototype ) ;
55-
5651 this . code = opts . code ;
5752 this . docsUrl = opts . docsUrl ;
5853 this . longMessage = opts . longMessage ;
@@ -62,6 +57,20 @@ export class ClerkError extends Error {
6257 public toString ( ) {
6358 return `[${ this . name } ]\nMessage:${ this . message } ` ;
6459 }
60+
61+ protected static formatMessage ( name : string , msg : string , code : string , docsUrl : string | undefined ) {
62+ // Keeping the Clerk prefix for backward compatibility
63+ // msg = `${name}: ${msg.trim()}\n\n(code="${code}")\n\n`;
64+ // We can remove the Clerk prefix in the next major version
65+ const prefix = 'Clerk:' ;
66+ const regex = new RegExp ( prefix . replace ( ' ' , '\\s*' ) , 'i' ) ;
67+ msg = msg . replace ( regex , '' ) ;
68+ msg = `${ prefix } ${ msg . trim ( ) } \n\n(code="${ code } ")\n\n` ;
69+ if ( __DEV__ && docsUrl ) {
70+ msg += `\n\nDocs: ${ docsUrl } ` ;
71+ }
72+ return msg ;
73+ }
6574}
6675
6776/**
0 commit comments