class BerkeleyLibrary::TIND::API::APIException
Wrapper for network-related exceptions.
Attributes
@return [Hash, nil] the API
query parameters, if any
@return [RestClient::Response, nil] the response, if any
@return [Integer, nil] the numeric HTTP status code, if any
@return [String, nil] the HTTP status message, if any
@return [String, nil] the request URI, if any
Public Class Methods
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
# 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
# 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
# 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
@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
# 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
# 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