Skip to content

Commit

Permalink
cohttp-eio: Improve error handling in example server
Browse files Browse the repository at this point in the history
The example was using `~on_error:raise`, which causes the server to
crash if any error occurs handling a connection. This is unlikely to be
what you want. Instead, log the error and continue.

This also now matches the behaviour of the Lwt example, which just logs
errors and continues.
  • Loading branch information
talex5 committed Feb 27, 2024
1 parent 6ed9bae commit 5c9b4dd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

- cohttp-eio: Improve error handling in example server (talex5 #1023)
- cohttp-eio: Don't blow up `Server.callback` on client disconnections. (mefyl #1015)
- http: Fix assertion in `Source.to_string_trim` when `pos <> 0` (mefyl #1017)

Expand Down
4 changes: 3 additions & 1 deletion cohttp-eio/examples/server1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ let handler _socket request _body =
Eio.Flow.string_source text )
| _ -> (Http.Response.make ~status:`Not_found (), Cohttp_eio.Body.of_string "")

let log_warning ex = Logs.warn (fun f -> f "%a" Eio.Exn.pp ex)

let () =
let port = ref 8080 in
Arg.parse
Expand All @@ -53,4 +55,4 @@ let () =
Eio.Net.listen env#net ~sw ~backlog:128 ~reuse_addr:true
(`Tcp (Eio.Net.Ipaddr.V4.loopback, !port))
and server = Cohttp_eio.Server.make ~callback:handler () in
Cohttp_eio.Server.run socket server ~on_error:raise
Cohttp_eio.Server.run socket server ~on_error:log_warning

0 comments on commit 5c9b4dd

Please sign in to comment.