balena-cli/lib/resin/errors/errors.spec.coffee

64 lines
1.8 KiB
CoffeeScript
Raw Normal View History

2014-11-24 18:00:35 +00:00
expect = require('chai').expect
sinon = require('sinon')
log = require('../log/log')
errors = require('./errors')
describe 'Errors:', ->
describe '#handle()', ->
it 'should log the error message to stderr', ->
2014-12-05 16:52:31 +00:00
message = 'Hello World'
2014-11-24 18:00:35 +00:00
logErrorStub = sinon.stub(log, 'error')
2014-12-05 16:52:31 +00:00
error = new Error(message)
2014-11-24 18:00:35 +00:00
errors.handle(error, false)
2014-12-05 16:52:31 +00:00
expect(logErrorStub).to.have.been.calledWith(message)
2014-11-24 18:00:35 +00:00
logErrorStub.restore()
it 'should do nothing if error is not an instance of Error', ->
logErrorStub = sinon.stub(log, 'error')
for item in [
undefined
null
[ 1, 2, 3 ]
'Hello'
{ message: 'foo bar' }
]
errors.handle(item, false)
expect(logErrorStub).to.not.have.been.called
logErrorStub.restore()
2014-11-26 14:32:57 +00:00
checkProcessExitOption = (error, value, expectations) ->
2014-11-24 18:00:35 +00:00
processExitStub = sinon.stub(process, 'exit')
logErrorStub = sinon.stub(log, 'error')
2014-11-26 14:32:57 +00:00
errors.handle(error, value)
2014-11-24 18:00:35 +00:00
expectations(processExitStub)
processExitStub.restore()
logErrorStub.restore()
it 'should exit if the last parameter is true', ->
2014-12-05 16:52:31 +00:00
error = new Error()
2014-11-26 14:32:57 +00:00
checkProcessExitOption error, true, (processExitStub) ->
expect(processExitStub).to.have.been.calledWith(1)
2014-11-24 18:00:35 +00:00
it 'should not exit if the last parameter is false', ->
2014-12-05 16:52:31 +00:00
error = new Error()
2014-11-26 14:32:57 +00:00
checkProcessExitOption error, false, (processExitStub) ->
2014-11-24 18:00:35 +00:00
expect(processExitStub).to.not.have.been.called
2014-11-24 18:41:16 +00:00
2014-11-26 14:32:57 +00:00
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)
2014-11-24 18:41:16 +00:00
describe 'NotFound', ->
it 'should get a custom message', ->
message = 'Foo'
error = new errors.NotFound(message)
expect(error.message).to.not.equal(message)
expect(error.message).to.contain(message)