Skip to content

Commit

Permalink
Merge pull request #792 from bassettsj/test-for-callback
Browse files Browse the repository at this point in the history
Add test for issue #789 showing error message [Object object]
  • Loading branch information
matteofigus committed Jan 4, 2018
2 parents 3afb521 + 4d851e2 commit df8f0e1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/registry/domain/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const settings = require('../../resources/settings');
const strings = require('../../resources');
const validator = require('./validators');
const versionHandler = require('./version-handler');
const errorToString = require('../../utils/error-to-string');

module.exports = function(conf) {
const cdn = !conf.local && new conf.storage.adapter(conf.storage.options);
Expand Down Expand Up @@ -161,7 +162,10 @@ module.exports = function(conf) {
version,
(err, component) => {
if (err) {
return callback(`component not available: ${err}`, null);
return callback(
`component not available: ${errorToString(err)}`,
null
);
}
callback(null, _.extend(component, { allVersions }));
}
Expand Down Expand Up @@ -258,12 +262,9 @@ module.exports = function(conf) {
)}`,

getStaticFilePath: (componentName, componentVersion, filePath) =>
`${repository.getComponentPath(
componentName,
componentVersion
)}${conf.local
? settings.registry.localStaticRedirectorPath
: ''}${filePath}`,
`${repository.getComponentPath(componentName, componentVersion)}${
conf.local ? settings.registry.localStaticRedirectorPath : ''
}${filePath}`,

getTemplatesInfo: () => templatesInfo,
getTemplate: type => templatesHash[type],
Expand Down
12 changes: 12 additions & 0 deletions src/utils/error-to-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const _ = require('lodash');

module.exports = function errorToString(err) {
const hasMessage = err != null && _.isString(err.msg);
if (_.isString(err)) {
return err;
} else if (hasMessage) {
return err.msg;
}

return err + '';
};
27 changes: 26 additions & 1 deletion test/unit/registry-domain-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const injectr = require('injectr');
const path = require('path');
const sinon = require('sinon');
const _ = require('lodash');
const resources = require('../../src/resources');

describe('registry : domain : repository', () => {
let response;
Expand Down Expand Up @@ -181,7 +182,31 @@ describe('registry : domain : repository', () => {
);
});
});

describe('when the get component info fails', () => {
before(done => {
componentsCacheMock.get.yields(null, componentsCacheBaseResponse);
sinon
.stub(repository, 'getComponentInfo')
.callsFake((name, version, callback) => {
callback({
msg: resources.errors.STORAGE.FILE_NOT_VALID,
code: resources.errors.STORAGE.FILE_NOT_VALID_CODE
});
});
repository.getComponent('hello-world', '1.0.0', saveResult(done));
});
after(() => {
repository.getComponentInfo.restore();
});
it('should respond with a proper error', () => {
expect(response.error).not.to.be.empty;
expect(response.error).to.equal(
`component not available: ${
resources.errors.STORAGE.FILE_NOT_VALID
}`
);
});
});
describe('when the component exists but version does not', () => {
before(done => {
componentsCacheMock.get.yields(null, componentsCacheBaseResponse);
Expand Down

0 comments on commit df8f0e1

Please sign in to comment.