Skip to content

Commit 0e5443e

Browse files
committed
log failed requests as errors
1 parent 6d55b5e commit 0e5443e

File tree

5 files changed

+70
-21
lines changed

5 files changed

+70
-21
lines changed

source/dlangbot/appveyor.d

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ import vibe.core.log;
1313
void cancelBuild(string repoSlug, size_t buildId)
1414
{
1515
import std.format : format;
16-
import vibe.http.client : requestHTTP;
1716
import vibe.http.common : HTTPMethod;
1817
import vibe.stream.operations : readAllUTF8;
18+
import dlangbot.utils : request;
1919

2020
auto url = "%s/builds/%s/%s/cancel".format(appveyorAPIURL, repoSlug, buildId);
21-
requestHTTP(url, (scope req) {
21+
request(url, (scope req) {
2222
req.headers["Authorization"] = appveyorAuth;
2323
req.method = HTTPMethod.DELETE;
2424
}, (scope res) {
2525
if (res.statusCode / 100 == 2)
2626
logInfo("[appveyor/%s]: Canceled Build %s\n", repoSlug, buildId);
2727
else
28-
logWarn("[appveyor/%s]: POST %s failed; %s %s.\n%s", repoSlug, url, res.statusPhrase,
28+
logError("[appveyor/%s]: POST %s failed; %s %s.\n%s", repoSlug, url, res.statusPhrase,
2929
res.statusCode, res.bodyReader.readAllUTF8);
3030
});
3131
}
@@ -38,7 +38,7 @@ void dedupAppVeyorBuilds(string action, string repoSlug, uint pullRequestNumber)
3838
import std.format : format;
3939
import std.range : drop;
4040
import vibe.data.json : Json;
41-
import vibe.http.client : requestHTTP;
41+
import dlangbot.utils : request;
4242

4343
if (action != "synchronize" && action != "merged")
4444
return;
@@ -51,7 +51,7 @@ void dedupAppVeyorBuilds(string action, string repoSlug, uint pullRequestNumber)
5151
// GET /api/projects/{accountName}/{projectSlug}/history?recordsNumber={records-per-page}[&startBuildId={buildId}&branch={branch}]
5252

5353
auto url = "%s/projects/%s/history?recordsNumber=100".format(appveyorAPIURL, repoSlug);
54-
auto activeBuildsForPR = requestHTTP(url, (scope req) {
54+
auto activeBuildsForPR = request(url, (scope req) {
5555
req.headers["Authorization"] = appveyorAuth;
5656
})
5757
.readJson["builds"][]

source/dlangbot/bugzilla.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ struct Issue
6262
Issue[] getDescriptions(R)(R issueRefs)
6363
{
6464
import std.csv;
65-
import vibe.http.client : requestHTTP;
6665
import vibe.stream.operations : readAllUTF8;
66+
import dlangbot.utils : request;
6767

6868
if (issueRefs.empty)
6969
return null;
7070
return "%s/buglist.cgi?bug_id=%(%d,%)&ctype=csv&columnlist=short_desc,bug_status,resolution,bug_severity,priority"
7171
.format(bugzillaURL, issueRefs.map!(r => r.id))
72-
.requestHTTP
72+
.request
7373
.bodyReader.readAllUTF8
7474
.csvReader!Issue(null)
7575
.array

source/dlangbot/github_api.d

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ import std.typecons : Nullable;
1111

1212
import vibe.core.log;
1313
import vibe.data.json;
14-
import vibe.http.client : HTTPClientRequest, requestHTTP;
14+
import vibe.http.client : HTTPClientRequest;
1515
public import vibe.http.common : HTTPMethod;
1616
import vibe.stream.operations : readAllUTF8;
1717

18+
import dlangbot.utils : request;
19+
1820
auto ghGetRequest(string url)
1921
{
20-
return requestHTTP(url, (scope req) {
22+
return request(url, (scope req) {
2123
req.headers["Authorization"] = githubAuth;
2224
});
2325
}
2426

2527
auto ghGetRequest(scope void delegate(scope HTTPClientRequest req) userReq, string url)
2628
{
27-
return requestHTTP(url, (scope req) {
29+
return request(url, (scope req) {
2830
req.headers["Authorization"] = githubAuth;
2931
userReq(req);
3032
});
@@ -33,7 +35,7 @@ auto ghGetRequest(scope void delegate(scope HTTPClientRequest req) userReq, stri
3335
auto ghSendRequest(scope void delegate(scope HTTPClientRequest req) userReq, string url)
3436
{
3537
HTTPMethod method;
36-
requestHTTP(url, (scope req) {
38+
request(url, (scope req) {
3739
req.headers["Authorization"] = githubAuth;
3840
userReq(req);
3941
method = req.method;
@@ -43,9 +45,6 @@ auto ghSendRequest(scope void delegate(scope HTTPClientRequest req) userReq, str
4345
logInfo("%s %s, %s\n", method, url, res.statusPhrase);
4446
res.bodyReader.readAllUTF8;
4547
}
46-
else
47-
logWarn("%s %s failed; %s %s.\n%s", method, url,
48-
res.statusPhrase, res.statusCode, res.bodyReader.readAllUTF8);
4948
});
5049
}
5150

source/dlangbot/trello.d

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ string trelloAPIURL = "https://api.trello.com";
44
string trelloSecret, trelloAuth;
55

66
import dlangbot.bugzilla : Issue, IssueRef;
7+
import dlangbot.utils : request;
78
import std.algorithm, std.range;
89
import std.format : format;
910

1011
import vibe.core.log;
1112
import vibe.data.json;
12-
import vibe.http.client : requestHTTP;
1313
import vibe.http.common : HTTPMethod;
1414
import vibe.stream.operations : readAllUTF8;
1515

@@ -20,7 +20,7 @@ import vibe.stream.operations : readAllUTF8;
2020
void trelloSendRequest(T...)(HTTPMethod method, string url, T arg)
2121
if (T.length <= 1)
2222
{
23-
requestHTTP(url, (scope req) {
23+
request(url, (scope req) {
2424
req.method = method;
2525
static if (T.length)
2626
req.writeJsonBody(arg);
@@ -29,7 +29,7 @@ void trelloSendRequest(T...)(HTTPMethod method, string url, T arg)
2929
logInfo("%s %s: %s\n", method, url.replace(trelloAuth, "key=[hidden]&token=[hidden]")
3030
, res.statusPhrase);
3131
else
32-
logWarn("%s %s: %s %s.\n%s", method, url.replace(trelloAuth, "key=[hidden]&token=[hidden]"),
32+
logError("%s %s: %s %s.\n%s", method, url.replace(trelloAuth, "key=[hidden]&token=[hidden]"),
3333
res.statusPhrase, res.statusCode, res.bodyReader.readAllUTF8);
3434
});
3535
}
@@ -79,7 +79,7 @@ auto findTrelloCards(int issueID)
7979
{
8080

8181
return trelloAPI("/1/search?query=name:\"Issue %d\"", issueID)
82-
.requestHTTP
82+
.request
8383
.readJson["cards"][]
8484
.map!(c => TrelloCard(c["id"].get!string, issueID));
8585
}
@@ -89,7 +89,7 @@ struct Comment { string url, body_; }
8989
Comment getTrelloBotComment(string cardID)
9090
{
9191
auto res = trelloAPI("/1/cards/%s/actions?filter=commentCard", cardID)
92-
.requestHTTP
92+
.request
9393
.readJson[]
9494
.find!(c => c["memberCreator"]["username"] == "dlangbot");
9595
if (res.length)
@@ -102,10 +102,10 @@ Comment getTrelloBotComment(string cardID)
102102
void moveCardToList(string cardID, string listName)
103103
{
104104
auto card = trelloAPI("/1/cards/%s", cardID)
105-
.requestHTTP
105+
.request
106106
.readJson;
107107
auto lists = trelloAPI("/1/board/%s/lists", card["idBoard"].get!string)
108-
.requestHTTP
108+
.request
109109
.readJson[];
110110

111111
immutable curListName = lists.find!(c => c["id"].get!string == card["idList"].get!string)

source/dlangbot/utils.d

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,58 @@ module dlangbot.utils;
22

33
import dlangbot.app : runAsync;
44

5+
import vibe.http.client : HTTPClientRequest, HTTPClientResponse;
6+
57
import std.datetime : Duration;
68

9+
HTTPClientResponse request(
10+
string url,
11+
scope void delegate(scope HTTPClientRequest) requester = cast(void delegate(scope HTTPClientRequest req))null
12+
) @safe
13+
{
14+
import vibe.core.log : logError;
15+
import vibe.http.client : requestHTTP;
16+
import vibe.http.common : HTTPMethod;
17+
18+
HTTPMethod method;
19+
auto res = requestHTTP(
20+
url,
21+
(scope req) {
22+
if (requester !is null)
23+
requester(req);
24+
method = req.method;
25+
});
26+
if (res.statusCode / 100 != 2)
27+
logError("%s %s failed; %s %s.", method, url, res.statusPhrase, res.statusCode);
28+
return res;
29+
}
30+
31+
void request(
32+
string url,
33+
scope void delegate(scope HTTPClientRequest) requester,
34+
scope void delegate(scope HTTPClientResponse) responder
35+
) @safe
36+
{
37+
import vibe.core.log : logError;
38+
import vibe.http.client : requestHTTP;
39+
import vibe.http.common : HTTPMethod;
40+
41+
HTTPMethod method;
42+
requestHTTP(
43+
url,
44+
(scope req) {
45+
requester(req);
46+
method = req.method;
47+
},
48+
(scope res) {
49+
if (res.statusCode / 100 != 2)
50+
logError("%s %s failed; %s %s.", method, url, res.statusPhrase, res.statusCode);
51+
responder(res);
52+
}
53+
);
54+
55+
}
56+
757
auto runTaskHelper(Fun, Args...)(Fun fun, auto ref Args args)
858
{
959
import std.functional : toDelegate;

0 commit comments

Comments
 (0)