Skip to content

Commit 875c73e

Browse files
committed
bubble up nested error reasons to the toplevel handler
`Action.process_github_notification` logs all errors raised in its subroutines.
1 parent 9d80975 commit 875c73e

File tree

4 files changed

+13
-31
lines changed

4 files changed

+13
-31
lines changed

lib/action.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
186186
let send_notifications (ctx : Context.t) notifications =
187187
let notify (chan, msg) =
188188
match Context.hook_of_channel ctx chan with
189-
| None ->
190-
log#error "webhook not defined for Slack channel '%s'" chan;
191-
Lwt.return_unit
189+
| None -> Printf.ksprintf action_error "webhook not defined for Slack channel '%s'" chan
192190
| Some url ->
193191
( match%lwt Slack_api.send_notification ~chan ~msg ~url with
194192
| Ok () -> Lwt.return_unit

lib/api_local.ml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,16 @@ let cwd = Caml.Sys.getcwd ()
77
let cache_dir = Caml.Filename.concat cwd "github-api-cache"
88

99
module Github : Api.Github = struct
10-
let log = Log.from "github"
11-
1210
let get_config ~(ctx : Context.t) ~repo:_ =
1311
let url = Caml.Filename.concat cwd ctx.config_filename in
1412
match%lwt get_local_file url with
15-
| Error e ->
16-
log#error "error while getting local file %s: %s" url e;
17-
Lwt.return @@ fmt_error "failed to get config from file %s" url
13+
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get config %s" e url
1814
| Ok res -> Lwt.return @@ Ok (Config_j.config_of_string res)
1915

2016
let get_api_commit ~ctx:_ ~repo:_ ~sha =
2117
let url = Caml.Filename.concat cache_dir sha in
2218
match%lwt get_local_file url with
23-
| Error e ->
24-
log#error "error while getting local file %s: %s" url e;
25-
Lwt.return @@ fmt_error "failed to get api commit from file %s" url
19+
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get api commit %s" e url
2620
| Ok res -> Lwt.return @@ Ok (Github_j.api_commit_of_string res)
2721
end
2822

lib/api_remote.ml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ open Devkit
44
open Common
55

66
module Github : Api.Github = struct
7-
let log = Log.from "github"
8-
97
let commits_url ~(repo : Github_t.repository) ~sha =
108
String.substr_replace_first ~pattern:"{/sha}" ~with_:sha repo.commits_url
119

@@ -21,9 +19,7 @@ module Github : Api.Github = struct
2119
let url = contents_url ~repo ~path:ctx.config_filename in
2220
let headers = build_headers ?token:secrets.gh_token () in
2321
match%lwt http_request ~headers `GET url with
24-
| Error e ->
25-
log#error "error while querying %s: %s" url e;
26-
Lwt.return @@ fmt_error "failed to get config from file %s" url
22+
| Error e -> Lwt.return @@ fmt_error "error while querying remote: %s\nfailed to get config from file %s" e url
2723
| Ok res ->
2824
let response = Github_j.content_api_response_of_string res in
2925
( match response.encoding with
@@ -33,12 +29,13 @@ module Github : Api.Github = struct
3329
response.content |> String.split_lines |> String.concat |> decode_string_pad |> Config_j.config_of_string
3430
|> fun res -> Lwt.return @@ Ok res
3531
with Base64.Invalid_char as exn ->
36-
log#error ~exn "failed to decode base64 in Github response";
37-
Lwt.return @@ fmt_error "failed to get config from file %s" url
32+
let e = Exn.to_string exn in
33+
Lwt.return
34+
@@ fmt_error "error while decoding base64 in GitHub response: %s\nfailed to get config from file %s" e url
3835
end
3936
| encoding ->
40-
log#error "unexpected encoding '%s' in Github response" encoding;
41-
Lwt.return @@ fmt_error "failed to get config from file %s" url
37+
Lwt.return
38+
@@ fmt_error "unexpected encoding '%s' in Github response\nfailed to get config from file %s" encoding url
4239
)
4340

4441
let get_api_commit ~(ctx : Context.t) ~repo ~sha =
@@ -47,9 +44,7 @@ module Github : Api.Github = struct
4744
let headers = build_headers ?token:secrets.gh_token () in
4845
match%lwt http_request ~headers `GET url with
4946
| Ok res -> Lwt.return @@ Ok (Github_j.api_commit_of_string res)
50-
| Error e ->
51-
log#error "error while querying %s: %s" url e;
52-
Lwt.return @@ fmt_error "failed to get api commit %s" sha
47+
| Error e -> Lwt.return @@ fmt_error "error while querying remote: %s\nfailed to get api commit from file %s" e url
5348
end
5449

5550
module Slack : Api.Slack = struct
@@ -62,6 +57,5 @@ module Slack : Api.Slack = struct
6257
match%lwt http_request ~body `POST url with
6358
| Ok _ -> Lwt.return @@ Ok ()
6459
| Error e ->
65-
log#error "error while querying %s: %s" url e;
66-
Lwt.return @@ fmt_error "failed to send Slack notification"
60+
Lwt.return @@ fmt_error "error while querying remote: %s\nfailed to send Slack notification to %s" e url
6761
end

lib/context.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ let refresh_secrets ctx =
5757
| Ok res ->
5858
ctx.secrets <- Some (Config_j.secrets_of_string res);
5959
Lwt.return @@ Ok ctx
60-
| Error e ->
61-
log#error "error while getting local file %s: %s" path e;
62-
Lwt.return @@ fmt_error "failed to get secrets from file %s" path
60+
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get secrets from file %s" e path
6361

6462
let refresh_state ctx =
6563
match ctx.state_filepath with
@@ -70,7 +68,5 @@ let refresh_state ctx =
7068
log#info "loading saved state from file %s" path;
7169
let state = State_j.state_of_string res in
7270
Lwt.return @@ Ok { ctx with state }
73-
| Error e ->
74-
log#error "error while getting local file %s: %s" path e;
75-
Lwt.return @@ fmt_error "failed to get state from file %s" path
71+
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path
7672
)

0 commit comments

Comments
 (0)