Skip to content

Commit 551f0fc

Browse files
committed
Add some more error handling and documentation.
1 parent 0523049 commit 551f0fc

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

lib/src/client/errors.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ class OrganizationNotFound extends GitHubError {
2626

2727
class TeamNotFound extends GitHubError {
2828
TeamNotFound(GitHub github, int id) : super(github, "Team Not Found: ${id}");
29+
}
30+
31+
class AccessForbidden extends GitHubError {
32+
AccessForbidden(GitHub github) : super(github, "Client not Authenticated");
33+
}
34+
35+
class UnknownError extends GitHubError {
36+
UnknownError(GitHub github) : super(github, "Unknown Error");
2937
}

lib/src/client/github.dart

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,24 @@ class GitHub {
109109
/**
110110
* Fetches the teams for the organization specified by [name].
111111
*/
112-
Future<List<Team>> teams(String name) {
112+
Future<List<Team>> teams(String name, [int limit]) {
113113
var group = new FutureGroup<Team>();
114114
getJSON("/orgs/${name}/teams").then((teams) {
115115
for (var team in teams) {
116-
group.add(getJSON(team['url'], convert: Team.fromJSON, statusCode: 200, fail: (error) {
117-
throw new TeamNotFound(this, team['id']);
116+
group.add(getJSON(team['url'], convert: Team.fromJSON, statusCode: 200, fail: (http.Response response) {
117+
if (response.statusCode == 404) {
118+
throw new TeamNotFound(this, team['id']);
119+
}
120+
throw new UnknownError(this);
118121
}));
119122
}
120123
});
121124
return group.future;
122125
}
123126

127+
/**
128+
* Renders Markdown from the [input].
129+
*/
124130
Future<String> renderMarkdown(String input, {String mode: "markdown", String context}) {
125131
return request("POST", "/markdown", body: JSON.encode({
126132
"text": input,
@@ -131,10 +137,16 @@ class GitHub {
131137
});
132138
}
133139

140+
/**
141+
* Gets .gitignore template names.
142+
*/
134143
Future<List<String>> gitignoreTemplates() {
135144
return getJSON("/gitignore/templates");
136145
}
137146

147+
/**
148+
* Gets a .gitignore template by [name].
149+
*/
138150
Future<GitignoreTemplate> gitignoreTemplate(String name) {
139151
return getJSON("/gitignore/templates/${name}", convert: GitignoreTemplate.fromJSON);
140152
}
@@ -148,28 +160,47 @@ class GitHub {
148160
});
149161
}
150162

163+
/**
164+
* Gets a Repositories Releases
165+
*/
151166
Future<List<Release>> releases(RepositorySlug slug, [int limit = 30]) {
152167
return getJSON("/repos/${slug.fullName}/releases", params: { "per_page": limit }).then((releases) {
153168
return copyOf(releases.map((it) => Release.fromJSON(this, it)));
154169
});
155170
}
156171

172+
/**
173+
* Fetches a GitHub Release.
174+
*/
157175
Future<Release> release(RepositorySlug slug, int id) {
158176
return getJSON("/repos/${slug.fullName}/releases/${id}", convert: Release.fromJSON);
159177
}
160178

179+
/**
180+
* Gets API Rate Limit Information
181+
*/
161182
Future<RateLimit> rateLimit() {
162183
return request("GET", "/").then((response) {
163184
return RateLimit.fromHeaders(response.headers);
164185
});
165186
}
166187

188+
/**
189+
* Gets the Currently Authenticated User
190+
*
191+
* Throws [AccessForbidden] if we are not authenticated.
192+
*/
167193
Future<CurrentUser> currentUser() {
168-
return getJSON("/user", statusCode: 200, fail: (response) {
169-
throw "Not Authenticated";
194+
return getJSON("/user", statusCode: 200, fail: (http.Response response) {
195+
if (response.statusCode == 403) {
196+
throw new AccessForbidden(this);
197+
}
170198
}, convert: CurrentUser.fromJSON);
171199
}
172200

201+
/**
202+
* Handles Get Requests that respond with JSON
203+
*/
173204
Future<dynamic> getJSON(String path, {int statusCode, void fail(http.Response response), Map<String, String> headers, Map<String, String> params, JSONConverter convert}) {
174205
if (convert == null) {
175206
convert = (github, input) => input;
@@ -184,6 +215,9 @@ class GitHub {
184215
});
185216
}
186217

218+
/**
219+
* Handles Post Requests that respond with JSON
220+
*/
187221
Future<dynamic> postJSON(String path, {int statusCode, void fail(http.Response response), Map<String, String> headers, Map<String, String> params, JSONConverter convert, body}) {
188222
if (convert == null) {
189223
convert = (github, input) => input;
@@ -198,6 +232,9 @@ class GitHub {
198232
});
199233
}
200234

235+
/**
236+
* Handles Authenticated Requests
237+
*/
201238
Future<http.Response> request(String method, String path, {Map<String, String> headers, Map<String, dynamic> params, String body}) {
202239
if (headers == null) {
203240
headers = {};

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