diff --git a/src/registry/domain/repository.js b/src/registry/domain/repository.js index aac0c4fb3..a1837e2c4 100644 --- a/src/registry/domain/repository.js +++ b/src/registry/domain/repository.js @@ -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); @@ -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 })); } @@ -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], diff --git a/src/utils/error-to-string.js b/src/utils/error-to-string.js new file mode 100644 index 000000000..40a1df322 --- /dev/null +++ b/src/utils/error-to-string.js @@ -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 + ''; +}; diff --git a/test/unit/registry-domain-repository.js b/test/unit/registry-domain-repository.js index 0d59b5e47..a60b5ea69 100644 --- a/test/unit/registry-domain-repository.js +++ b/test/unit/registry-domain-repository.js @@ -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; @@ -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);