[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [MirageOS-devel] Error handling in Mirage - request for comments!
> System 3 provides (I think) no easy way to match on the type of error. You can. An Error.t is constructed by using Sexplib. Thus, you can deserialize them to whatever specific error you want to consider. Here's an example: open Core.Std;; type myerr = [`Zero | `Neg of int] with sexp;; (* Get back to myerr if possible. *) let myerr_of_error (e:Error.t) : myerr option =  match Error.sexp_of_t e with  | Sexp.List (_::x::[]) -> Some (myerr_of_sexp x)  | _ -> None ;; let f x : int Or_error.t =  if x = 0 then error "zero not allowed" `Zero sexp_of_error  else if x < 0 then error "negative not allowed" (`Neg x) sexp_of_error  else Ok x ;; let g x = match f x with | Ok x -> x*x | Error e -> match myerr_of_error e with  | Some `Zero -> 0  | Some (`Neg x) -> x*x  | None -> 0 (* or parse e as another error type *) ;; In the last None clause above, you could try to parse e as another error type, continuing to consider as many error types as you want. The nice thing about this is you only have one error type, Error.t, so your APIs remain uncluttered. But you can deserialize to more strongly typed error types when you want to. The bad thing is the compiler can't tell you what are all the errors you have to consider, but this isn't achieved my most error handling strategies. Furthermore, this is all about errors, so I'm not sure exhaustiveness is so important. > Sometimes it's useful to organise errors into a hierarchy ... I don't see any way to do this in OCaml. You can get this with polymorphic variants, e.g type tcp_connection_refused = [`TCP_connection_refused] type tcp_error = [tcp_connection_refused | `Another_err] On Tue, Feb 3, 2015 at 12:26 PM, Sebastien Mondet <sebastien.mondet@xxxxxxxxx> wrote:
_______________________________________________ MirageOS-devel mailing list MirageOS-devel@xxxxxxxxxxxxxxxxxxxx http://lists.xenproject.org/cgi-bin/mailman/listinfo/mirageos-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |