mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-21 14:37:47 +00:00
Improve error support
This commit is contained in:
parent
2fa26d3dba
commit
af00bd1faf
@ -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
|
||||
|
@ -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', ->
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user