Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise and reraise exceptions with Stdlib rather than Lwt. #430

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/conduit-lwt-unix/conduit_lwt_launchd.dummy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
*
*)

let activate _fn _name = Lwt.fail_with "No Launchd support"
let activate _fn _name = failwith "No Launchd support"
2 changes: 1 addition & 1 deletion src/conduit-lwt-unix/conduit_lwt_launchd.real.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ let activate fn name =
Lwt_launchd.activate_socket name >>= fun sockets ->
match Launchd.error_to_msg sockets with
| Ok sockets -> Lwt_list.iter_p fn sockets
| Error (`Msg m) -> Lwt.fail_with m
| Error (`Msg m) -> failwith m
2 changes: 1 addition & 1 deletion src/conduit-lwt-unix/conduit_lwt_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let with_socket sockaddr f =
(fun () -> f fd)
(fun e ->
Lwt.catch (fun () -> Lwt_unix.close fd) (fun _ -> Lwt.return_unit)
>>= fun () -> Lwt.fail e)
>>= fun () -> Lwt.reraise e)

let listen ?(backlog = 128) sa =
with_socket sa (fun fd ->
Expand Down
8 changes: 4 additions & 4 deletions src/conduit-lwt-unix/conduit_lwt_tls.dummy.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module X509 = struct
let private_of_pems ~cert:_ ~priv_key:_ = Lwt.fail_with "Tls not available"
let private_of_pems ~cert:_ ~priv_key:_ = failwith "Tls not available"

type authenticator = unit

Expand All @@ -8,15 +8,15 @@ end

module Client = struct
let connect ?src:_ ?certificates:_ ~authenticator:_ _host _sa =
Lwt.fail_with "Tls not available"
failwith "Tls not available"
end

module Server = struct
let init' ?backlog:_ ?stop:_ ?timeout:_ _tls _sa _callback =
Lwt.fail_with "Tls not available"
failwith "Tls not available"

let init ?backlog:_ ~certfile:_ ~keyfile:_ ?stop:_ ?timeout:_ _sa _callback =
Lwt.fail_with "Tls not available"
failwith "Tls not available"
end

let available = false
2 changes: 1 addition & 1 deletion src/conduit-lwt-unix/conduit_lwt_tls.real.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module Server = struct
(fun t ->
let ic, oc = Tls_lwt.of_t t in
Lwt.return (fd, ic, oc))
(fun exn -> Lwt_unix.close fd >>= fun () -> Lwt.fail exn)
(fun exn -> Lwt_unix.close fd >>= fun () -> Lwt.reraise exn)
>>= Conduit_lwt_server.process_accept ?timeout (callback addr))

