Skip to content

Commit

Permalink
Merge pull request #229 from talex5/error
Browse files Browse the repository at this point in the history
Add `Service.fail_lwt` convenience function
  • Loading branch information
talex5 authored Apr 27, 2021
2 parents 2b3225a + 2f574a0 commit c42b461
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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)))

0 comments on commit c42b461

Please sign in to comment.