Exception: Bitsor::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Bitsor::Error
- Defined in:
- lib/bitsor/error.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
- #build_error_message ⇒ Object
-
#initialize(response = nil) ⇒ Error
constructor
A new instance of Error.
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() end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
51 52 53 |
# File 'lib/bitsor/error.rb', line 51 def body @body end |
#request ⇒ Object
Returns the value of attribute request.
51 52 53 |
# File 'lib/bitsor/error.rb', line 51 def request @request end |
#response ⇒ Object
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_message ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/bitsor/error.rb', line 43 def return nil if @response.nil? = ["#{@request.[:method].to_s.upcase} "] << @response.[:effective_url].to_s + "\n" << "Code #{@body[:error][:code]}: #{@body[:error][:message]} \n" .join end |