let init ?backlog ~certfile ~keyfile ?stop ?timeout sa callback =
Expand Down
33 changes: 15 additions & 18 deletions src/conduit-lwt-unix/conduit_lwt_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ let init ?src ?(tls_own_key = `None)
>>= function
| { ai_addr; _ } :: _ ->
Lwt.return { no_source_ctx with src = Some ai_addr }
| [] -> Lwt.fail_with "Invalid conduit source address specified")
| [] -> failwith "Invalid conduit source address specified")

module Sockaddr_io = struct
let shutdown_no_exn fd mode =
Expand Down Expand Up @@ -267,7 +267,7 @@ let connect_with_tls_native ~ctx (`Hostname hostname, `IP ip, `Port port) =
(match ctx.tls_own_key with
| `None -> Lwt.return_none
| `TLS (_, _, `Password _) ->
Lwt.fail_with "OCaml-TLS cannot handle encrypted pem files"
failwith "OCaml-TLS cannot handle encrypted pem files"
| `TLS (`Crt_file_path cert, `Key_file_path priv_key, `No_password) ->
Conduit_lwt_tls.X509.private_of_pems ~cert ~priv_key
>|= fun certificate -> Some (`Single certificate))
Expand Down Expand Up @@ -311,7 +311,7 @@ let connect_with_default_tls ~ctx tls_client_config =
match !tls_library with
| OpenSSL -> connect_with_openssl ~ctx tls_client_config
| Native -> connect_with_tls_native ~ctx tls_client_config
| No_tls -> Lwt.fail_with "No SSL or TLS support compiled into Conduit"
| No_tls -> failwith "No SSL or TLS support compiled into Conduit"

(** Main connection function *)

Expand All @@ -329,9 +329,8 @@ let connect ~ctx (mode : client) =
| `TLS c -> connect_with_default_tls ~ctx c
| `OpenSSL c -> connect_with_openssl ~ctx c
| `TLS_native c -> connect_with_tls_native ~ctx c
| `Vchan_direct _ -> Lwt.fail_with "Vchan_direct not available on unix"
| `Vchan_domain_socket _uuid ->
Lwt.fail_with "Vchan_domain_socket not implemented"
| `Vchan_direct _ -> failwith "Vchan_direct not available on unix"
| `Vchan_domain_socket _uuid -> failwith "Vchan_domain_socket not implemented"

let sockaddr_on_tcp_port ctx port =
let open Unix in
Expand All @@ -354,7 +353,7 @@ let serve_with_tls_native ?timeout ?stop ~ctx ~certfile ~keyfile ~pass ~port
let sockaddr, _ = sockaddr_on_tcp_port ctx port in
(match pass with
| `No_password -> Lwt.return ()
| `Password _ -> Lwt.fail_with "OCaml-TLS cannot handle encrypted pem files")
| `Password _ -> failwith "OCaml-TLS cannot handle encrypted pem files")
>>= fun () ->
Conduit_lwt_tls.Server.init ~certfile ~keyfile ?timeout ?stop sockaddr
(fun addr fd ic oc -> callback (flow_of_fd fd addr) ic oc)
Expand Down Expand Up @@ -401,9 +400,8 @@ let serve ?backlog ?timeout ?stop ~on_exn ~(ctx : ctx) ~(mode : server) callback
(`Crt_file_path certfile, `Key_file_path keyfile, pass, `Port port) ->
serve_with_tls_native ?timeout ?stop ~ctx ~certfile ~keyfile ~pass ~port
callback
| `Vchan_direct _ -> Lwt.fail_with "Vchan_direct not implemented"
| `Vchan_domain_socket _uuid ->
Lwt.fail_with "Vchan_domain_socket not implemented"
| `Vchan_direct _ -> failwith "Vchan_direct not implemented"
| `Vchan_domain_socket _uuid -> failwith "Vchan_domain_socket not implemented"
| `Launchd name ->
let fn s = Sockaddr_server.init ~on:(`Socket s) ?timeout ?stop callback in
Conduit_lwt_launchd.activate fn name
Expand All @@ -426,23 +424,22 @@ let endp_to_client ~ctx:_ (endp : Conduit.endp) : client Lwt.t =
| `TLS (host, `TCP (ip, port)) ->
Lwt.return (`TLS (`Hostname host, `IP ip, `Port port))
| `TLS (host, endp) ->
Lwt.fail_with
(Printf.sprintf "TLS to non-TCP currently unsupported: host=%s endp=%s"
host
(Sexplib0.Sexp.to_string_hum (Conduit.sexp_of_endp endp)))
| `Unknown err -> Lwt.fail_with ("resolution failed: " ^ err)
Printf.ksprintf failwith
"TLS to non-TCP currently unsupported: host=%s endp=%s" host
(Sexplib0.Sexp.to_string_hum (Conduit.sexp_of_endp endp))
| `Unknown err -> failwith ("resolution failed: " ^ err)

let endp_to_server ~ctx (endp : Conduit.endp) =
match endp with
| `Unix_domain_socket path -> Lwt.return (`Unix_domain_socket (`File path))
| `TLS (_host, `TCP (_ip, port)) -> (
match ctx.tls_own_key with
| `None -> Lwt.fail_with "No TLS server key configured"
| `None -> failwith "No TLS server key configured"
| `TLS (`Crt_file_path crt, `Key_file_path key, pass) ->
Lwt.return
(`TLS (`Crt_file_path crt, `Key_file_path key, pass, `Port port)))
| `TCP (_ip, port) -> Lwt.return (`TCP (`Port port))
| `Vchan_direct _ as mode -> Lwt.return mode
| `Vchan_domain_socket _ as mode -> Lwt.return mode
| `TLS (_host, _) -> Lwt.fail_with "TLS to non-TCP currently unsupported"
| `Unknown err -> Lwt.fail_with ("resolution failed: " ^ err)
| `TLS (_host, _) -> failwith "TLS to non-TCP currently unsupported"
| `Unknown err -> failwith ("resolution failed: " ^ err)
4 changes: 2 additions & 2 deletions src/conduit-lwt-unix/conduit_lwt_unix_ssl.dummy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Client = struct

let connect ?(ctx = default_ctx) ?src:_ ?hostname:_ ?ip:_ ?verify:_ _sa =
ignore ctx;
Lwt.fail_with "Ssl not available"
failwith "Ssl not available"
end

module Server = struct
Expand All @@ -36,7 +36,7 @@ module Server = struct
let init ?(ctx = default_ctx) ?backlog:_ ?password:_ ~certfile:_ ~keyfile:_
?stop:_ ?timeout:_ _sa _cb =
ignore ctx;
Lwt.fail_with "Ssl not available"
failwith "Ssl not available"
end

let available = false
2 changes: 1 addition & 1 deletion src/conduit-lwt-unix/conduit_lwt_unix_ssl.real.ml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ module Server = struct
Lwt.try_bind
(fun () -> Lwt_ssl.ssl_accept fd ctx)
(fun sock -> Lwt.return (chans_of_fd sock))
(fun exn -> Lwt_unix.close fd >>= fun () -> Lwt.fail exn)
(fun exn -> Lwt_unix.close fd >>= fun () -> Lwt.reraise exn)
>>= Conduit_lwt_server.process_accept ?timeout (cb addr))
end

Expand Down
2 changes: 1 addition & 1 deletion src/conduit-lwt-unix/resolver_lwt_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let system_service name =
let tls = is_tls_service name in
let svc = { Resolver.name; port = s.Lwt_unix.s_port; tls } in
Lwt.return (Some svc))
(function Not_found -> Lwt.return_none | e -> Lwt.fail e)
(function Not_found -> Lwt.return_none | e -> Lwt.reraise e)

let static_service name =
match Uri_services.tcp_port_of_service name with
Expand Down
5 changes: 2 additions & 3 deletions src/conduit-mirage/conduit_mirage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open Sexplib0.Sexp_conv

let ( >>= ) = Lwt.( >>= )
let ( >|= ) = Lwt.( >|= )
let fail fmt = Fmt.kstr (fun s -> Lwt.fail (Failure s)) fmt
let fail fmt = Fmt.failwith fmt
let err_tcp_not_supported = fail "%s: TCP is not supported"
let err_tls_not_supported = fail "%s: TLS is not supported"

Expand Down Expand Up @@ -103,8 +103,7 @@ module TCP (S : Tcpip.Stack.V4V6) = struct
type t = S.t

let err_tcp e =
Lwt.fail
@@ Failure (Format.asprintf "TCP connection failed: %a" S.TCP.pp_error e)
Format.kasprintf failwith "TCP connection failed: %a" S.TCP.pp_error e

let connect (t : t) (c : client) =
match c with
Expand Down
6 changes: 2 additions & 4 deletions src/conduit-mirage/conduit_xenstore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type direct = [ `Direct of int * Vchan.Port.t ]

let ( >>= ) = Lwt.( >>= )
let ( / ) = Filename.concat
let fail fmt = Printf.ksprintf (fun m -> Lwt.fail (Failure m)) fmt
let fail fmt = Printf.ksprintf failwith fmt
let err_peer_not_found = fail "Conduit_xenstore: %s peer not found"

let err_no_entry_found () =
Expand All @@ -48,9 +48,7 @@ module Make (Xs : Xs_client_lwt.S) = struct
let readdir h d =
Xs.(directory h d) >>= fun dirs ->
let dirs = List.filter (fun p -> p <> "") dirs in
match dirs with
| [] -> Lwt.fail Xs_protocol.Eagain
| hd :: _ -> Lwt.return hd
match dirs with [] -> raise Xs_protocol.Eagain | hd :: _ -> Lwt.return hd

let register name =
Xs.make () >>= fun xs ->
Expand Down
Loading