Skip to content

Commit 0d2a199

Browse files
Fixed handling of some errors in paginator
1 parent b563c6b commit 0d2a199

File tree

3 files changed

+38
-32
lines changed

3 files changed

+38
-32
lines changed

lib/src/common/github.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,9 @@ class GitHub {
205205

206206
headers.putIfAbsent("Accept", () => "application/vnd.github.v3+json");
207207

208-
return request("GET", path, headers: headers, params: params).then(
208+
return request("GET", path, headers: headers, params: params,
209+
statusCode: statusCode, fail: fail).then(
209210
(response) {
210-
if (statusCode != null && statusCode != response.statusCode) {
211-
fail != null ? fail(response) : null;
212-
handleStatusCode(response);
213-
return new Future.value(null);
214-
}
215211
return convert(JSON.decode(response.body));
216212
});
217213
}
@@ -249,13 +245,9 @@ class GitHub {
249245

250246
headers.putIfAbsent("Accept", () => "application/vnd.github.v3+json");
251247

252-
return request("POST", path, headers: headers, params: params, body: body)
248+
return request("POST", path, headers: headers, params: params, body: body,
249+
statusCode: statusCode, fail: fail)
253250
.then((response) {
254-
if (statusCode != null && statusCode != response.statusCode) {
255-
fail != null ? fail(response) : null;
256-
handleStatusCode(response);
257-
return new Future.value(null);
258-
}
259251
return convert(JSON.decode(response.body));
260252
});
261253
}
@@ -278,6 +270,7 @@ class GitHub {
278270
} else if (msg == "Body should be a JSON Hash") {
279271
throw new InvalidJSON(this, msg);
280272
}
273+
else throw new BadRequest(this);
281274
break;
282275
case 422:
283276
var json = response.asJSON();
@@ -315,7 +308,9 @@ class GitHub {
315308
* [body] is the body content of requests that take content.
316309
*/
317310
Future<http.Response> request(String method, String path,
318-
{Map<String, String> headers, Map<String, dynamic> params, String body}) {
311+
{Map<String, String> headers, Map<String, dynamic> params, String body,
312+
int statusCode,
313+
void fail(http.Response response)}) {
319314
if (headers == null) headers = {};
320315

321316
if (auth.isToken) {
@@ -347,7 +342,14 @@ class GitHub {
347342
}
348343

349344
return client.request(new http.Request(url.toString(),
350-
method: method, headers: headers, body: body));
345+
method: method, headers: headers, body: body)).then((response) {
346+
if (statusCode != null && statusCode != response.statusCode) {
347+
fail != null ? fail(response) : null;
348+
handleStatusCode(response);
349+
return null;
350+
}
351+
else return response;
352+
});
351353
}
352354

353355
/**

lib/src/common/util/errors.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
part of github.common;
22

33
/// Error Generated by [GitHub]
4-
class GitHubError {
4+
class GitHubError implements Exception {
55
final String message;
66
final String apiUrl;
77
final GitHub github;
@@ -18,6 +18,10 @@ class NotFound extends GitHubError {
1818
NotFound(GitHub github, String msg) : super(github, msg);
1919
}
2020

21+
class BadRequest extends GitHubError {
22+
BadRequest(GitHub github, [String msg = 'Not Found']) : super(github, msg);
23+
}
24+
2125
/// GitHub Repository was not found
2226
class RepositoryNotFound extends NotFound {
2327
RepositoryNotFound(GitHub github, String repo)
@@ -61,7 +65,7 @@ class NotAuthenticated extends GitHubError {
6165
NotAuthenticated(GitHub github) : super(github, "Client not Authenticated");
6266
}
6367

64-
class InvalidJSON extends GitHubError {
68+
class InvalidJSON extends BadRequest {
6569
InvalidJSON(GitHub github, [String message = "Invalid JSON"])
6670
: super(github, message);
6771
}

lib/src/common/util/pagination.dart

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ class PaginationHelper<T> {
77
PaginationHelper(this.github);
88

99
Future<List<http.Response>> fetch(String method, String path, {int pages,
10-
Map<String, String> headers, Map<String, dynamic> params, String body}) {
10+
Map<String, String> headers, Map<String, dynamic> params, String body,
11+
int statusCode: 200}) {
1112
var completer = new Completer();
1213
var responses = [];
1314
if (headers == null) headers = {};
1415
Future<http.Response> actualFetch(String realPath) {
1516
return github.request(method, realPath,
16-
headers: headers, params: params, body: body);
17+
headers: headers, params: params, body: body, statusCode: statusCode);
1718
}
1819

1920
void done() => completer.complete(responses);
@@ -54,7 +55,7 @@ class PaginationHelper<T> {
5455

5556
Stream<http.Response> fetchStreamed(String method, String path, {int pages,
5657
bool reverse: false, int start, Map<String, String> headers,
57-
Map<String, dynamic> params, String body}) {
58+
Map<String, dynamic> params, String body, int statusCode: 200}) {
5859
if (headers == null) headers = {};
5960
var controller = new StreamController.broadcast();
6061

@@ -70,13 +71,12 @@ class PaginationHelper<T> {
7071

7172

7273
return github.request(method, realPath,
73-
headers: headers, params: p, body: body);
74+
headers: headers, params: p, body: body, statusCode: statusCode);
7475
}
7576

7677
var count = 0;
7778

78-
var handleResponse;
79-
handleResponse = (http.Response response) {
79+
handleResponse(http.Response response) {
8080
count++;
8181
controller.add(response);
8282

@@ -113,29 +113,29 @@ class PaginationHelper<T> {
113113
} else {
114114
handleResponse(response);
115115
}
116+
}).catchError((e, s) {
117+
controller.addError(e, s);
118+
controller.close();
116119
});
117120

118121
return controller.stream;
119122
}
120123

121124
Stream<T> objects(String method, String path, JSONConverter converter,
122125
{int pages, bool reverse: false, int start, Map<String, String> headers,
123-
Map<String, dynamic> params, String body}) {
126+
Map<String, dynamic> params, String body, int statusCode: 200}) {
124127
if (headers == null) headers = {};
125128
headers.putIfAbsent("Accept", () => "application/vnd.github.v3+json");
126-
var controller = new StreamController();
127-
fetchStreamed(method, path,
129+
return fetchStreamed(method, path,
128130
pages: pages,
129131
start: start,
130132
reverse: reverse,
131133
headers: headers,
132134
params: params,
133-
body: body).listen((response) {
134-
var json = response.asJSON();
135-
for (var item in json) {
136-
controller.add(converter(item));
137-
}
138-
}).onDone(() => controller.close());
139-
return controller.stream;
135+
body: body,
136+
statusCode: statusCode).expand((response) {
137+
var json = response.asJSON();
138+
return (json as List).map(converter).toList(growable:false);
139+
});
140140
}
141141
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy