You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HTTP Server: incorrect error message if status code is set to a string (like ENOENT) #9027
If you have app code that does something dumb like copying err.code to the http status code, and err.code happens to be, for example, "ENOENT", the error message that is thrown is:
RangeError: Invalid status code: 0
at ServerResponse.writeHead (_http_server.js:192:11)
at ServerResponse.writeHead (/home/vcap/app/node_modules/on-headers/index.js:55:19)
at ServerResponse._implicitHeader (_http_server.js:157:8)
at ServerResponse.OutgoingMessage.end (_http_outgoing.js:559:10)
at ServerResponse.send (/home/vcap/app/node_modules/express/lib/response.js:205:10)
at ServerResponse.json (/home/vcap/app/node_modules/express/lib/response.js:250:15)
at app.use (/home/vcap/app/config/error-handler.js:38:28)
at Layer.handle_error (/home/vcap/app/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/home/vcap/app/node_modules/express/lib/router/index.js:310:13)
A better error message in this situation would be
RangeError: Invalid status code: ENOENT
I'll have a PR with a patch and a test for this shortly.
I found most libraries will to custom HTTP Error to cover that.
Like below:
functionCustomError(code,message){this.code=codethis.status=this.statusCode=codeinhttp.STATUS_CODES ? code : 500// if you wantthis.message=message||http.STATUS_CODES[this.status]this.name=this.constructor.nameError.captureStackTrace(this,this.constructor)}
@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.
Activity
fundon commentedon Oct 11, 2016
I found most libraries will to custom
HTTP Errorto cover that.Like below:
Is there better practice?
nfriedly commentedon Jan 11, 2017
@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 commentedon Mar 1, 2017
Fixed in a4bb9fd