diff --git a/lib/errors/errors.coffee b/lib/errors/errors.coffee index 3e66a931..bbcddbe6 100644 --- a/lib/errors/errors.coffee +++ b/lib/errors/errors.coffee @@ -1,9 +1,16 @@ +_ = require('lodash') TypedError = require('typed-error') log = require('../log/log') exports.NotFound = class NotFound extends TypedError constructor: (name) -> @message = "Couldn't find #{name}" + @code = 1 + +exports.NotAny = class NotAny extends TypedError + constructor: (name) -> + @message = "You don't have any #{name}" + @code = 0 exports.handle = (error, exit = true) -> return if not error? or error not instanceof Error @@ -11,4 +18,9 @@ exports.handle = (error, exit = true) -> if error.message? log.error(error.message) - process.exit(1) if exit + if _.isNumber(error.code) + errorCode = error.code + else + errorCode = 1 + + process.exit(errorCode) if exit diff --git a/lib/errors/errors.spec.coffee b/lib/errors/errors.spec.coffee index 77fbe70a..2bc67e20 100644 --- a/lib/errors/errors.spec.coffee +++ b/lib/errors/errors.spec.coffee @@ -32,22 +32,30 @@ describe 'Errors:', -> expect(logErrorStub).to.not.have.been.called logErrorStub.restore() - checkProcessExitOption = (value, expectations) -> + checkProcessExitOption = (error, value, expectations) -> processExitStub = sinon.stub(process, 'exit') logErrorStub = sinon.stub(log, 'error') - errors.handle(new Error(MESSAGES.helloWorld), value) + errors.handle(error, value) expectations(processExitStub) processExitStub.restore() logErrorStub.restore() it 'should exit if the last parameter is true', -> - checkProcessExitOption true, (processExitStub) -> - expect(processExitStub).to.have.been.called + error = new Error(MESSAGES.helloWorld) + checkProcessExitOption error, true, (processExitStub) -> + expect(processExitStub).to.have.been.calledWith(1) it 'should not exit if the last parameter is false', -> - checkProcessExitOption false, (processExitStub) -> + error = new Error(MESSAGES.helloWorld) + checkProcessExitOption error, false, (processExitStub) -> expect(processExitStub).to.not.have.been.called + it 'should handle a custom error code from the error instance', -> + error = new Error() + error.code = 123 + checkProcessExitOption error, true, (processExitStub) -> + expect(processExitStub).to.have.been.calledWith(123) + describe 'NotFound', -> it 'should get a custom message', -> diff --git a/lib/models/application.coffee b/lib/models/application.coffee index 52ed981c..48e9ded0 100644 --- a/lib/models/application.coffee +++ b/lib/models/application.coffee @@ -1,3 +1,4 @@ +_ = require('lodash') Promise = require('bluebird') canvas = require('./_canvas') errors = require('../errors/errors') @@ -8,6 +9,11 @@ exports.getAll = -> options: orderby: 'app_name asc' expand: 'device' + .then (applications) -> + if _.isEmpty(applications) + return Promise.reject(new errors.NotAny('applications')) + + return applications exports.get = (id) -> return canvas.get diff --git a/lib/models/device.coffee b/lib/models/device.coffee index e50cd0c4..79a3eb91 100644 --- a/lib/models/device.coffee +++ b/lib/models/device.coffee @@ -1,4 +1,7 @@ canvas = require('./_canvas') +_ = require('lodash') +Promise = require('bluebird') +errors = require('../errors/errors') exports.getAll = (applicationId) -> return canvas.get @@ -8,6 +11,11 @@ exports.getAll = (applicationId) -> application: applicationId expand: 'application' orderby: 'name asc' + .then (devices) -> + if _.isEmpty(devices) + return Promise.reject(new errors.NotAny('devices')) + + return devices exports.remove = (id) -> return canvas.delete