Skip to content

Commit e7eaccf

Browse files
committed
chore: make UserError extend Error
1 parent 9b22594 commit e7eaccf

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

packages/corelib/src/error.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/job-worker/src/__mocks__/_extendJest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ expect.extend({
1414

1515
const pass =
1616
received instanceof UserError &&
17-
received.message.key === expectedError.message.key &&
18-
(args === undefined || JSON.stringify(args) === JSON.stringify(received.message.args))
17+
received.userMessage.key === expectedError.userMessage.key &&
18+
(args === undefined || JSON.stringify(args) === JSON.stringify(received.userMessage.args))
1919

2020
return {
2121
message: () =>
2222
`expected ${JSON.stringify(
23-
received instanceof UserError ? received.message : received
24-
)} to match ${JSON.stringify(expectedError.message)}`,
23+
received instanceof UserError ? received.userMessage : received
24+
)} to match ${JSON.stringify(expectedError.userMessage)}`,
2525
pass: pass,
2626
}
2727
},

packages/meteor-lib/src/api/__tests__/client.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('ClientAPI', () => {
2222
expect(error).toMatchObject({
2323
error: {
2424
key: UserErrorMessage.InactiveRundown,
25-
message: {
25+
userMessage: {
2626
args: mockArgs,
2727
key: 'Rundown must be active!',
2828
},
@@ -37,7 +37,7 @@ describe('ClientAPI', () => {
3737
expect(error).toMatchObject({
3838
error: {
3939
key: UserErrorMessage.InternalError,
40-
message: {
40+
userMessage: {
4141
args: mockArgs,
4242
key: 'An internal error occured!',
4343
},

0 commit comments

Comments
 (0)