Skip to content

Commit

Permalink
fix(setupServer): suppress "ERR_INVALID_ARG_TYPE" errors only
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Oct 20, 2023
1 parent c1f6b7e commit da09266
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/node/SetupServerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SharedOptions> = {
onUnhandledRequest: 'warn',
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/node/utils/isNodeException.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit da09266

Please sign in to comment.