Exception: FunWithJsonApi::Exception

Inherits:
StandardError
  • Object
show all
Defined in:
lib/fun_with_json_api/exception.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, payload) ⇒ Exception

Returns a new instance of Exception.



7
8
9
10
# File 'lib/fun_with_json_api/exception.rb', line 7

def initialize(message, payload)
  super(message)
  @payload = Array.wrap(payload)
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/fun_with_json_api/exception.rb', line 5

def payload
  @payload
end

Instance Method Details

#http_statusInteger

Returns The http status code for rendering this error.

Returns:

  • (Integer)

    The http status code for rendering this error



13
14
15
16
17
18
19
20
21
22
# File 'lib/fun_with_json_api/exception.rb', line 13

def http_status
  payload_statuses = payload.map(&:status).uniq
  if payload_statuses.length == 1
    Integer(payload_statuses.first || '400') # Return the unique status code
  elsif payload_statuses.any? { |status| status.starts_with?('5') }
    500 # We have a server error
  else
    400 # Bad Request
  end
end