mist icon indicating copy to clipboard operation
mist copied to clipboard

Error reporting in handler requests

Open rockerBOO opened this issue 1 year ago • 3 comments

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}}">>

rockerBOO avatar Dec 10 '23 20:12 rockerBOO

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!

rawhat avatar Dec 10 '23 21:12 rawhat

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 avatar Dec 12 '23 22:12 rockerBOO

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.

rockerBOO avatar Dec 13 '23 00:12 rockerBOO