From 1b5e4128c7266d1c534aeafe25278b054604d826 Mon Sep 17 00:00:00 2001 From: Jon Ludlam Date: Wed, 23 Nov 2016 10:46:22 +0000 Subject: [PATCH] Add a functor to help define errors Signed-off-by: Jon Ludlam --- example/example3_idl.ml | 8 ++------ lib/idl.ml | 10 ++++++++++ lib/idl.mli | 4 ++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/example/example3_idl.ml b/example/example3_idl.ml index 491c46a4..30b04f9e 100644 --- a/example/example3_idl.ml +++ b/example/example3_idl.ml @@ -36,13 +36,9 @@ type blocklist = { type error = | Unimplemented of string [@@deriving rpcty] -exception Exn of error -let error = Idl.Error.{ - def=error; - raiser=(fun e -> raise (Exn e)); - matcher=(function | Exn e -> Some e | _ -> None); - } +module E = Idl.Error.Make(struct type t=error let t = error end) +let error = E.error type domain = string [@@deriving rpcty] diff --git a/lib/idl.ml b/lib/idl.ml index a28b60ec..165fa256 100644 --- a/lib/idl.ml +++ b/lib/idl.ml @@ -22,6 +22,16 @@ module Error = struct raiser : 'a -> exn; matcher : exn -> 'a option; } + + module Make(T : sig type t val t : t Rpc.Types.def end) = struct + exception Exn of T.t + let error = { + def = T.t; + raiser = (function e -> Exn e); + matcher = (function | Exn e -> Some e | _ -> None) + } + end + end module Interface = struct diff --git a/lib/idl.mli b/lib/idl.mli index 07da30bf..9aa84343 100644 --- a/lib/idl.mli +++ b/lib/idl.mli @@ -29,6 +29,10 @@ module Error : sig raiser : 'a -> exn; matcher : exn -> 'a option; } + + module Make(T : sig type t val t : t Rpc.Types.def end) : sig + val error : T.t t + end end (** An interface is a collection of RPC declarations. *)