Class: ApiErrorHandler::Serializers::Json

Inherits:
BaseSerializer show all
Defined in:
lib/api_error_handler/serializers/json.rb

Constant Summary

Constants inherited from BaseSerializer

BaseSerializer::DEFAULT_STATUS_CODE

Instance Method Summary collapse

Methods inherited from BaseSerializer

#initialize, #status_code, #title

Constructor Details

This class inherits a constructor from ApiErrorHandler::Serializers::BaseSerializer

Instance Method Details

#render_formatObject



29
30
31
# File 'lib/api_error_handler/serializers/json.rb', line 29

def render_format
  :json
end

#serialize(options = {}) ⇒ Object

There is no official spec that governs this error response format so this serializer is just trying to impliment a simple response with sensible defaults.

I borrowed heavily from Facebook's error response format since it seems to be a reasonable approach for a simple light-weight error response.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/api_error_handler/serializers/json.rb', line 15

def serialize(options = {})
  body = {
    error: {
      title: title,
      detail: @error.message,
    }
  }

  body[:error][:id] = options[:error_id] if options[:error_id]
  body[:error][:backtrace] = @error.backtrace if options[:backtrace]

  body.to_json
end