-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
HTTP Server: incorrect error message if status code is set to a string (like ENOENT) #9027
Labels
http
Issues or PRs related to the http subsystem.
Comments
I found most libraries will to custom Like below: function CustomError (code, message) {
this.code = code
this.status = this.statusCode = code in http.STATUS_CODES ? code : 500 // if you want
this.message = message || http.STATUS_CODES[this.status]
this.name = this.constructor.name
Error.captureStackTrace(this, this.constructor)
} Is there better practice? |
3 tasks
3 tasks
@fundon That looks good to me, but it's kind of orthogonal to this issue. Node.js still should give helpful error messages when the user (or library) gets something wrong. |
nfriedly
added a commit
to nfriedly/node
that referenced
this issue
Jan 17, 2017
HTTP ServerResponse.writeHead previously coerced the given statusCode to an integer to validate it, then threw an error with the converted number if it was invalid. This meant that non-numeric input led to confusing error messages. This change makes the input validation more strict, throwing on any non-integer input rather than silently coercing it. Of note: * Strings (e.g. '200') were previously accepted, now they cause a TypeError * Numbers with decimals (e.g. 200.6) were previously floored, now they cause a RangeError (with the original value included in the error message). Fixes nodejs#9027
Fixed in a4bb9fd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/nodejs/node/blob/master/lib/_http_server.js#L190 converts the http status code to a number and then validates that it's within an appropriate range, throwing an error if it is not. However the error message includes the converted value rather than the original value.
If you have app code that does something dumb like copying
err.code
to the http status code, anderr.code
happens to be, for example,"ENOENT"
, the error message that is thrown is:A better error message in this situation would be
I'll have a PR with a patch and a test for this shortly.
The text was updated successfully, but these errors were encountered: