Skip to content

Commit

Permalink
Merge pull request #10 from strongloop/feature/relax-error-message-ch…
Browse files Browse the repository at this point in the history
…ecks

Relax error message checks
  • Loading branch information
bajtos committed Mar 24, 2014
2 parents c178826 + 2079ac5 commit bf88a09
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/end-to-end/install.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('install', function() {

it('returns error when package name does not exist', function() {
return npm.install(targetDir, 'unknown-package')
.then(installShouldHaveFailed, expectError(/^404 Not Found/));
.then(installShouldHaveFailed, expectErrorWithStatusCode(404));
});

it('returns error when package version does not exist', function() {
Expand All @@ -55,7 +55,7 @@ describe('install', function() {
req = pkg.name + '@2.0.0';
return npm.install(targetDir, req);
})
.then(installShouldHaveFailed, expectError(/^version not found/));
.then(installShouldHaveFailed, expectErrorWithStatusCode(404));
});

beforeEach(function() {
Expand All @@ -68,9 +68,9 @@ describe('install', function() {
throw new Error('npm install should have failed');
}

function expectError(matcher) {
function expectErrorWithStatusCode(code) {
return function(err) {
expect(err.message).to.match(matcher);
expect(err.statusCode).to.equal(code);
};
}
});
24 changes: 23 additions & 1 deletion lib/npm-exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,29 @@ function loadConfig(options) {
purgeNpmFromRequireCache();
var npm = require('npm');
var load = Promise.promisify(npm.load, npm);
return load(options).return(npm);
return load(options)
.then(function() {
// Monkey patch npm-registry-client to include
// HTTP response details in error objects
npm.registry.request = createErrorPatchingRequest(npm.registry.request);
})
.return(npm);
}

function createErrorPatchingRequest(request) {
return function() {
var args = Array.prototype.slice.call(arguments);
var cb = args.pop();

args.push(function patchCallback(err, remoteData, raw, response) {
if (err && response) {
err.statusCode = response.statusCode;
}
cb.apply(this, arguments);
});

request.apply(this, args);
};
}

function purgeNpmFromRequireCache() {
Expand Down

0 comments on commit bf88a09

Please sign in to comment.