class Johac::Error::ResponseError

Exception to be used when dealing with HTTP responses from a Johac API.

Attributes

body[R]
headers[R]
status[R]

Public Class Methods

from_response(status_code, headers, body) click to toggle source

If a problem is detected in an HTTP response, build the proper exception, otherwise return nil.

@param status_code [Integer] HTTP response code. @param body [String] Response body. @param headers [Hash] Response headers.

@return [Johac::Error::RetryableError] A client or server error which may be retried @return [Johac::Error::ClientError] The client has made a mistake and should take corrective action. @return [Johac::Error::ServerError] If there was a problem server-side, we're on it. @return [nil] If no error was detected in the response.

# File lib/johac/error.rb, line 40
def self.from_response(status_code, headers, body)
  if klass =  case status_code
              when 429 then RetryableError
              when 503 then RetryableError
              when 504 then RetryableError
              when 400..499 then ClientError
              when 500..599 then ServerError
              end
    klass.new(status_code, body, headers)
  end
end
new(status, body, headers) click to toggle source

Will attempt to decode a response body via JSON, and look for the 'message' key in the resulting (assumed) hash. If response body cannot be parsed via JSON the entire response body is set as the message for the exception.

@param body [String] Response body. @param headers [Hash] Response headers.

Calls superclass method
# File lib/johac/error.rb, line 19
def initialize(status, body, headers)
  @status = status
  @headers = headers
  @body = if headers['Content-Type'] == 'application/json'
    JSON.parse(body) rescue body
  else
    body
  end
  super(body)
end