2017-03-09 11:44:51 +00:00
|
|
|
module ApplicationController::HandlesErrors
|
2017-01-20 09:45:19 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
rescue_from StandardError, with: :internal_server_error
|
|
|
|
rescue_from ExecJS::RuntimeError, with: :internal_server_error
|
|
|
|
rescue_from ActiveRecord::RecordNotFound, with: :not_found
|
|
|
|
rescue_from ActiveRecord::StatementInvalid, with: :unprocessable_entity
|
|
|
|
rescue_from ActiveRecord::RecordInvalid, with: :unprocessable_entity
|
|
|
|
rescue_from ArgumentError, with: :unprocessable_entity
|
|
|
|
rescue_from Exceptions::UnprocessableEntity, with: :unprocessable_entity
|
|
|
|
rescue_from Exceptions::NotAuthorized, with: :unauthorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_found(e)
|
2017-04-19 10:09:54 +00:00
|
|
|
logger.error e
|
2017-01-20 09:45:19 +00:00
|
|
|
respond_to_exception(e, :not_found)
|
2017-08-21 23:13:19 +00:00
|
|
|
http_log
|
2017-01-20 09:45:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def unprocessable_entity(e)
|
2017-04-19 10:09:54 +00:00
|
|
|
logger.error e
|
2017-01-20 09:45:19 +00:00
|
|
|
respond_to_exception(e, :unprocessable_entity)
|
2017-08-21 23:13:19 +00:00
|
|
|
http_log
|
2017-01-20 09:45:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def internal_server_error(e)
|
2017-04-19 10:09:54 +00:00
|
|
|
logger.error e
|
2017-01-20 09:45:19 +00:00
|
|
|
respond_to_exception(e, :internal_server_error)
|
2017-08-21 23:13:19 +00:00
|
|
|
http_log
|
2017-01-20 09:45:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def unauthorized(e)
|
|
|
|
error = humanize_error(e.message)
|
|
|
|
response.headers['X-Failure'] = error.fetch(:error_human, error[:error])
|
|
|
|
respond_to_exception(e, :unauthorized)
|
2017-08-21 23:13:19 +00:00
|
|
|
http_log
|
2017-01-20 09:45:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def respond_to_exception(e, status)
|
|
|
|
status_code = Rack::Utils.status_code(status)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: humanize_error(e.message), status: status }
|
2017-10-01 12:25:52 +00:00
|
|
|
format.any do
|
2017-01-20 09:45:19 +00:00
|
|
|
@exception = e
|
|
|
|
@traceback = !Rails.env.production?
|
|
|
|
file = File.open(Rails.root.join('public', "#{status_code}.html"), 'r')
|
|
|
|
render inline: file.read, status: status
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-20 09:45:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def humanize_error(error)
|
|
|
|
data = {
|
|
|
|
error: error
|
|
|
|
}
|
|
|
|
|
|
|
|
case error
|
|
|
|
when /Validation failed: (.+?)(,|$)/i
|
|
|
|
data[:error_human] = $1
|
|
|
|
when /(already exists|duplicate key|duplicate entry)/i
|
|
|
|
data[:error_human] = 'Object already exists!'
|
|
|
|
when /null value in column "(.+?)" violates not-null constraint/i
|
|
|
|
data[:error_human] = "Attribute '#{$1}' required!"
|
|
|
|
when /Field '(.+?)' doesn't have a default value/i
|
|
|
|
data[:error_human] = "Attribute '#{$1}' required!"
|
|
|
|
when 'Exceptions::NotAuthorized'
|
|
|
|
data[:error] = 'Not authorized'
|
|
|
|
data[:error_human] = data[:error]
|
|
|
|
end
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
if Rails.env.production? && data[:error_human].present?
|
2017-01-20 09:45:19 +00:00
|
|
|
data[:error] = data.delete(:error_human)
|
|
|
|
end
|
|
|
|
data
|
|
|
|
end
|
|
|
|
end
|