lib/ballast/concerns/errors_handling.rb in ballast-1.9.3 vs lib/ballast/concerns/errors_handling.rb in ballast-2.0.0
- old
+ new
@@ -10,46 +10,37 @@
extend ActiveSupport::Concern
# Handles an error in the application.
#
# @param exception [Hash|Exception] The exception to handle.
- # @param layout [String] The layout to use to render the error.
+ # @param layout [String] The layout to use to render the error. The `@error` variable will be exposed.
# @param title [String] The title to set in case of custom errors.
- # @param format [String|Symbol] The format of the response.
- def handle_error(exception = nil, layout = "error", title = "Error - Application", format = nil)
- @error ||= exception
+ # @param format [String|Symbol|NilClass] The format of the response.
+ def handle_error(exception, layout: "error", title: "Error - Application", format: nil)
+ @error =
+ if exception.is_a?(Lazier::Exceptions::Debug)
+ {status: 503, title: "Debug", error: exception.message, exception: exception}
+ elsif exception.is_a?(::Hash)
+ exception.reverse_merge({title: title})
+ else
+ {status: 500, title: "Error - #{exception.class}", error: exception.message, exception: exception}
+ end
- if @error.is_a?(Lazier::Exceptions::Debug) then
- @error_title = "Debug"
- @error_code = 503
- elsif @error.is_a?(Hash) then
- @error_title = @error[:title] || title
- @error_code = @error[:status]
- @error_message = @error[:error]
- else
- @error_title = "Error - #{@error.class.to_s}"
- @error_code = 500
- end
-
send_or_render_error(layout, format)
end
private
- # Send an AJAX error o renders it.
- #
- # @param layout [String] The layout to use to render the error.
- # @param format [String|Symbol] The format of the response.
- def send_or_render_error(layout, format = nil)
- format ||= request.format.to_sym
- if is_ajax? || format.to_s =~ /^json/ then
- details = {type: @error_title}
- details[:backtrace] = @error.backtrace if @error.respond_to?(:backtrace)
- data = prepare_ajax(@error_code, details, @error_message || @error.message)
- send_ajax(data, format: format)
- else
- render(html: "", status: @error_code, layout: layout, formats: [:html])
- end
+ # :nodoc:
+ def send_or_render_error(layout, format = nil)
+ format ||= request.format.to_sym
+
+ if ajax_request? || format.match(/^json/)
+ details = {description: @error[:title], backtrace: @error[:exception].safe_send(:backtrace)}
+ prepare_ajax_response(status: @error[:status], data: details, error: @error[:error]).reply(format: format)
+ else
+ render(html: "", status: @error[:status], layout: layout, formats: [:html])
end
+ end
end
end
end