Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace exceptions by result types for the requests #11

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## unreleased

### Changed

- Replace exceptions by result types for the requests (#<PR_NUMBER>, @gpetiot)
gpetiot marked this conversation as resolved.
Show resolved Hide resolved
+ `Graphql.exec` now returns `_ result Lwt.t`
+ `Contributions.fetch` now returns `_ result`

## 0.2.0

### Added
Expand Down
13 changes: 8 additions & 5 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down Expand Up @@ -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 =
Expand Down
5 changes: 4 additions & 1 deletion lib/contributions.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
Expand Down
19 changes: 12 additions & 7 deletions lib/graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "@[<v2>GitHub returned errors: %a@]"
(Yojson.Safe.pretty_print ~std:true)
json)
Error
(`Msg
(Format.asprintf "@[<v2>GitHub returned errors: %a@]"
(Yojson.Safe.pretty_print ~std:true)
json)))
| err ->
Fmt.failwith "@[<v2>Error performing GraphQL query on GitHub: %s@,%s@]"
(Cohttp.Code.string_of_status err)
body
Error
(`Msg
(Format.asprintf
"@[<v2>Error performing GraphQL query on GitHub: %s@,%s@]"
(Cohttp.Code.string_of_status err)
body))
2 changes: 1 addition & 1 deletion lib/graphql.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading