interceptors expects the error to be from type Error in handleRequest, which is not required by Node.
const http = require('http')
const { ClientRequestInterceptor } = require('@mswjs/interceptors/ClientRequest')
const interceptor = new ClientRequestInterceptor()
interceptor.on('request', async function rootListener({ controller }) {
controller.errorWith({ message: 'custom error', code: 'test' })
})
// interceptor.apply()
const req = http.request('http://example.com')
req.on('response', () => {
console.log('response');
})
req.on('error', e => {
console.log('error', e);
})
req.end()
If the interceptor is applied, the program prints response and throws:
/.../nock/node_modules/@mswjs/interceptors/lib/node/chunk-WTJL7BRV.js:234
if (!Reflect.has(headers, kRawHeaders)) {
^
TypeError: Reflect.has called on non-object
at Reflect.has (<anonymous>)
at getRawFetchHeaders (/home/michael/projects/js/nock/node_modules/@mswjs/interceptors/lib/node/chunk-WTJL7BRV.js:234:16)
at MockHttpSocket.respondWith (/home/michael/projects/js/nock/node_modules/@mswjs/interceptors/lib/node/chunk-WTJL7BRV.js:488:32)
at Object.onResponse (/home/michael/projects/js/nock/node_modules/@mswjs/interceptors/lib/node/chunk-WTJL7BRV.js:901:18)
at handleResponse (/home/michael/projects/js/nock/node_modules/@mswjs/interceptors/lib/node/chunk-TQD7SQGP.js:100:21)
at handleRequest (/home/michael/projects/js/nock/node_modules/@mswjs/interceptors/lib/node/chunk-TQD7SQGP.js:186:12)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async _ClientRequestInterceptor.onRequest (/home/michael/projects/js/nock/node_modules/@mswjs/interceptors/lib/node/chunk-WTJL7BRV.js:895:32)
Node.js v20.17.0
For real requests, it is called the error callback.
WDYT?
interceptorsexpects the error to be from typeErrorinhandleRequest, which is not required by Node.If the interceptor is applied, the program prints
responseand throws:For real requests, it is called the
errorcallback.WDYT?