Exception: Karatekit::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Karatekit::Error
- Defined in:
- lib/karatekit/error.rb
Overview
Custom error class for rescuing from all kampfsport.center errors
Direct Known Subclasses
Class Method Summary collapse
-
.error_for_401(headers) ⇒ Object
Returns most appropriate error for 401 HTTP status code.
-
.error_for_403(body) ⇒ Object
Returns most appropriate error for 403 HTTP status code.
-
.error_for_404(body) ⇒ Object
Return most appropriate error for 404 HTTP status code.
-
.from_response(response) ⇒ Karatekit::Error
Returns the appropriate Karatekit::Error subclass based on status and response message.
Instance Method Summary collapse
-
#documentation_url ⇒ String
Documentation URL returned by the API for some errors.
-
#errors ⇒ Array<Hash>
Array of validation errors.
-
#initialize(response = nil) ⇒ Error
constructor
A new instance of Error.
-
#response_body ⇒ String
Body returned by the kampfsport.center server.
-
#response_headers ⇒ Hash
Headers returned by the kampfsport.center server.
-
#response_status ⇒ Integer
Status code returned by the kampfsport.center server.
Constructor Details
#initialize(response = nil) ⇒ Error
Returns a new instance of Error.
37 38 39 40 |
# File 'lib/karatekit/error.rb', line 37 def initialize(response=nil) @response = response super() end |
Class Method Details
.error_for_401(headers) ⇒ Object
Returns most appropriate error for 401 HTTP status code
51 52 53 |
# File 'lib/karatekit/error.rb', line 51 def self.error_for_401(headers) Karatekit::Unauthorized end |
.error_for_403(body) ⇒ Object
Returns most appropriate error for 403 HTTP status code
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/karatekit/error.rb', line 57 def self.error_for_403(body) if body =~ /rate limit exceeded/i Karatekit::TooManyRequests elsif body =~ /login attempts exceeded/i Karatekit::TooManyLoginAttempts elsif body =~ /abuse/i Karatekit::AbuseDetected elsif body =~ /email address must be verified/i Karatekit::UnverifiedEmail elsif body =~ /account was suspended/i Karatekit::AccountSuspended else Karatekit::Forbidden end end |
.error_for_404(body) ⇒ Object
Return most appropriate error for 404 HTTP status code
75 76 77 |
# File 'lib/karatekit/error.rb', line 75 def self.error_for_404(body) Karatekit::NotFound end |
.from_response(response) ⇒ Karatekit::Error
Returns the appropriate Karatekit::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/karatekit/error.rb', line 10 def self.from_response(response) status = response[:status].to_i body = response[:body].to_s headers = response[:response_headers] if klass = case status when 400 then Karatekit::BadRequest when 401 then error_for_401(headers) when 403 then error_for_403(body) when 404 then error_for_404(body) when 405 then Karatekit::MethodNotAllowed when 406 then Karatekit::NotAcceptable when 409 then Karatekit::Conflict when 415 then Karatekit::UnsupportedMediaType when 422 then Karatekit::UnprocessableEntity when 451 then Karatekit::UnavailableForLegalReasons when 400..499 then Karatekit::ClientError when 500 then Karatekit::InternalServerError when 501 then Karatekit::NotImplemented when 502 then Karatekit::BadGateway when 503 then Karatekit::ServiceUnavailable when 500..599 then Karatekit::ServerError end klass.new(response) end end |
Instance Method Details
#documentation_url ⇒ String
Documentation URL returned by the API for some errors
45 46 47 |
# File 'lib/karatekit/error.rb', line 45 def documentation_url data[:documentation_url] if data.is_a? Hash end |
#errors ⇒ Array<Hash>
Array of validation errors
81 82 83 84 85 86 87 |
# File 'lib/karatekit/error.rb', line 81 def errors if data && data.is_a?(Hash) data[:errors] || [] else [] end end |
#response_body ⇒ String
Body returned by the kampfsport.center server.
106 107 108 |
# File 'lib/karatekit/error.rb', line 106 def response_body @response[:body] end |
#response_headers ⇒ Hash
Headers returned by the kampfsport.center server.
99 100 101 |
# File 'lib/karatekit/error.rb', line 99 def response_headers @response[:response_headers] end |
#response_status ⇒ Integer
Status code returned by the kampfsport.center server.
92 93 94 |
# File 'lib/karatekit/error.rb', line 92 def response_status @response[:status] end |