(Thanks for reporting an issue! Please make sure you click the link above to view the issue guidelines, then fill out the blanks below.)
NOTE: There seems to be no link above...
I think the @middy/http-error-handler should respect the expose property of http-errors.
I am happy to fix this if wanted.
expose=false in middleware or handlerThe response includes the error message
The response to not include the error message
From the http-errors docs:
expose - can be used to signal if message should be sent to the client, defaulting to false when status >= 500
Not relevant
I think this is a great idea. If you can put a PR together, that would be awesome. I can port it to release/v2.x, or we can have this included in v2.x only. I'll leave it up to you.
If I were to jump on this @willfarrell is this where I should be looking [https://github.com/middyjs/middy/tree/master/packages/http-error-handler] ?
What I am pushing will look like this;
http-errors uses standard error codes from native Node module)I've pushed a change to the v2 branch w/ tests. Let me know if this is what you had in mind: https://github.com/middyjs/middy/blob/release/2.x/packages/http-error-handler/index.js
Yes, what I did is something like this but on version one.
Something I want to point out is this line, shouldn't the logic here be < 500?
expose should be true for anything less than a 500 error code
I believe that should mean
if (handler.error?.statusCode && handler.error?.expose === undefined) {
handler.error.expose = handler.error.statusCode < 500
}
instead of
if (handler.error?.statusCode && handler.error?.expose === undefined) {
handler.error.expose = handler.error.statusCode >= 500
}
or am I missing something
There is also the possibility to check if it is a httpError, instead of checking for expose:
var createError = require('http-errors')
var error = createError(404, err, { expose: false })
createError.isHttpError(error) // true
I my code I did something along the lines of
Just some thoughts, no need to incorporate...
I think there is a typing for that, you can check the accompanying index.d.ts file @raae 馃檪
if (handler.error?.statusCode) {
handler.error.expose =
!handler.error?.expose
? false
: handler.error.statusCode < 500
}
I think something like this should pass.
Also it won't be a bad idea to incorporate @raae 's idea at very beginning of the httpErrorHandlerMiddlewareOnError function
@AyoAlfonso Good catch, I've added in more tests and fixed the logic.
@raae Not including http-error is by design, though I was tempted to last night. By not including it, the codebase size stays small and allows for custom errors that may not strictly follow all requirements of the http-error pattern.
I've also updated the docs to reflect the breaking change to errors with statusCode >= 500. Thanks for your help in adding this in.
Ref:
Most helpful comment
@AyoAlfonso Good catch, I've added in more tests and fixed the logic.
@raae Not including
http-erroris by design, though I was tempted to last night. By not including it, the codebase size stays small and allows for custom errors that may not strictly follow all requirements of thehttp-errorpattern.I've also updated the docs to reflect the breaking change to errors with statusCode >= 500. Thanks for your help in adding this in.
Ref: