miragesdk: cleaner errors

Signed-off-by: Thomas Gazagnaire <thomas@gazagnaire.org>
This commit is contained in:
Thomas Gazagnaire
2017-04-06 16:49:56 +02:00
parent fd447ee082
commit f44e2ffbcb
3 changed files with 31 additions and 11 deletions

View File

@@ -65,6 +65,9 @@ module Query = struct
} [@@little_endian]
]
type error = [ `Eof | `Msg of string ]
let pp_error ppf t = Fmt.string ppf (match t with `Eof -> "EOF" | `Msg s -> s)
(* to avoid warning 32 *)
let _ = hexdump_msg
let _ = string_to_operation
@@ -114,7 +117,7 @@ module Query = struct
buf
let err e = Lwt.return (Error (`Msg (Fmt.to_to_string C.pp_error e)))
let err_eof = Lwt.return (Error (`Msg "EOF"))
let err_eof = Lwt.return (Error `Eof)
let read fd =
let fd = C.create fd in
@@ -171,6 +174,9 @@ module Reply = struct
} [@@little_endian]
]
type error = [ `Eof | `Msg of string ]
let pp_error ppf t = Fmt.string ppf (match t with `Eof -> "EOF" | `Msg s -> s)
(* to avoid warning 32 *)
let _ = hexdump_msg
let _ = string_to_status
@@ -208,7 +214,7 @@ module Reply = struct
buf
let err e = Lwt.return (Result.Error (`Msg (Fmt.to_to_string C.pp_error e)))
let err_eof = Lwt.return (Result.Error (`Msg "EOF"))
let err_eof = Lwt.return (Result.Error `Eof)
let read fd =
let fd = C.create fd in
@@ -275,8 +281,9 @@ module Client = struct
Lwt.return r
with Not_found ->
Reply.read t.fd >>= function
| Error (`Msg e) ->
Log.err (fun l -> l "Got %s while waiting for a reply to %ld" e id);
| Error e ->
Log.err (fun l -> l "Got %a while waiting for a reply to %ld"
Query.pp_error e id);
loop ()
| Ok r ->
if r.id = id then Lwt.return r
@@ -361,6 +368,7 @@ module Server = struct
let cond = Lwt_condition.create () in
let rec listen () =
Query.read fd >>= function
| Error `Eof -> Lwt.return_unit
| Error (`Msg e) ->
Log.err (fun l -> l "received invalid message: %s" e);
listen ()

View File

@@ -18,6 +18,12 @@ module Query: sig
payload : string; (** Arbitrary payload. *)
}
type error = [ `Eof | `Msg of string ]
(** The type of errors. *)
val pp_error: error Fmt.t
(** [pp_error] is the pretty-printer for query errors. *)
val pp: t Fmt.t
(** [pp] is the pretty-printer for queries. *)
@@ -31,7 +37,7 @@ module Query: sig
val write: IO.flow -> t -> unit Lwt.t
(** [write fd t] writes a query message. *)
val read: IO.flow -> (t, [`Msg of string]) result Lwt.t
val read: IO.flow -> (t, error) result Lwt.t
(** [read fd] reads a query message. *)
end
@@ -60,10 +66,16 @@ module Reply: sig
val to_cstruct: t -> Cstruct.t
(** [to_cstruct t] is the serialization of [t]. *)
type error = [`Eof | `Msg of string ]
(** The type for reply errors. *)
val pp_error: error Fmt.t
(** [pp_error] is the pretty-printer for errors. *)
val write: IO.flow -> t -> unit Lwt.t
(** [write fd t] writes a reply message. *)
val read: IO.flow -> (t, [`Msg of string]) result Lwt.t
val read: IO.flow -> (t, error) result Lwt.t
(** [read fd] reads a reply message. *)
end