From f3ddab637e269e67e852ffce07b060bab2b085c0 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Wed, 12 Oct 2022 10:31:43 -0700 Subject: [PATCH] fix(errors): Allows to pass no error message (#2794) --- packages/errors/src/index.ts | 4 ++-- packages/errors/test/index.test.ts | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/errors/src/index.ts b/packages/errors/src/index.ts index 073eda1c94..cca8761fd3 100644 --- a/packages/errors/src/index.ts +++ b/packages/errors/src/index.ts @@ -8,7 +8,7 @@ export interface FeathersErrorJSON { } export type DynamicError = Error & { [key: string]: any } -export type ErrorMessage = string | DynamicError | { [key: string]: any } | any[] +export type ErrorMessage = null | string | DynamicError | { [key: string]: any } | any[] interface ErrorProperties extends Omit { type: string @@ -33,7 +33,7 @@ export class FeathersError extends Error { if (Array.isArray(_data)) { properties.data = _data } else if (typeof err === 'object' || _data !== undefined) { - const { message, errors, ...rest } = typeof err === 'object' ? err : _data + const { message, errors, ...rest } = err !== null && typeof err === 'object' ? err : _data msg = message || msg properties.errors = errors diff --git a/packages/errors/test/index.test.ts b/packages/errors/test/index.test.ts index f161148d59..e4a0263108 100644 --- a/packages/errors/test/index.test.ts +++ b/packages/errors/test/index.test.ts @@ -345,6 +345,11 @@ describe('@feathersjs/errors', () => { assert.deepStrictEqual(error.data, [{ hello: 'world' }]) }) + it('can be instantiated with `null` (#2789)', () => { + const err = new errors.BadRequest(null, {}) + assert.strictEqual(err.message, 'Error') + }) + it('has proper stack trace (#78)', () => { try { throw new errors.NotFound('Not the error you are looking for')