caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Milan Stanojević" <milanst@gmail.com>
To: Ollie Frolovs <ollie.frolovs.2012@my.bristol.ac.uk>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Extracting exception details (Async, Cohttp, Exn)
Date: Sun, 1 Dec 2013 22:45:40 -0500	[thread overview]
Message-ID: <CAKR7PS-obzWahpYT4ALrMMCSohYoSfCrrPjjVO3XkukQ4MK0RQ@mail.gmail.com> (raw)
In-Reply-To: <96620953-9991-4976-A207-7DBB32584D57@my.bristol.ac.uk>

On Fri, Nov 29, 2013 at 9:34 AM, Ollie Frolovs
<ollie.frolovs.2012@my.bristol.ac.uk> wrote:
> 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?
>

There is no "description" field in Exn type.
The confusion here is that the exception you see is actually a wrapped
async exception. When async catches an exception, it wraps it up in a
different exception with more information for easier debugging.
Its definition is (you can see it at
https://github.com/janestreet/async_core/blob/master/lib/monitor.ml#L55)

module Exn_for_monitor = struct
  type t =
    { exn : exn;
      backtrace : string sexp_list;
      backtrace_history : Backtrace.t sexp_list;
      monitor : monitor;
    }
  with fields, sexp_of
end

exception Error_ of Exn_for_monitor.t with sexp

And that is what you see in your output.
To get the original exception you can use Monitor.extract_exn but that
gives you less information (but it might be ok in your particular
case)

> 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.

utop has some special support for async but I'm not sure exactly what
it does but I doubt it delays running a function.

In general when you have a function in async that returns a deferred,
you do kick off code that will eventually fill in the deferred that
you got back. It is not the case that only when you use that deferred
something happens. Just wanted to point this out to avoid
misconceptions about async.


> 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?
>

As I said before, this is an issue with async wrapping the original
exception with some more data and then raising the new exception.

Exn.to_string gives you a sexp in a string precisely to avoid
duplication of effort. Otherwise we would need to write to_string and
sexp_of_t for each exception (basically having two preprocessors, one
for to_string and one for sexp). This way you just define your
exception and add "with sexp" and you get all the data in exception
when it's raised.

Hope this helps,
   Milan

  reply	other threads:[~2013-12-02  3:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-29 14:34 Ollie Frolovs
2013-12-02  3:45 ` Milan Stanojević [this message]
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=CAKR7PS-obzWahpYT4ALrMMCSohYoSfCrrPjjVO3XkukQ4MK0RQ@mail.gmail.com \
    --to=milanst@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=ollie.frolovs.2012@my.bristol.ac.uk \
    /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).