Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Handling Middleware #311

Closed
danieladams456 opened this issue Nov 14, 2018 · 5 comments
Closed

Error Handling Middleware #311

danieladams456 opened this issue Nov 14, 2018 · 5 comments

Comments

@danieladams456
Copy link

Is this a feature request?

Yes

http-proxy events forces you to handle writing the response within the handler onError(err, req, res) instead of allowing you to pass it to an Express error handling middleware.

Motivation: I have a standard Express middleware that handles error response formatting for the rest of the app (authentication errors, etc). My app is simple and that logic is just a few lines of code, but it would be nice especially for more complex apps not to have to duplicate that logic. I am also using NewRelic APM and it can more easily deduce errors when they flow through an Express error handling middleware. Again, nothing a few lines of manual code couldn't fix, but the ability to delegate to an error handling middleware would be ideal.

I'm not sure if there is a clean way of handling this without monkey-patching or something since http-proxy wasn't designed to be tied to Express.

Steps to reproduce

  1. There is no next argument for a next(err) call in the http-proxy onError event function signature.

Expected behavior

Feature request to investigate whether there is a clean solution to extend that function call signature with another argument that would let the remainder of the request flow through Express' standard error handling middleware.

Actual behavior

http-proxy-middleware must fully write the response, including HTTP headers, etc.

Setup

  • http-proxy-middleware: 0.19.0
  • http-proxy-middleware configuration: see below
  • server: express 4.16.4
const app = express();
app.use(healthMiddleware);
app.use(authMiddleware);
app.use(proxy({target: PROXY_TARGET, onError}));
app.use(errorMiddleware);
@chimurai
Copy link
Owner

Sounds interesting @danieladams456.

I'll have to investigate this.
Do you have a proposal for a generic solution?

@danieladams456
Copy link
Author

danieladams456 commented Nov 27, 2018

I'm not sure how the stack/scoping works out, but theoretically could next be bound to the 4th/last argument in the onError function using lodash bind? (credit) I see lodash is already a dependency.

@jardakotesovec
Copy link

Having next available in onError sounds good to me. I am also struggling with similar use case, where the failures inside the proxy were not caught with my error handler.

@OliverJAsh
Copy link

Potentially related: webpack/webpack-dev-server#2380

@hpl002
Copy link

hpl002 commented Jan 6, 2021

Any update on this?

I too have other downstream middleware that is not triggered because the response is ended in the proxy instead of being passed along.

Repository owner locked and limited conversation to collaborators Apr 18, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants