Module: ApiErrorHandler
- Defined in:
- lib/api_error_handler.rb,
lib/api_error_handler/errors.rb,
lib/api_error_handler/version.rb,
lib/api_error_handler/serializers/xml.rb,
lib/api_error_handler/serializers/json.rb,
lib/api_error_handler/serializers/json_api.rb,
lib/api_error_handler/serializers/base_serializer.rb
Defined Under Namespace
Modules: Serializers
Constant Summary collapse
- SERIALIZERS_BY_FORMAT =
{ json: Serializers::Json, json_api: Serializers::JsonApi, xml: Serializers::Xml, }.freeze
- SERIALIZER_OPTIONS =
{ backtrace: false, }.freeze
- CONTENT_TYPE_BY_FORMAT =
{ json_api: "application/vnd.api+json" }.freeze
- VERSION =
"0.1.0"
Instance Method Summary collapse
Instance Method Details
#handle_api_errors(options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/api_error_handler.rb', line 20 def handle_api_errors( = {}) format = .fetch(:format, :json) status_mapping = ActionDispatch::ExceptionWrapper.rescue_responses error_reporter = [:error_reporter] = SERIALIZER_OPTIONS.merge( .slice(*SERIALIZER_OPTIONS.keys) ) serializer_class = [:serializer] || SERIALIZERS_BY_FORMAT.fetch(format) content_type = [:content_type] || CONTENT_TYPE_BY_FORMAT[format] rescue_from StandardError do |error| begin status = status_mapping[error.class.to_s] error_id = nil error_id = [:error_id].call(error) if [:error_id] error_reporter.call(error, error_id) if error_reporter serializer = serializer_class.new(error, status) response_body = serializer.serialize( .merge(error_id: error_id) ) render( serializer.render_format => response_body, content_type: content_type, status: status ) rescue raise error end end end |