express-http-problem-details icon indicating copy to clipboard operation
express-http-problem-details copied to clipboard

Map response headers

Open ighunter opened this issue 2 years ago • 0 comments

There are cases where I would like to set additional headers on a problem details response. For example:

  • After authentication failure, we should set the WWW-Authenticate header (https://datatracker.ietf.org/doc/html/rfc6750#section-3)
  • For rate-limiting, it would be nice to set the Retry-After header (which is mentioned at https://www.rfc-editor.org/rfc/rfc7807#section-4) and also X-RateLimit-Remaining, etc.

For now this is my workaround, which relies on the errors thrown or passed to next having a valid headers property, and feels a bit hacky:

app.use((err, req, res, next) => {
  if (err.headers && (typeof err.headers === 'object')) {
    for (let [key, val] of Object.entries(err.headers)) {
      res.header(key, val);
    }
  }
  next(err);
});

app.use(HttpProblemResponse({strategy}));

I think it would be better if we could somehow explicitly include response headers when mapping specific error types, such that those headers are then set on the response sent by HttpProblemDetailsMiddleware().

ighunter avatar Nov 07 '22 11:11 ighunter