Permalink
Browse files

Better error handling.

  • Loading branch information...
1 parent ea81ac3 commit 0c358542201aab46019000f74cee9444fef00b08 @vesln committed Jan 18, 2012
Showing with 8 additions and 13 deletions.
  1. +8 −13 lib/github.js
View
21 lib/github.js
@@ -42,12 +42,10 @@ github.repos = function(username, cb) {
var repos = [];
github.request(url, function(err, response, body) {
- if (response.statusCode == 404) {
- throw new Error('Oops. Invalid user.');
- }
-
- if (response.statusCode == 401) {
- done(new Error('Oops. Invalid login credentials.'));
+ switch (response.statusCode) {
+ case 404: throw new Error('Oops. Invalid user.');
+ case 401: return done(new Error('Oops. Invalid login credentials.'));
+ case 403: throw new Error('Hit GitHub api limit. Try again after few secs.');
}
body.repositories.forEach(function(repo) {
@@ -71,14 +69,11 @@ github.issues = function(username, repos, cb) {
var url = github.url('issues', { user: username, repo: repo });
github.request(url, function(err, response, body) {
- if (response.statusCode == 404) {
- done(new Error('Oops. Invalid user or repo.'));
- }
-
- if (response.statusCode == 401) {
- done(new Error('Oops. Invalid login credentials.'));
+ switch (response.statusCode) {
+ case 404: return done(new Error('Oops. Invalid user or repo.'));
+ case 401: return done(new Error('Oops. Invalid login credentials.'));
+ case 403: return done(new Error('Hit GitHub api limit. Try again after few secs.'));
}
-
results[repo] = body.issues;
done(err);
});

0 comments on commit 0c35854

Please sign in to comment.