module Onsi::ErrorResponderBase
Handles default errors without StandardError
Error handled by default:
-
ActiveRecord::RecordNotFound
-
ActiveRecord::RecordInvalid
-
ActionController::ParameterMissing
-
{Onsi::Params::MissingReqiredAttribute}
-
{Onsi::Params::RelationshipNotFound}
-
{Onsi::Errors::UnknownVersionError}
-
{Onsi::Errors::IncludedParamError}
@example
class PeopleController < ApplicationController include Onsi::Controller include Onsi::ErrorResponderBase # ... end
@author Maddie Schipper @since 1.0.0
Public Instance Methods
notify_unhandled_exception(exception)
click to toggle source
Can be overriden to report an un-handled exception to your error service of choice.
@param exception [StandardError] The error to report.
@example
class ApplicationController < ActionController::API include Onsi::ErrorResponderBase include Onsi::Controller def notify_unhandled_exception(exception) Bugsnag.notify(exception) end # ... end
# File lib/onsi/error_responder.rb, line 65 def notify_unhandled_exception(exception) Rails.logger.error "Unhandled Exception `#{exception.class.name}: #{exception.message}`" end
render_error(response)
click to toggle source
Render an API error response.
@param response [Onsi::ErrorResponse] The response object to render
# File lib/onsi/error_responder.rb, line 44 def render_error(response) render(response.renderable) end
render_error_404(_error)
click to toggle source
@private
# File lib/onsi/error_responder.rb, line 71 def render_error_404(_error) response = ErrorResponse.new(404) response.add(404, 'not_found') render_error(response) end
render_error_422(error)
click to toggle source
@private
# File lib/onsi/error_responder.rb, line 79 def render_error_422(error) response = ErrorResponse.new(422) error.record.errors.details.each do |name, details| details.each do |info| response.add( 422, 'validation_error', title: "Validation Error: #{info[:error]}", meta: info.merge(param: name) ) end end render_error(response) end
respond_included_param_error_400(error)
click to toggle source
@private
# File lib/onsi/error_responder.rb, line 108 def respond_included_param_error_400(error) response = ErrorResponse.new(400) response.add( 400, 'missing_include', details: error.message, meta: { source: error.path } ) render_error(response) end
respond_invalid_version_error_400(error)
click to toggle source
@private
# File lib/onsi/error_responder.rb, line 133 def respond_invalid_version_error_400(error) notify_unhandled_exception(error) response = ErrorResponse.new(400) response.add( 400, 'invalid_version', details: "API version #{error.version} unsupported for #{error.klass.name.underscore}" ) render_error(response) end
respond_missing_attr_error_400(error)
click to toggle source
@private
# File lib/onsi/error_responder.rb, line 146 def respond_missing_attr_error_400(error) response = ErrorResponse.new(400) response.add( 400, 'missing_attribute', meta: { attribute: error.attribute } ) render_error(response) end
respond_missing_relationship_error_400(error)
click to toggle source
@private
# File lib/onsi/error_responder.rb, line 121 def respond_missing_relationship_error_400(error) response = ErrorResponse.new(400) response.add( 400, 'missing_relationship', meta: { param: error.key } ) render_error(response) end
respond_pagination_error_400(error)
click to toggle source
@private
# File lib/onsi/error_responder.rb, line 160 def respond_pagination_error_400(error) response = ErrorResponse.new(400) response.add( 400, 'pagination_error', details: error.message ) render_error(response) end
respond_param_error_400(error)
click to toggle source
@private
# File lib/onsi/error_responder.rb, line 96 def respond_param_error_400(error) response = ErrorResponse.new(400) response.add( 400, 'missing_parameter', meta: { param: error.param } ) render_error(response) end
Private Instance Methods
error_metadata(error)
click to toggle source
# File lib/onsi/error_responder.rb, line 172 def error_metadata(error) return nil unless Rails.configuration.consider_all_requests_local { exception: { '_note' => '`exception` will be removed if Rails.configuration.consider_all_requests_local is false', class: error.class.name, message: error.message, backtrace: error.backtrace } } end