<commit_before>_ = 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.InvalidConfigFile = class NotFound extends TypedError
	constructor: (file) ->
		@message = "Invalid configuration file: #{file}"
		@code = 1

exports.InvalidCredentials = class InvalidCredentials extends TypedError
	constructor: ->
		@message = 'Invalid credentials'
		@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

	if error.message?<commit_msg>Document resin/errors<commit_after>_ = require('lodash')
TypedError = require('typed-error')
log = require('../log/log')

exports.NotFound = class NotFound extends TypedError

	# 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
	#
	constructor: (name) ->
		@message = "Couldn't find #{name}"

	# 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}"

	# Error code
	code: 1

exports.InvalidCredentials = class InvalidCredentials extends TypedError

	# Construct an Invalid Credentials error
	#
	# @example Invalid credentials error
	#		throw new resin.errors.InvalidCredentials()
	#		Error: Invalid credentials
	#
	constructor: ->
		@message = 'Invalid credentials'

	# Error code
	code: 1

exports.NotAny = class NotAny extends TypedError

	# 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
	#
	constructor: (name) ->
		@message = "You don't have any #{name}"

	# 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)
#
exports.handle = (error, exit = true) ->
	return if not error? or error not instanceof Error

	if error.message?
{'fraction_non_alphanumeric': 0.10643478260869565, 'fraction_numerical': 0.003130434782608696, 'mean_word_length': 4.2290909090909095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4798730', 'n_tokens_mistral': 903, 'n_tokens_neox': 863, 'n_words': 307}