koa-proxies icon indicating copy to clipboard operation
koa-proxies copied to clipboard

Error callback blocks all custom error handlers

Open whenlit opened this issue 2 years ago • 0 comments

Hi, thanks a lot for this library. It makes my life easier.

One thing though: when anything goes wrong in proxying, let's say because the domain cannot be found, any error messages are eaten by the callback starting in line 109:

proxy.web(ctx.req, ctx.res, httpProxyOpts, e => {
  const status = {
    ECONNREFUSED: 503,
    ETIMEOUT: 504
  }[e.code]
  ctx.status = status || 500
  resolve()
})

Because the http-proxy package does not emit the error event when a callback is specified, there is no way to get to those error messages in your package.

Here is the relevant bit from http-proxy/lib/http-proxy/passes/web-incoming.js (clb === callback):

if (clb) {
  clb(err, req, res, url);
} else {
  server.emit('error', err, req, res, url);
}

So, it would be nice if koa-proxies would call the error handler manually if it is specified. I hope you agree. I'll add a PR shortly.

whenlit avatar Jun 28 '22 11:06 whenlit