Exception: BerkeleyLibrary::TIND::API::APIException

Inherits:
StandardError
  • Object
show all
Defined in:
lib/berkeley_library/tind/api/api_exception.rb

Overview

Wrapper for network-related exceptions.

Direct Known Subclasses

APIKeyNotSet, BaseURINotSet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, **opts) ⇒ APIException

Initializes a new APIException.

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :msg (String)

    the exception message (if not present, a default message will be constructed)

  • :url (String)

    the request URL, if any

  • :params (Hash)

    the query or form parameters, if any

  • :status_code (Integer)

    the numeric HTTP status code, if any

  • :status_message (String)

    a human-readable string representation of the HTTP status (if not present, a default will be constructed)

  • :response (RestClient::Response)

    the HTTP response, if any



32
33
34
35
36
37
38
39
40
# 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

Instance Attribute Details

#paramsHash? (readonly)

Returns the API query parameters, if any.

Returns:

  • (Hash, nil)

    the API query parameters, if any



12
13
14
# File 'lib/berkeley_library/tind/api/api_exception.rb', line 12

def params
  @params
end

#responseRestClient::Response? (readonly)

Returns the response, if any.

Returns:

  • (RestClient::Response, nil)

    the response, if any



21
22
23
# File 'lib/berkeley_library/tind/api/api_exception.rb', line 21

def response
  @response
end

#status_codeInteger? (readonly)

Returns the numeric HTTP status code, if any.

Returns:

  • (Integer, nil)

    the numeric HTTP status code, if any



15
16
17
# File 'lib/berkeley_library/tind/api/api_exception.rb', line 15

def status_code
  @status_code
end

#status_messageString? (readonly)

Returns the HTTP status message, if any.

Returns:

  • (String, nil)

    the HTTP status message, if any



18
19
20
# File 'lib/berkeley_library/tind/api/api_exception.rb', line 18

def status_message
  @status_message
end

#urlString? (readonly)

Returns the request URI, if any.

Returns:

  • (String, nil)

    the request URI, if any



9
10
11
# File 'lib/berkeley_library/tind/api/api_exception.rb', line 9

def url
  @url
end

Class Method Details

.wrap(ex, **opts) ⇒ Object

Parameters:

  • ex (Exception)

    the exception to wrap

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :msg (String)

    the exception message (if not present, a default message will be constructed)

  • :url (String)

    the request URL, if any

  • :params (Hash)

    the query or form parameters, if any

  • :msg_context (String)

    context information to prepend to the default message

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
# 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

Instance Method Details

#bodyObject



42
43
44
45
46
# 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