Exception: JMA::Error
- Inherits:
-
StandardError
- Object
- StandardError
- JMA::Error
- Defined in:
- lib/jma/error.rb
Overview
Custom error class for rescuing from all API errors Inspired by github.com/octokit/octokit.rb
Direct Known Subclasses
Class Method Summary collapse
-
.error_for_401 ⇒ 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 ⇒ Object
Return most appropriate error for 404 HTTP status code.
-
.from_response(response) ⇒ JMA::Error
Returns the appropriate JMA::Error subclass based on status and response message.
- .match_status(status, body) ⇒ Object
- .match_status_410_500(status) ⇒ Object
- .match_status_over_500(status) ⇒ Object
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.
78 79 80 81 |
# File 'lib/jma/error.rb', line 78 def initialize(response = nil) @response = response super() end |
Class Method Details
.error_for_401 ⇒ Object
Returns most appropriate error for 401 HTTP status code
85 86 87 |
# File 'lib/jma/error.rb', line 85 def self.error_for_401 JMA::Unauthorized end |
.error_for_403(body) ⇒ Object
Returns most appropriate error for 403 HTTP status code
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/jma/error.rb', line 91 def self.error_for_403(body) case body when /rate limit exceeded/i JMA::TooManyRequests when /login attempts exceeded/i JMA::TooManyLoginAttempts when /abuse/i JMA::AbuseDetected else JMA::Forbidden end end |
.error_for_404 ⇒ Object
Return most appropriate error for 404 HTTP status code
106 107 108 |
# File 'lib/jma/error.rb', line 106 def self.error_for_404 JMA::NotFound end |
.from_response(response) ⇒ JMA::Error
Returns the appropriate JMA::Error subclass based on status and response message
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/jma/error.rb', line 12 def self.from_response(response) if response.methods.include? :code return unless (klass = match_status response.code.to_i, response.body.to_s) else return unless (klass = match_status response[:status].to_i, response[:body].to_s) end klass.new(response) end |
.match_status(status, body) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jma/error.rb', line 23 def self.match_status(status, body) case status when 400 JMA::BadRequest when 401 error_for_401 when 403 error_for_403 body when 404 error_for_404 body when 405 JMA::MethodNotAllowed when 406 JMA::NotAcceptable when 409 JMA::Conflict when 400..499 match_status_410_500 status when 500..599 match_status_over_500 status end end |
.match_status_410_500(status) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jma/error.rb', line 46 def self.match_status_410_500(status) case status when 415 JMA::UnsupportedMediaType when 422 JMA::UnprocessableEntity when 451 JMA::UnavailableForLegalReasons when 400..499 JMA::ClientError else JMA::ClientError end end |
.match_status_over_500(status) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/jma/error.rb', line 61 def self.match_status_over_500(status) case status when 500 JMA::InternalServerError when 501 JMA::NotImplemented when 502 JMA::BadGateway when 503 JMA::ServiceUnavailable when 500..599 JMA::ServerError else JMA::ServerError end end |
Instance Method Details
#build_error_message ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/jma/error.rb', line 110 def return nil if @response.nil? if @response.methods.include? :code "#{@response.code} - #{@response.}" else = "#{@response[:method].to_s.upcase} ".dup << "#{@response[:url]}: " << (@response[:status]).to_s end end |