Class: Noise::ExceptionRenderer

Inherits:
Struct
  • Object
show all
Defined in:
lib/noise/exception_renderer.rb

Overview

Determines how to render exception

Examples:

class ApplicationExceptionRenderer < ExceptionRenderer
  def render(responder)
    if env[Rack::PATH_INFO] =~ %r{\A/partners}
      {
        id: error_id,
        error: message,
        code: code
      }
    else
      super
    end
  end

  def message
    if error.is_a?(PublicError)
      error.message
    else
      'Internal Server Error'
    end
  end

  def code
    if error.is_a?(PublicError)
      error.code
    else
      :internal_server_error
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envObject

Returns the value of attribute env

Returns:

  • (Object)

    the current value of env



39
40
41
# File 'lib/noise/exception_renderer.rb', line 39

def env
  @env
end

Instance Method Details

#errorStandardError

Returns:

  • (StandardError)


58
59
60
# File 'lib/noise/exception_renderer.rb', line 58

def error
  env['action_dispatch.exception']
end

#error_idString

Returns error identifier, UUID.

Returns:

  • (String)

    error identifier, UUID



63
64
65
# File 'lib/noise/exception_renderer.rb', line 63

def error_id
  env['action_dispatch.request_id']
end

#error_serializerObject



53
54
55
# File 'lib/noise/exception_renderer.rb', line 53

def error_serializer
  error.is_a?(PublicError) ? PublicErrorSerializer : ErrorSerializer
end

#render(responder) ⇒ String

Returns error representation.

Parameters:

Returns:

  • (String)

    error representation



42
43
44
45
46
47
48
49
50
51
# File 'lib/noise/exception_renderer.rb', line 42

def render(responder)
  ActiveModelSerializers::SerializableResource.new(
    Array(error),
    each_serializer: error_serializer,
    adapter: :json,
    root: 'errors',
    meta: { 'status' => responder.status_code }.merge(error.try(:meta_hash) || {}),
    scope: { http_status: responder.status_code, id: error_id },
  ).as_json.to_json
end