mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
535 stars 120 forks source link

Suppress errors after request handler #474

Closed mikicho closed 1 month ago

mikicho commented 10 months ago

We don't suppress any error after the MockLookupEnd state. It seems like Node still emits some errors that we might want to suppress or avoid somehow.

const { ClientRequestInterceptor } = require('@mswjs/interceptors/ClientRequest')
const http = require('http');

const interceptor = new ClientRequestInterceptor({
  name: 'my-interceptor',
})
interceptor.apply();
interceptor.on('request', async ({ request }) => {
  throw new Error('a')
});

http.get('http://example.text/')
  .on('error', e => {
    console.log(e)
  })

prints two errors:

Error: a at _Emitter. (/..../nock/a.js:9:9) at emitAsync (/.../nock/node_modules/@mswjs/interceptors/lib/node/chunk-5PTPJLB7.js:50:20) at /.../nock/node_modules/@mswjs/interceptors/lib/node/chunk-YVNH3GJ5.js:290:40 at until (/.../nock/node_modules/@open-draft/until/lib/index.js:29:24) at _NodeClientRequest.end (/.../nock/node_modules/@mswjs/interceptors/lib/node/chunk-YVNH3GJ5.js:284:18) at Object.interceptorsHttpGet [as get] (/.../nock/node_modules/@mswjs/interceptors/lib/node/chunk-YVNH3GJ5.js:804:14) at Object. (/.../nock/a.js:12:6) at Module._compile (node:internal/modules/cjs/loader:1241:14) at Module._extensions..js (node:internal/modules/cjs/loader:1295:10) at Module.load (node:internal/modules/cjs/loader:1091:32)

Error: socket hang up at connResetException (node:internal/errors:720:14) at Socket.socketCloseListener (node:_http_client:468:25) at Socket.emit (node:events:526:35) at TCP. (node:net:337:12) { code: 'ECONNRESET' }

Another case I get:

getaddrinfo ENOTFOUND example.test

mikicho commented 8 months ago

Another example here: https://github.com/mswjs/interceptors/issues/492#issue-2044075571