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

91 lines
2.2 KiB
CoffeeScript
Raw Normal View History

2014-11-26 14:32:57 +00:00
_ = require('lodash')
2014-11-24 18:00:35 +00:00
TypedError = require('typed-error')
log = require('../log/log')
exports.NotFound = class NotFound extends TypedError
2014-12-05 19:04:49 +00:00
# Construct a Not Found error
#
# @param {String} name name of the thing that was not found
#
# @example Application not found
# throw new resin.errors.NotFound('application')
# Error: Couldn't find application
#
2014-11-24 18:41:16 +00:00
constructor: (name) ->
2014-11-24 18:00:35 +00:00
@message = "Couldn't find #{name}"
2014-11-26 14:32:57 +00:00
2014-12-05 19:04:49 +00:00
# Error code
code: 1
exports.InvalidConfigFile = class InvalidConfigFile extends TypedError
# Construct an Invalid Config File error
#
# @param {String} file the name of the invalid configuration file
#
# @example Invalid config file error
# throw new resin.errors.InvalidConfigFile('/opt/resin.conf')
# Error: Invalid configuration file: /opt/resin.conf
#
constructor: (file) ->
@message = "Invalid configuration file: #{file}"
2014-12-05 19:04:49 +00:00
# Error code
code: 1
2014-12-01 14:18:39 +00:00
exports.InvalidCredentials = class InvalidCredentials extends TypedError
2014-12-05 19:04:49 +00:00
# Construct an Invalid Credentials error
#
# @example Invalid credentials error
# throw new resin.errors.InvalidCredentials()
# Error: Invalid credentials
#
2014-12-01 14:18:39 +00:00
constructor: ->
@message = 'Invalid credentials'
2014-12-05 19:04:49 +00:00
# Error code
code: 1
2014-12-01 14:18:39 +00:00
2014-11-26 14:32:57 +00:00
exports.NotAny = class NotAny extends TypedError
2014-12-05 19:04:49 +00:00
# Construct an Not Any error
#
# @param {String} name name of the thing that the user doesn't have
#
# @example Not Any applications error
# throw new resin.errors.NotAny('applications')
# Error: You don't have any applications
#
2014-11-26 14:32:57 +00:00
constructor: (name) ->
@message = "You don't have any #{name}"
2014-11-24 18:00:35 +00:00
2014-12-05 19:04:49 +00:00
# Error code
code: 0
# Handle error instances
#
# Prints the message to stderr and aborts the program with the corresponding error code, or 0 if none.
#
# @param {Error} error the error instance
# @param {Boolean} exit whether to exit or not (defaults to true)
#
# @example Handle error
# error = new Error('My Error')
# shouldExit = false
# resin.errors.handle(error, shouldExit)
#
2014-11-24 18:00:35 +00:00
exports.handle = (error, exit = true) ->
return if not error? or error not instanceof Error
if error.message?
log.error(error.message)
2014-11-26 14:32:57 +00:00
if _.isNumber(error.code)
errorCode = error.code
else
errorCode = 1
process.exit(errorCode) if exit