Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #179 from cnpm/show-server-error
Browse files Browse the repository at this point in the history
Show registry server error response. fixed #178
  • Loading branch information
dead-horse committed Jan 24, 2014
2 parents 152f680 + 67eb7fa commit fedec47
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion proxy/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ function request(url, options, callback) {
options.dataType = options.dataType || 'json';
options.timeout = options.timeout || 120000;
url = config.sourceNpmRegistry + url;
urllib.request(url, options, callback);
urllib.request(url, options, function (err, data, res) {
if (err) {
var statusCode = res && res.statusCode || -1;
if (err.name === 'JSONResponseFormatError' && statusCode >= 500) {
err.name = 'NPMServerError';
err.message = 'Status ' + statusCode + ', ' + (data && data.toString() || 'empty body');
}
}
callback(err, data, res);
});
}

exports.get = function (name, callback) {
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/500.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Internal routing error

Sorry, we cannot connect to the intended server.

We have just been notified of this problem. We will correct it as soon as possible.

Feel free to contact us if you have any questions: [email protected]
16 changes: 16 additions & 0 deletions test/proxy/npm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

var should = require('should');
var mm = require('mm');
var fs = require('fs');
var path = require('path');
var npm = require('../../proxy/npm');

var fixtures = path.join(path.dirname(__dirname), 'fixtures');

describe('proxy/npm.test.js', function () {
afterEach(mm.restore);

Expand Down Expand Up @@ -47,4 +51,16 @@ describe('proxy/npm.test.js', function () {
done();
});
});

it('should return ServerError when http 500 response', function (done) {
var content = fs.readFileSync(path.join(fixtures, '500.txt'), 'utf8');
mm.http.request(/\//, content, { statusCode: 500 });
// http://registry.npmjs.org/octopie
npm.get('octopie', function (err, data) {
should.exist(err);
err.name.should.equal('NPMServerError');
err.message.should.equal('Status 500, ' + content);
done();
});
});
});

0 comments on commit fedec47

Please sign in to comment.