2014-12-22 16:47:12 +00:00
|
|
|
_ = require('lodash')
|
|
|
|
log = require('../log/log')
|
|
|
|
|
|
|
|
exports.handle = (error, exit = true) ->
|
|
|
|
return if not error? or error not instanceof Error
|
|
|
|
|
|
|
|
if process.env.DEBUG
|
|
|
|
log.error(error.stack)
|
|
|
|
else
|
2015-01-06 16:54:40 +00:00
|
|
|
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?
|
2014-12-22 16:47:12 +00:00
|
|
|
log.error(error.message)
|
|
|
|
|
2015-01-06 16:46:31 +00:00
|
|
|
if _.isNumber(error.exitCode)
|
|
|
|
errorCode = error.exitCode
|
2014-12-22 16:47:12 +00:00
|
|
|
else
|
|
|
|
errorCode = 1
|
|
|
|
|
|
|
|
process.exit(errorCode) if exit
|
2015-01-09 15:22:18 +00:00
|
|
|
|
|
|
|
exports.handleCallback = (callback, context, exit) ->
|
|
|
|
if not _.isFunction(callback)
|
|
|
|
throw new Error('Callback is not a function')
|
|
|
|
|
|
|
|
return (error, args...) ->
|
|
|
|
exports.handle(error, exit) if error?
|
|
|
|
return callback.apply(context, args)
|