From 5c9b4dde9ded26bb3dc730493f21c79fa7f58fd6 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Tue, 27 Feb 2024 13:48:52 +0000 Subject: [PATCH] cohttp-eio: Improve error handling in example server 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. --- CHANGES.md | 1 + cohttp-eio/examples/server1.ml | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 0beaf4d54..8aaa01936 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/cohttp-eio/examples/server1.ml b/cohttp-eio/examples/server1.ml index 4393d6071..7b0a4fd10 100644 --- a/cohttp-eio/examples/server1.ml +++ b/cohttp-eio/examples/server1.ml @@ -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 @@ -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