Exception: MgmtConsole::Error
- Inherits:
-
StandardError
- Object
- StandardError
- MgmtConsole::Error
- Defined in:
- lib/mgmt_console/error.rb
Overview
Custom error class for rescuing from all GitHub 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.
-
.from_response(response) ⇒ MgmtConsole::Error
Returns the appropriate MgmtConsole::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.
Constructor Details
#initialize(response = nil) ⇒ Error
Returns a new instance of Error.
36 37 38 39 |
# File 'lib/mgmt_console/error.rb', line 36 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
50 51 52 53 54 55 56 |
# File 'lib/mgmt_console/error.rb', line 50 def self.error_for_401(headers) if MgmtConsole::OneTimePasswordRequired.required_header(headers) MgmtConsole::OneTimePasswordRequired else MgmtConsole::Unauthorized end end |
.error_for_403(body) ⇒ Object
Returns most appropriate error for 403 HTTP status code
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mgmt_console/error.rb', line 60 def self.error_for_403(body) if body =~ /rate limit exceeded/i MgmtConsole::TooManyRequests elsif body =~ /login attempts exceeded/i MgmtConsole::TooManyLoginAttempts elsif body =~ /abuse/i MgmtConsole::AbuseDetected elsif body =~ /repository access blocked/i MgmtConsole::RepositoryUnavailable else MgmtConsole::Forbidden end end |
.from_response(response) ⇒ MgmtConsole::Error
Returns the appropriate MgmtConsole::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 |
# File 'lib/mgmt_console/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 MgmtConsole::BadRequest when 401 then error_for_401(headers) when 403 then error_for_403(body) when 404 then MgmtConsole::NotFound when 405 then MgmtConsole::MethodNotAllowed when 406 then MgmtConsole::NotAcceptable when 409 then MgmtConsole::Conflict when 415 then MgmtConsole::UnsupportedMediaType when 422 then MgmtConsole::UnprocessableEntity when 400..499 then MgmtConsole::ClientError when 500 then MgmtConsole::InternalServerError when 501 then MgmtConsole::NotImplemented when 502 then MgmtConsole::BadGateway when 503 then MgmtConsole::ServiceUnavailable when 500..599 then MgmtConsole::ServerError end klass.new(response) end end |
Instance Method Details
#documentation_url ⇒ String
Documentation URL returned by the API for some errors
44 45 46 |
# File 'lib/mgmt_console/error.rb', line 44 def documentation_url data[:documentation_url] if data.is_a? Hash end |
#errors ⇒ Array<Hash>
Array of validation errors
76 77 78 79 80 81 82 |
# File 'lib/mgmt_console/error.rb', line 76 def errors if data && data.is_a?(Hash) data[:errors] || [] else [] end end |