Skip to content

Commit

Permalink
fix: simplify isNodeExceptionLike check
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
mattcosta7 committed Nov 1, 2023
1 parent 1589e83 commit 83347ff
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/node/SetupServerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / exports

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / exports

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.7)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.7)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.8)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.8)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.9)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.9)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

Check failure on line 17 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Cannot find module './utils/isNodeException' or its corresponding type declarations.

const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
onUnhandledRequest: 'warn',
Expand Down Expand Up @@ -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'

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / build

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / build

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / exports

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / exports

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.7)

Object is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.7)

Object is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.8)

Object is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.8)

Object is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.9)

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (4.9)

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

'error' is of type 'unknown'.

Check failure on line 85 in src/node/SetupServerApi.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

'error' is of type 'unknown'.
)
) {
throw error
}
Expand Down
10 changes: 0 additions & 10 deletions src/node/utils/isNodeException.ts

This file was deleted.

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

0 comments on commit 83347ff

Please sign in to comment.