# Error handling with \`ocaml-github\`

**URL:** <https://discuss.ocaml.org/t/error-handling-with-ocaml-github/4478>\
**Category:** Learning\
**Tags:** monads, github\
**Created:** [October 2, 2019, 10:08am UTC](https://discuss.ocaml.org/t/error-handling-with-ocaml-github/4478 "2019-10-02T10:08:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![phaer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/phaer/32/1800_2.png) [@phaer](https://discuss.ocaml.org/u/phaer)\
**Post date:** [October 2, 2019, 10:08am UTC](https://discuss.ocaml.org/t/error-handling-with-ocaml-github/4478/1 "2019-10-02T10:08:13Z")

</div>

Hi,

I am trying to use [https://github.com/mirage/ocaml-github/](https://github.com/mirage/ocaml-github/) and it works fine for the happy path, but I am having some trouble catching exceptions and assume that I misunderstand something about `ocaml-github`s monadic interface?

Both compile, both work fine when given a real commits sha, but both are seemingly failing in catching the exception for invalid commit shas; I’d except an exception to be printed, preceeded by "ERROR ", but the output I get is just  
`Fatal error: exception GitHub API error: 422 Unprocessable Entity (WebDAV) (RFC 4918) -- No commit found for SHA: not-a-real-commit` - without my prefix.

I tried using `Github.Monad.catch` as in

```ocaml
let t =
  let open Github.Monad in
  let token = Github.Token.of_string "my-token"
  and user = "ocaml"
  and repo = "opam"
  in
  run Github_t.(
      catch
        (fun () ->
           let sha = "not-a-real-commit" in
           Github.Repo.get_commit ~token ~user ~repo ~sha ()
           >>~ fun commit ->
           return (Printf.eprintf "commit url: %s\n" commit.commit_url))
        (function
          | e -> return (Printf.eprintf "ERROR %s\n" (Printexc.to_string e)))
    )

let () =
  Lwt_main.run t

```

As well as a conventional try-catch-clause:

```ocaml
let t =
  let open Github.Monad in
  let token = Github.Token.of_string "my-token"
  and user = "ocaml"
  and repo = "opam"
  in
  run Github_t.( 
     try
        let sha = "not-a-real-commit" in
        Github.Repo.get_commit ~token ~user ~repo ~sha ()
        >>~ fun commit ->
        return (Printf.eprintf "commit url: %s\n" commit.commit_url)
      with
      | e -> return (Printf.eprintf "ERROR %s\n" (Printexc.to_string e))
    )

let () =
  Lwt_main.run t

```

Thanks for reading, looking forward to any pointers 🙂
