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

Add Service.fail_lwt convenience function #229

Merged
merged 1 commit into from
Apr 27, 2021
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
6 changes: 6 additions & 0 deletions capnp-rpc-lwt/capnp_rpc_lwt.mli
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ module Service : sig

val fail : ?ty:Capnp_rpc.Exception.ty -> ('a, Format.formatter, unit, 'b StructRef.t) format4 -> 'a
(** [fail msg] is an exception with reason [msg]. *)

val fail_lwt :
?ty:Capnp_rpc.Exception.ty ->
('a, Format.formatter, unit, (_, [> `Capnp of Capnp_rpc.Error.t]) Lwt_result.t) format4 ->
'a
(** [fail_lwt msg] is like [fail msg], but can be used with [return_lwt]. *)
end

(**/**)
Expand Down
8 changes: 6 additions & 2 deletions capnp-rpc-lwt/service.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ let return_empty () =
return @@ Response.create_empty ()

(* A convenient way to implement a simple blocking local function, where
pipelining is not supported (further messages will be queued up at this
host until it returns). *)
pipelining is not supported (messages sent to the result promise will be
queued up at this host until it returns). *)
let return_lwt fn =
let result, resolver = Local_struct_promise.make () in
Lwt.async (fun () ->
Expand All @@ -81,3 +81,7 @@ let return_lwt fn =
result

let fail = Core_types.fail

let fail_lwt ?ty fmt =
fmt |> Fmt.kstr @@ fun msg ->
Lwt_result.fail (`Capnp (`Exception (Capnp_rpc.Exception.v ?ty msg)))