Exception: Octokit::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/octokit/error.rb

Overview

Custom error class for rescuing from all GitHub errors

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



37
38
39
40
# File 'lib/octokit/error.rb', line 37

def initialize(response=nil)
  @response = response
  super(build_error_message)
end

Class Method Details

.from_response(response) ⇒ Octokit::Error

Returns the appropriate Octokit::Error sublcass based on status and response message

Parameters:

  • response (Hash)

    HTTP response

Returns:



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/octokit/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 Octokit::BadRequest
              when 401 then Octokit::Unauthorized
              when 403
                if body =~ /rate limit exceeded/i
                  Octokit::TooManyRequests
                elsif body =~ /login attempts exceeded/i
                  Octokit::TooManyLoginAttempts
                else
                  Octokit::Forbidden
                end
              when 404 then Octokit::NotFound
              when 406 then Octokit::NotAcceptable
              when 422 then Octokit::UnprocessableEntity
              when 500 then Octokit::InternalServerError
              when 501 then Octokit::NotImplemented
              when 502 then Octokit::BadGateway
              when 503 then Octokit::ServiceUnavailable
              end
    klass.new(response)
  end
end