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
Fix: emit restifyError event even for router errors #1420
Conversation
lib/server.js
Outdated
|
||
|
||
/** | ||
* TODO: this is relevant for CORS only. Should move this out eventually to a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we scrub TODOs from the codebase in favor of GitHub issues?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 on this idea! I think it might be useful to have a new label then, something like.. internal
for these non user facing clean up tasks/refactoring.
lib/server.js
Outdated
res.send(200); | ||
ok = true; | ||
if (err.statusCode === 404 && req.method === 'OPTIONS' && req.url === '*') { | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! This is much cleaner! 😄
We could even return the equality:
return err.statusCode === 404 && req.method === 'OPTIONS' && req.url === '*'
var r = route ? route.name : null; | ||
|
||
if (err) { | ||
// TODO: if its a 404 for OPTION method (likely a CORS | ||
// preflight), return OK. This should move into userland. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would we return 200 on an invalid preflight route?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the TODO in the optionsError()
method to reflect what's going on here. This behavior isn't actually new - just moved it out of the optionsError()
method. Here's the comment I added:
/**
* returns true if the router generated a 404 for an options request.
* TODO: this is relevant for CORS only. Should move this out eventually to a
* userland middleware? This also seems a little like overreach, as there is no
* option to opt out of this behavior today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @rprieto
lib/server.js
Outdated
@@ -889,73 +885,18 @@ Server.prototype._run = function _run(req, res, route, chain, cb) { | |||
|
|||
if (arg) { | |||
if (arg instanceof Error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not part of this PR, but doesnt arg instanceof Error === true
imply arg
? This second check seems redundant and adds a level of indentation. Same with typeof
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arg
can also be a boolean, or more specifically, a value of false
which tells restify to stop executing the handler chain.
lib/server.js
Outdated
// setting this flag will allow us to to not call cb() | ||
// automatically later in this function. | ||
emittedError = self._emitErrorEvents(req, res, route, arg, | ||
function emitErrorsDone() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we lint this? It looks like this should have an extra level of indentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! Lint doesn't complain because function invocations don't have to be indented even if they are > 1 line.
I actually prefer this style, as it gives us an extra 4 spaces for this last arg. Open to suggestions on how to better do this and still keep within 80cols though.
lib/server.js
Outdated
* emit error events when errors are encountered either while attempting to | ||
* route the request (via router) or while executing the handler chain. | ||
* @private | ||
* @function emitErrorEvents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is missing the _
prefix
|
||
// while async listeners are firing, return a boolean indicating whether or | ||
// not we had any listeners to fire. | ||
return (errEvtNames.length > 0) ? true : false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code is super clever! I dig it! 👏 poetry 👏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
724d21f
to
9823469
Compare
Did a little clean up of the _run function (scope creep!) but also added a ton of comments to better breakdown what's going on. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM 🎉
9823469
to
869a41c
Compare
// flush the err. | ||
var errResponse = function errResponse(err) { | ||
return self._emitErrorEvents(req, res, null, err, function () { | ||
res.send(err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all res.send()
requires to check res.headersSent
otherwise node will throw Can't set headers after they are sent.
Fixes #1419
This also removes the old FormatterError used in async formatters, which is no longer applicable.