Exception: Ohanakapa::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Ohanakapa::Error
- Defined in:
- lib/ohanakapa/error.rb
Overview
Custom error class for rescuing from all Ohana API errors
Direct Known Subclasses
BadGateway, BadRequest, Forbidden, InternalServerError, NotAcceptable, NotFound, NotImplemented, ServiceUnavailable, Unauthorized, UnprocessableEntity
Class Method Summary collapse
-
.from_response(response) ⇒ Ohanakapa::Error
Returns the appropriate Ohanakapa::Error subclass based on status and response message.
Instance Method Summary collapse
-
#initialize(response = nil) ⇒ Error
constructor
A new instance of Error.
Constructor Details
#initialize(response = nil) ⇒ Error
Returns a new instance of Error.
37 38 39 40 |
# File 'lib/ohanakapa/error.rb', line 37 def initialize(response=nil) @response = response super() end |
Class Method Details
.from_response(response) ⇒ Ohanakapa::Error
Returns the appropriate Ohanakapa::Error subclass based on status and response message
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ohanakapa/error.rb', line 10 def self.from_response(response) status = response[:status].to_i body = response[:body].to_s if klass = case status when 400 then Ohanakapa::BadRequest when 401 then Ohanakapa::Unauthorized when 403 if body =~ /rate limit exceeded/i Ohanakapa::TooManyRequests elsif body =~ /login attempts exceeded/i Ohanakapa::TooManyLoginAttempts else Ohanakapa::Forbidden end when 404 then Ohanakapa::NotFound when 406 then Ohanakapa::NotAcceptable when 422 then Ohanakapa::UnprocessableEntity when 500 then Ohanakapa::InternalServerError when 501 then Ohanakapa::NotImplemented when 502 then Ohanakapa::BadGateway when 503 then Ohanakapa::ServiceUnavailable end klass.new(response) end end |