caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Extracting exception details (Async, Cohttp, Exn)
@ 2013-11-29 14:34 Ollie Frolovs
  2013-12-02  3:45 ` Milan Stanojević
  0 siblings, 1 reply; 4+ messages in thread
From: Ollie Frolovs @ 2013-11-29 14:34 UTC (permalink / raw)
  To: caml users

Hello list!

I’m designing a function to query a web service and i am stuck trying to implement quality error handling/reporting. I have a specific question – how do i extract the “description” field from the Core’s Exn type?

I’ve been experimenting in the top-level and i got stuck. My workflow with step by step explanations is below.


First, i create a request, which i believe does not run just yet because it is “deferred”.

utop # let result = try_with (fun () -> Cohttp_async.Client.get (Uri.of_string "http://127.0.0.2"));;
val result : (Cohttp.Response.t * string Pipe.Reader.t, exn) Result.t Deferred.t = <abstr>

Then, i “Deferred.map" the result at which point the Cohttp/Async library try to connect and (as desired) fail. 
I’m using ident here because i want to see what’s inside the exception, thankfully the pretty-printer for exceptions is quite good.

utop # result >>| ident;;
- : (Cohttp.Response.t * string Pipe.Reader.t, exn) Result.t =  Core.Std.Result.Error                                                            (lib/monitor.ml.Error_                                                        
((exn ("connection attempt timeout" 127.0.0.2:80)) (backtrace (""))
(monitor
(((name try_with) (here ()) (id 553) (has_seen_error true)
 (someone_is_listening true) (kill_index 0))
((name main) (here ()) (id 1) (has_seen_error false)
 (someone_is_listening false) (kill_index 0))))))

So i could have pattern-matched Error x where x has type exn (exception). But how do i extract the exception description which is “connection timeout attempt"? I would like to be able to do something like this

utop # result >>| function Error e -> Error (Exn.description_string e) | Ok x -> Ok (do_some_magic x);;

So at this point my future function would return Result.t, string for Error and something else for Ok. 

I had a look at Core’s documentation for Exn but its to_string returns the whole sexp converted into string so i am stuck :’(

As a side question for someone familiar with Core – why does Exn.to_string return sexp in a string at all, it also has sexp_of_t, so i suppose one could evaluate Sexp.to_string @@ Exn.sexp_of_t e. I am surprised that i do not see a  function to extract the valuable description field from the exception and yet there is this apparent duplication of effort. Or am i talking non-sense?

Kind regards,

Ollie

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-12-02 15:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-29 14:34 [Caml-list] Extracting exception details (Async, Cohttp, Exn) Ollie Frolovs
2013-12-02  3:45 ` Milan Stanojević
2013-12-02  8:55   ` Ollie Frolovs
2013-12-02 15:14     ` Milan Stanojević

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).