mtrudel / bandit

Bandit is a pure Elixir HTTP server for Plug & WebSock applications
MIT License
1.7k stars 86 forks source link

Attempt to improve plug call exception handling and logging #398

Closed grzuy closed 1 month ago

grzuy commented 2 months ago

Supersedes #396.

mtrudel commented 2 months ago

A couple of changes requested to keep things consistent and explicit; other than those changes the rest of this looks great!

grzuy commented 2 months ago

Fixed format.

Can't reproduce locally the protocol_test.exs failures happening here, though.

grzuy commented 2 months ago

@mtrudel Just realized that Bandit was originally taking the approach of re-raising plug call errors in 1.4 instead of just "logging or silence" with changes in 1.5 with https://github.com/mtrudel/bandit/pull/342/files#diff-68626306df11d3e10964941997c6cdafbf587a9ec7de087141840655a47a1855L65.

Curious about the reason not to continue re-raising?

Similar to what plug_cowboy does...

In case there's the possibility of introducing re-raise again, this pull request could be re-worked.

grzuy commented 2 months ago

Reading this thread (https://groups.google.com/g/elixir-lang-core/c/pWz-uTVMEVM/m/QGXncxD1AQAJ) also got me thinking about it.

mtrudel commented 2 months ago

Similar to what plug_cowboy does...

Interesting! It seems like they're using exactly the 'catch :error types' convention I raised above, such that exceptions are in fact re-raising. I think when I'd read the plug_cowboy implementation originally I saw the catch and assumed that handling was only for idiomatic throw/catch usage (which the current Bandit implementation handles).

The main purpose of that PR was to ensure that logging of errors takes into consideration the accompanying result code (ie: only log errors that get raised with a 500..599 response code). This generally means that we do the logging ourselves (as we do in that PR) instead of reraising and letting the runtime log it

All of which is to say that I think we're doing the right thing by not re-raising?

grzuy commented 2 months ago

Mmm... not sure to he honest.

I'm exploring the "re-raising approach" in a separate branch to compare.

I'll let you know how it goes.

grzuy commented 2 months ago

I'm exploring the "re-raising approach" in a separate branch to compare.

Opening draft with what I got from playing with that alternative approach: https://github.com/mtrudel/bandit/pull/400.

grzuy commented 1 month ago

Closing in favor smaller and more focused #411 for now. Will revisit and build on top of #411 if that one accepted and merged.