class BerkeleyLibrary::TIND::API::APIException

Wrapper for network-related exceptions.

Attributes

params[R]

@return [Hash, nil] the API query parameters, if any

response[R]

@return [RestClient::Response, nil] the response, if any

status_code[R]

@return [Integer, nil] the numeric HTTP status code, if any

status_message[R]

@return [String, nil] the HTTP status message, if any

url[R]

@return [String, nil] the request URI, if any

Public Class Methods

new(msg, **opts) click to toggle source

Initializes a new APIException.

@option opts [String] :msg the exception message (if not present, a default message will be constructed) @option opts [String] :url the request URL, if any @option opts [Hash] :params the query or form parameters, if any @option opts [Integer] :status_code the numeric HTTP status code, if any @option opts [String] :status_message a human-readable string representation of the HTTP status

(if not present, a default will be constructed)

@option opts [RestClient::Response] :response the HTTP response, if any

Calls superclass method
# File lib/berkeley_library/tind/api/api_exception.rb, line 32
def initialize(msg, **opts)
  super(msg)

  @url = opts[:url].to_s if opts.key?(:url)
  @params = opts[:params]
  @status_code, default_status_message = format_status(opts[:status_code])
  @status_message = opts[:status_message] || default_status_message
  @response = opts[:response]
end

Private Class Methods

format_options(ex, url, params) click to toggle source
# File lib/berkeley_library/tind/api/api_exception.rb, line 75
def format_options(ex, url, params)
  {}.tap do |opts|
    opts[:url] = url if url
    opts[:params] = params if params
    next unless %i[http_code message response].all? { |f| ex.respond_to?(f) }

    opts[:status_code] = ex.http_code
    opts[:status_message] = ex.message
    opts[:response] = ex.response
  end
end
message_from(ex, url, params, detail) click to toggle source
# File lib/berkeley_library/tind/api/api_exception.rb, line 87
def message_from(ex, url, params, detail)
  ''.tap do |msg|
    msg << "#{detail}: " if detail
    msg << (url ? "#{API.format_request(url, params)} returned #{ex}" : ex.to_s)
  end
end
wrap(ex, **opts) click to toggle source

@param ex [Exception] the exception to wrap @option opts [String] :msg the exception message (if not present, a default message will be constructed) @option opts [String] :url the request URL, if any @option opts [Hash] :params the query or form parameters, if any @option opts [String] :msg_context context information to prepend to the default message

# File lib/berkeley_library/tind/api/api_exception.rb, line 65
def wrap(ex, **opts)
  raise ArgumentError, "Can't wrap a nil error" unless ex

  msg = opts[:msg] || message_from(ex, opts[:url], opts[:params], opts[:detail])
  options = format_options(ex, opts[:url], opts[:params])
  APIException.new(msg, **options)
end

Public Instance Methods

body() click to toggle source
# File lib/berkeley_library/tind/api/api_exception.rb, line 42
def body
  return @body if instance_variable_defined?(:@body)

  @body = response && response.body
end

Private Instance Methods

format_status(status_code) click to toggle source
# File lib/berkeley_library/tind/api/api_exception.rb, line 50
def format_status(status_code)
  return unless (numeric_status = Integer(status_code, exception: false))

  status_name = Net::HTTP::STATUS_CODES[numeric_status]
  default_status_message = [numeric_status, status_name].compact.join(' ')

  [numeric_status, default_status_message]
end