diff --git a/src/node/SetupServerApi.ts b/src/node/SetupServerApi.ts index 291c66feb..a451f521b 100644 --- a/src/node/SetupServerApi.ts +++ b/src/node/SetupServerApi.ts @@ -14,6 +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' const DEFAULT_LISTEN_OPTIONS: RequiredDeep = { onUnhandledRequest: 'warn', @@ -67,7 +68,7 @@ export class SetupServerApi Math.max(defaultMaxListeners, this.currentHandlers.length), request.signal, ) - } catch (e: unknown) { + } catch (error: unknown) { /** * @note Mock environments (JSDOM, ...) are not able to implement an internal * "kIsNodeEventTarget" Symbol that Node.js uses to identify Node.js `EventTarget`s. @@ -78,6 +79,11 @@ export class SetupServerApi * The main reason for using `setMaxListeners` is to suppress these warnings in Node.js, * which won't be printed anyway if `setMaxListeners` fails. */ + if ( + !(isNodeException(error) && error.code === 'ERR_INVALID_ARG_TYPE') + ) { + throw error + } } } diff --git a/src/node/utils/isNodeException.ts b/src/node/utils/isNodeException.ts new file mode 100644 index 000000000..268e5b8a5 --- /dev/null +++ b/src/node/utils/isNodeException.ts @@ -0,0 +1,10 @@ +/** + * 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 instanceof Error && 'code' in error +}