rawhat / mist

gleam HTTP server. because it glistens on a web
Apache License 2.0
333 stars 12 forks source link

Error reporting in handler requests #31

Open rockerBOO opened 11 months ago

rockerBOO commented 11 months ago

When a handler crashes, this can be somewhat ideal at times. But ideally not regularly.

For example errors report as:

<<"Caught error in websocket handler">>

But maybe we can put the reason in the error string. This would help a lot in figuring out what is causing the error.

fn handle_ws_message(state: Socket, conn, message) {
  case message {
    mist.Binary(json) -> {
      case decode_type(message) {
         Ok(RatingType("rating")) -> {
              let assert Ok(_) = mist.send_binary_frame(conn, <<image:utf8>>)
          }
      }
    }
  ...
}
import gleam/erlang

logger.error(string.concat(["Caught error in websocket handler: ", erlang.format(reason)]))

<<"Caught error in websocket handler: {errored,{case_clause,{ok,{rating_type,<<\"rating\">>}}}}">>

Would make it a lot easier to find these errors, even if it requires translating the erlang response. I would also love the ability to turn on stack traces but I did some poking around and couldn't find support in gleam for them.

Thank you for reading.

Edit:

Another example. Maybe it could be cleaned up a little more or handled differently.

<<"Caught error in websocket handler: {errored,#{function => <<\"handle_json_message\">>,line => 123,\n           message => <<\"Assertion pattern match failed\">>,\n           module => <<\"image_scorer\">>,\n           value => {error,{bit_decode,<<\"Could not decode base64\">>}},\n           gleam_error => let_assert}}">>
rawhat commented 11 months ago

I like that idea, and pretty easy to extend what I have with that.

=ERROR REPORT==== 10-Dec-2023::16:21:51.975168 ===
<<"Caught error in websocket handler: {errored,#{function => <<\"main\">>,line => 25,\n           message => <<\"Assertion pattern match failed\">>,\n           module => <<\"ws_test\">>,\n           value => {error,nil},\n           gleam_error => let_assert}}">>

Just pushed this, can do a release for this in the near future. Thank you!

rockerBOO commented 11 months ago

Sweet! I have been using it in my current build and it helps quite a bit :). Though I think the formatting could be improved but not sure if you wanted to go in that direction of parsing the formatted string (at least inferring the new lines as new log lines or something)

rockerBOO commented 11 months ago

2023-12-12_19-46

For example, in gleam tests, I get this error format. I know, in error logging, it might not be ideal though.