interceptors icon indicating copy to clipboard operation
interceptors copied to clipboard

Suppress errors after request handler

Open mikicho opened this issue 1 year ago • 1 comments

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 avatar Oct 26 '23 15:10 mikicho

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

mikicho avatar Dec 17 '23 20:12 mikicho