Exception: Bitsor::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/bitsor/error.rb

Direct Known Subclasses

ClientError, ServerError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bitsor/error.rb', line 27

def initialize(response = nil)
  @response = response
  @request = response.request
  @body = { error: {} }

  begin
    if response.body && !response.body.empty?
      @body = JSON.parse(response.body, symbolize_names: true)
    end
  rescue JSON::ParserError => e
    @body = { error: { code: response.response_code, message: 'Internal Server Error: An Error Was Encountered' } }
  end

  super(build_error_message)
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



51
52
53
# File 'lib/bitsor/error.rb', line 51

def body
  @body
end

#requestObject

Returns the value of attribute request.



51
52
53
# File 'lib/bitsor/error.rb', line 51

def request
  @request
end

#responseObject

Returns the value of attribute response.



51
52
53
# File 'lib/bitsor/error.rb', line 51

def response
  @response
end

Class Method Details

.from_response(response) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bitsor/error.rb', line 5

def self.from_response(response)
  status = response.response_code

  if klass =  case status
              when 400      then Bitsor::BadRequest
              when 401      then Bitsor::Forbidden
              when 403      then Bitsor::Forbidden
              when 404      then Bitsor::NotFound
              when 405      then Bitsor::MethodNotAllowed
              when 406      then Bitsor::NotAcceptable
              when 422      then Bitsor::UnprocessableEntity
              when 400..499 then Bitsor::ClientError
              when 500      then Bitsor::InternalServerError
              when 501      then Bitsor::NotImplemented
              when 502      then Bitsor::BadGateway
              when 503      then Bitsor::ServiceUnavailable
              when 500..599 then Bitsor::ServerError
              end
    klass.new(response)
  end
end

Instance Method Details

#build_error_messageObject



43
44
45
46
47
48
49
# File 'lib/bitsor/error.rb', line 43

def build_error_message
  return nil if @response.nil?
  message =  ["#{@request.options[:method].to_s.upcase} "]
  message << @response.options[:effective_url].to_s + "\n"
  message << "Code #{@body[:error][:code]}: #{@body[:error][:message]} \n"
  message.join
end