From 0433303c95f4a4d30af9ba3ed3b780947b8e6c08 Mon Sep 17 00:00:00 2001 From: "Guillaume \"Liam\" Petiot" Date: Thu, 7 Mar 2024 16:15:02 +0000 Subject: [PATCH] Replace exceptions by result types for the requests (#11) --- CHANGES.md | 8 ++++++++ bin/main.ml | 13 ++++++++----- lib/contributions.mli | 5 ++++- lib/graphql.ml | 19 ++++++++++++------- lib/graphql.mli | 2 +- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 7321338..8ac7838 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,11 @@ +## unreleased + +### Changed + +- Replace exceptions by result types for the requests (#11, @gpetiot) + + `Graphql.exec` now returns `_ result Lwt.t` + + `Contributions.fetch` now returns `_ result` + ## 0.2.0 ### Added diff --git a/bin/main.ml b/bin/main.ml index e42e3fb..860066a 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -8,6 +8,8 @@ let or_die = function Fmt.epr "%s@." m; exit 1 +let ( let* ) x y = y @@ or_die x + let home = match Sys.getenv_opt "HOME" with | None -> Fmt.failwith "$HOME is not set!" @@ -73,13 +75,14 @@ let run period : unit = | `Normal -> Period.with_period period ~last_fetch_file ~f:(fun period -> (* Fmt.pr "period: %a@." Fmt.(pair string string) period; *) - let token = get_token () |> or_die in - show ~from:(fst period) @@ Contributions.fetch ~period ~token) + let* token = get_token () in + let* contributions = Contributions.fetch ~period ~token in + show ~from:(fst period) contributions) | `Save -> Period.with_period period ~last_fetch_file ~f:(fun period -> - let token = get_token () |> or_die in - Contributions.fetch ~period ~token - |> Yojson.Safe.to_file "activity.json") + let* token = get_token () in + let* contributions = Contributions.fetch ~period ~token in + Yojson.Safe.to_file "activity.json" contributions) | `Load -> (* When testing formatting changes, it is quicker to fetch the data once and then load it again for each test: *) let from = diff --git a/lib/contributions.mli b/lib/contributions.mli index c3e7bb3..2377546 100644 --- a/lib/contributions.mli +++ b/lib/contributions.mli @@ -15,7 +15,10 @@ module Repo_map : Map.S with type key = string type t = { username : string; activity : item list Repo_map.t } -val fetch : period:string * string -> token:Token.t -> Yojson.Safe.t +val fetch : + period:string * string -> + token:Token.t -> + (Yojson.Safe.t, [ `Msg of string ]) result val of_json : from:string -> Yojson.Safe.t -> t (** We pass [from] again here so we can filter out anything that GitHub included by accident. *) diff --git a/lib/graphql.ml b/lib/graphql.ml index 01a5eda..4bfed1c 100644 --- a/lib/graphql.ml +++ b/lib/graphql.ml @@ -21,12 +21,17 @@ let exec ?variables ~token ~query () = | `OK -> ( let json = Yojson.Safe.from_string body in match json / "errors" with - | `Null -> json + | `Null -> Ok json | _errors -> - Fmt.failwith "@[GitHub returned errors: %a@]" - (Yojson.Safe.pretty_print ~std:true) - json) + Error + (`Msg + (Format.asprintf "@[GitHub returned errors: %a@]" + (Yojson.Safe.pretty_print ~std:true) + json))) | err -> - Fmt.failwith "@[Error performing GraphQL query on GitHub: %s@,%s@]" - (Cohttp.Code.string_of_status err) - body + Error + (`Msg + (Format.asprintf + "@[Error performing GraphQL query on GitHub: %s@,%s@]" + (Cohttp.Code.string_of_status err) + body)) diff --git a/lib/graphql.mli b/lib/graphql.mli index fc457bb..57899fb 100644 --- a/lib/graphql.mli +++ b/lib/graphql.mli @@ -3,4 +3,4 @@ val exec : token:string -> query:string -> unit -> - Yojson.Safe.t Lwt.t + (Yojson.Safe.t, [ `Msg of string ]) result Lwt.t