Closed osher closed 7 years ago
This is interesting, but I haven't seen putting a "log" attribute on request & response as a convention. Is this commonly used?
yes. Restify does it for sure, I saw express users do it (although it's not a part of the common infra that comes with express), I'm not sure about hapi - but I'm quite positive there is a plugin that does.
I'm on a work station now, I'll get it working shortly
um.. - I need a point in the right direction regarding the last stage of error handling.
If I get it right:
next(err)
resorts to the default error handling of the used framework, which is usually a rude 500 page with HTML content.application/json
Thereforenext(null, formattedJSONBody)
so I adjust my implementation following that understanding, I'd appreciate if you can confirm I'm right.
Also - we could use fittingDef
to customize the output more - I'd love any inputs on that...
re. how common that is:
restify - out of the box (http://restify.com/#log)
hapijs - out of the box (http://hapijs.com/tutorials/logging)
express - middleware that provides you with req.log
(https://www.npmjs.com/package/express-bunyan-logger, and https://www.npmjs.com/package/express-pino-logger, - which I only found about today, when I worked with express we did it ourselves, it's just a single little function that just req.log = logger.createLogger(...); next()
)
Fantastic. Yes, I agree with your design. This looks like a great patch. Thanks for your work on it!
Are you finished or do you want to add anything else? (You mentioned configuration above... not sure what you might have in mind.)
If you like it as it is and don't see any reason to provide output customization - then lets go.
For honesty's sake - I found one more frequent habit I do not cover which might be common among express users:
req.app.log
.
Would you like to support it? if yes - can it be in a later PR?
Ok. done. I did it too. I also arranged the order of checks by what I believe to be the most commonly used.
I would also update the README.md, but it looks you're not documenting there anything... where should I doc it? I see that the WIKI is empty too :open_mouth:
mm. one more thing
If you're aware on the performance penalty of just entering a try block - then you may want to consider starting the try at line 39 (or even try{}
only the JSON.stringify
).
let me know what you think
Documentation had been generally left to the swagger project that uses this module. (Although that probably needs to change as that project is no longer in sync with what's happening here.) That said, I've been documenting changes in the release notes.
Re: Optimization, yes, we could move the exit conditions (lines 27-38) to before the try block. Also, it appears that you could move your log assignment down to the catch block to ensure it's only defined when used.
So wierd. It fails test. We might have uncovered something else here, but this should belong to another issue.
The problematic line is the actual call to next(err)
in
if (context.statusCode === 500 && !fittingDef.handle500Errors) { return next(err); }
I added a //TODO
comment and left it in the try{}
block
Thanks!
Ah! great!!! thanks, super :smile:
I'm still not sure about line 43.
I did not touch it - but I feel there's a problem there - for the least of it, it should be
next(err)
or maybe evennext(err2)
...