Skip to content

Rate limit errors #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/Requestable.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,16 @@ function getNextPage(linksHeader = '') {
}

function callbackErrorOrThrow(cb, path) {
return function handler(response) {
let message = `error making request ${response.config.method} ${response.config.url}`;
let error = new ResponseError(message, path, response);
log(`${message} ${JSON.stringify(response.data)}`);
return function handler(object) {
let error;
if (object.hasOwnProperty('config')) {
const {status, statusText, config: {method, url}} = object;
let message = (`${status} error making request ${method} ${url}: "${statusText}"`);
error = new ResponseError(message, path, object);
log(`${message} ${JSON.stringify(object.data)}`);
} else {
error = object;
}
if (cb) {
log('going to error callback');
cb(error);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"minami": "^1.1.1",
"mocha": "^2.3.4",
"must": "^0.13.1",
"nock": "^8.0.0",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0"
},
Expand Down
83 changes: 83 additions & 0 deletions test/error.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import assert from 'assert';
import expect from 'must';
import nock from 'nock';

import Github from '../lib/GitHub';
import testUser from './fixtures/user.json';
import {assertSuccessful, assertFailure} from './helpers/callbacks';
import fixtureExhausted from './fixtures/repos-ratelimit-exhausted.js';
import fixtureOk from './fixtures/repos-ratelimit-ok.js';

describe('Rate limit error', function() {
let github;
let user;
let scope;

before(function() {
github = new Github();
user = github.getUser(testUser.USERNAME);
});

beforeEach(function() {
scope = fixtureExhausted();
});

it('should reject promise with 403 error', function() {
return user.listRepos().then(function() {
assert.fail(undefined, undefined, 'Promise was resolved instead of rejected');
}, function(error) {
expect(error).to.be.an.error();
expect(error).to.have.own('response');
expect(error.response).to.have.own('status');
expect(error.response.status).to.be(403);
});
});

it('should call callback', function(done) {
user.listRepos(assertFailure(done, function(error) {
expect(error).to.be.an.error();
expect(error).to.have.own('response');
expect(error.response).to.have.own('status');
expect(error.response.status).to.be(403);
done();
}));
});

afterEach(function() {
scope.done();
nock.cleanAll();
});
});

describe('Rate limit OK', function() {
let github;
let user;
let scope;

before(function() {
github = new Github();
user = github.getUser(testUser.USERNAME);
});

beforeEach(function() {
scope = fixtureOk();
});

it('should resolve promise', function() {
return expect(user.listRepos()).to.resolve.to.object();
});

it('should call callback with array of results', function(done) {
user.listRepos(assertSuccessful(done, function(error, result) {
expect(error).is.not.an.error();
expect(error).is.not.truthy();
expect(result).is.array();
done();
}));
});

afterEach(function() {
scope.done();
nock.cleanAll();
});
});
34 changes: 34 additions & 0 deletions test/fixtures/record.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import fs from 'fs';
import nock from 'nock';
import path from 'path';
import GitHub from '../../lib/GitHub';
import testUser from './user.json';

const gh = new GitHub();

let fileName;
gh.getRateLimit().getRateLimit()
.then((resp) => {
if (resp.data.rate.remaining === 0) {
fileName = 'repos-ratelimit-exhausted.js';
} else {
fileName = 'repos-ratelimit-ok.js';
}
nock.recorder.rec({
dont_print: true
});
gh.getUser(testUser.USERNAME).listRepos();
setTimeout(() => {
const fixtures = nock.recorder.play();
const filePath = path.join(__dirname, fileName);
const text = ('/* eslint-disable */\n' +
'import nock from \'nock\';\n' +
'export default function fixture() {\n' +
' let scope;\n' +
' scope = ' + fixtures.join('\nscope = ').trim().replace(/\n/g, '\n ') + '\n' +
' return scope;\n' +
'}\n');
fs.writeFileSync(filePath, text);
console.log('Wrote fixture data to', fileName);
}, 10000);
}).catch(console.error);
27 changes: 27 additions & 0 deletions test/fixtures/repos-ratelimit-exhausted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable */
import nock from 'nock';
export default function fixture() {
let scope;
scope = nock('https://api.github.com:443', {"encodedQueryParams":true})
.get('/users/mikedeboertest/repos')
.query({"type":"all","sort":"updated","per_page":"100"})
.reply(403, {"message":"API rate limit exceeded for 174.20.8.171. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)","documentation_url":"https://developer.github.com/v3/#rate-limiting"}, { server: 'GitHub.com',
date: 'Sat, 18 Jun 2016 11:50:00 GMT',
'content-type': 'application/json; charset=utf-8',
'content-length': '246',
connection: 'close',
status: '403 Forbidden',
'x-ratelimit-limit': '60',
'x-ratelimit-remaining': '0',
'x-ratelimit-reset': '1466253529',
'x-github-media-type': 'github.v3; format=json',
'access-control-expose-headers': 'ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval',
'access-control-allow-origin': '*',
'content-security-policy': 'default-src \'none\'',
'strict-transport-security': 'max-age=31536000; includeSubdomains; preload',
'x-content-type-options': 'nosniff',
'x-frame-options': 'deny',
'x-xss-protection': '1; mode=block',
'x-github-request-id': 'AE1408AB:EA59:14F2183:57653568' });
return scope;
}
31 changes: 31 additions & 0 deletions test/fixtures/repos-ratelimit-ok.js

Large diffs are not rendered by default.

Loading
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