Module: Insights::API::Common::ApplicationControllerMixins::ExceptionHandling
- Defined in:
- lib/insights/api/common/application_controller_mixins/exception_handling.rb
Constant Summary collapse
- DEFAULT_ERROR_CODE =
400
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(other) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/insights/api/common/application_controller_mixins/exception_handling.rb', line 8 def self.included(other) other.rescue_from(StandardError, RuntimeError) do |exception| logger.error("#{exception.class.name}: #{exception.}\n#{exception.backtrace.join("\n")}") errors = Insights::API::Common::ErrorDocument.new.tap do |error_document| exception_list_from(exception).each do |exc| code = exc.respond_to?(:code) ? exc.code : error_code_from_class(exc) error_document.add(code, "#{exc.class}: #{exc.}") end end render :json => errors.to_h, :status => error_code_from_class(exception) end end |
Instance Method Details
#error_code_from_class(exception) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/insights/api/common/application_controller_mixins/exception_handling.rb', line 31 def error_code_from_class(exception) if ActionDispatch::ExceptionWrapper.rescue_responses.key?(exception.class.to_s) ActionDispatch::ExceptionWrapper.rescue_responses[exception.class.to_s] else DEFAULT_ERROR_CODE end end |
#exception_list_from(exception) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/insights/api/common/application_controller_mixins/exception_handling.rb', line 22 def exception_list_from(exception) [].tap do |arr| until exception.nil? arr << exception exception = exception.cause end end end |