@@ -128,10 +128,10 @@ export interface UserErrorObj {
128128 /** The UserErrorMessage key (for matching certain error) */
129129 readonly key : UserErrorMessage
130130 /** The translatable string for the key */
131- readonly message : ITranslatableMessage
131+ readonly userMessage : ITranslatableMessage
132132}
133133
134- export class UserError implements UserErrorObj {
134+ export class UserError extends Error implements UserErrorObj {
135135 public readonly errorCode : number
136136
137137 private constructor (
@@ -140,10 +140,11 @@ export class UserError implements UserErrorObj {
140140 /** The UserErrorMessage key (for matching certain error) */
141141 public readonly key : UserErrorMessage ,
142142 /** The translatable string for the key */
143- public readonly message : ITranslatableMessage ,
143+ public readonly userMessage : ITranslatableMessage ,
144144 /** Appropriate HTTP status code. Defaults to 500 for generic internal error. */
145145 errorCode : number | undefined
146146 ) {
147+ super ( )
147148 this . errorCode = errorCode ?? 500
148149 }
149150
@@ -159,7 +160,7 @@ export class UserError implements UserErrorObj {
159160 static fromUnknown ( err : unknown , errorCode ?: number ) : UserError {
160161 if ( err instanceof UserError ) return err
161162 if ( this . isUserError ( err ) )
162- return new UserError ( new Error ( err . rawError . toString ( ) ) , err . key , err . message , err . errorCode )
163+ return new UserError ( new Error ( err . rawError . toString ( ) ) , err . key , err . userMessage , err . errorCode )
163164
164165 const err2 = err instanceof Error ? err : new Error ( stringifyError ( err ) )
165166 return new UserError (
@@ -179,7 +180,7 @@ export class UserError implements UserErrorObj {
179180 try {
180181 const p = JSON . parse ( str )
181182 if ( UserError . isUserError ( p ) ) {
182- return new UserError ( new Error ( p . rawError . toString ( ) ) , p . key , p . message , p . errorCode )
183+ return new UserError ( new Error ( p . rawError . toString ( ) ) , p . key , p . userMessage , p . errorCode )
183184 } else {
184185 return undefined
185186 }
@@ -191,17 +192,17 @@ export class UserError implements UserErrorObj {
191192 static toJSON ( e : UserErrorObj ) : string {
192193 return JSON . stringify ( {
193194 rawError : stringifyError ( e . rawError ) ,
194- message : e . message ,
195+ userMessage : e . userMessage ,
195196 key : e . key ,
196197 errorCode : e . errorCode ,
197198 } )
198199 }
199200
200201 static isUserError ( e : unknown ) : e is UserErrorObj {
201- return ! ( e instanceof Error ) && ! ! e && typeof e === 'object' && 'rawError' in e && 'message ' in e && 'key' in e
202+ return ! ! e && typeof e === 'object' && 'rawError' in e && 'userMessage ' in e && 'key' in e
202203 }
203204
204205 toErrorString ( ) : string {
205- return `${ translateMessage ( this . message , interpollateTranslation ) } \n${ stringifyError ( this . rawError ) } `
206+ return `${ translateMessage ( this . userMessage , interpollateTranslation ) } \n${ stringifyError ( this . rawError ) } `
206207 }
207208}
0 commit comments