From 83347ffe2d2f01ae57ea548812e46a874e114c27 Mon Sep 17 00:00:00 2001 From: Matthew Costabile Date: Tue, 31 Oct 2023 21:31:16 +0000 Subject: [PATCH] fix: simplify isNodeExceptionLike check in some environments, particularly jsdom with jest, Error objects may not be `instanceof Error` despite being similar and used in the same way. To avoid these issues we only need to validate the shape and properties that might be thrown. In some cases these could false positive, however it's less likely that they'll false positive, than it is that jest/jsdom will throw these objects _like_ the errors they represent (guaranteed) --- src/node/SetupServerApi.ts | 7 +++++-- src/node/utils/isNodeException.ts | 10 ---------- src/node/utils/isNodeExceptionLike.ts | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 12 deletions(-) delete mode 100644 src/node/utils/isNodeException.ts create mode 100644 src/node/utils/isNodeExceptionLike.ts diff --git a/src/node/SetupServerApi.ts b/src/node/SetupServerApi.ts index a451f521b..4eb026a6a 100644 --- a/src/node/SetupServerApi.ts +++ b/src/node/SetupServerApi.ts @@ -14,7 +14,7 @@ import { mergeRight } from '~/core/utils/internal/mergeRight' import { handleRequest } from '~/core/utils/handleRequest' import { devUtils } from '~/core/utils/internal/devUtils' import { SetupServer } from './glossary' -import { isNodeException } from './utils/isNodeException' +import { isNodeExceptionLike } from './utils/isNodeException' const DEFAULT_LISTEN_OPTIONS: RequiredDeep = { onUnhandledRequest: 'warn', @@ -80,7 +80,10 @@ export class SetupServerApi * which won't be printed anyway if `setMaxListeners` fails. */ if ( - !(isNodeException(error) && error.code === 'ERR_INVALID_ARG_TYPE') + !( + isNodeExceptionLike(error) && + error.code === 'ERR_INVALID_ARG_TYPE' + ) ) { throw error } diff --git a/src/node/utils/isNodeException.ts b/src/node/utils/isNodeException.ts deleted file mode 100644 index d66686ede..000000000 --- a/src/node/utils/isNodeException.ts +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Determines if the given value is a Node.js exception. - * Node.js exceptions have additional information, like - * the `code` and `errno` properties. - */ -export function isNodeException( - error: unknown, -): error is NodeJS.ErrnoException { - return !!error && typeof error === 'object' && 'code' in error -} diff --git a/src/node/utils/isNodeExceptionLike.ts b/src/node/utils/isNodeExceptionLike.ts new file mode 100644 index 000000000..65b6af6a3 --- /dev/null +++ b/src/node/utils/isNodeExceptionLike.ts @@ -0,0 +1,14 @@ +/** + * Determines if the given value is shaped like a Node.js exception. + * Node.js exceptions have additional information, like + * the `code` and `errno` properties. + * + * In some environments, particularly jsdom/jest these may not + * instances of `Error` or its subclasses, despite being similar + * to them. + */ +export function isNodeExceptionLike( + error: unknown, +): error is NodeJS.ErrnoException { + return !!error && typeof error === 'object' && 'code' in error +}