Make errors.handle handle EISDIR and ENOENT

This commit is contained in:
Juan Cruz Viotti 2015-01-06 13:54:40 -03:00
parent e9caca8c24
commit af98a89e51
3 changed files with 24 additions and 14 deletions

View File

@ -40,16 +40,4 @@ exports.add = permissions.user (params) ->
key = key.trim()
resin.models.key.create(params.name, key, callback)
], (error) ->
return if not error?
# TODO: Make handle() check the error type
# and accomodate as most as possible to prevent
# this types of checks in client code.
if error.code is 'EISDIR'
error.message = "File is a directory: #{error.path}"
if error.code is 'ENOENT'
error = new Error("File not found: #{error.path}")
errors.handle(error)
], errors.handle

View File

@ -7,7 +7,13 @@ exports.handle = (error, exit = true) ->
if process.env.DEBUG
log.error(error.stack)
else
if error.message?
if error.code is 'EISDIR'
log.error("File is a directory: #{error.path}")
else if error.code is 'ENOENT'
log.error("No such file or directory: #{error.path}")
else if error.message?
log.error(error.message)
if _.isNumber(error.exitCode)

View File

@ -61,3 +61,19 @@ describe 'Errors:', ->
expect(logErrorStub).to.have.been.calledOnce
expect(logErrorStub).to.have.been.calledWith(error.stack)
delete process.env.DEBUG
it.only 'should handle EISDIR', ->
error = new Error()
error.code = 'EISDIR'
error.path = 'hello'
checkProcessExitOption error, false, (processExitStub, logErrorStub) ->
expect(logErrorStub).to.have.been.calledOnce
expect(logErrorStub).to.have.been.calledWith('File is a directory: hello')
it.only 'should handle ENOENT', ->
error = new Error()
error.code = 'ENOENT'
error.path = 'hello'
checkProcessExitOption error, false, (processExitStub, logErrorStub) ->
expect(logErrorStub).to.have.been.calledOnce
expect(logErrorStub).to.have.been.calledWith('No such file or directory: hello')