caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ollie Frolovs <ollie.frolovs.2012@my.bristol.ac.uk>
To: caml users <caml-list@inria.fr>
Subject: [Caml-list] Extracting exception details (Async, Cohttp, Exn)
Date: Fri, 29 Nov 2013 14:34:40 +0000	[thread overview]
Message-ID: <96620953-9991-4976-A207-7DBB32584D57@my.bristol.ac.uk> (raw)

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

             reply	other threads:[~2013-11-29 14:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-29 14:34 Ollie Frolovs [this message]
2013-12-02  3:45 ` Milan Stanojević
2013-12-02  8:55   ` Ollie Frolovs
2013-12-02 15:14     ` Milan Stanojević

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=96620953-9991-4976-A207-7DBB32584D57@my.bristol.ac.uk \
    --to=ollie.frolovs.2012@my.bristol.ac.uk \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).