Exception: Octokit::Error

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

Overview

Custom error class for rescuing from all GitHub errors

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



35
36
37
38
# File 'lib/octokit/error.rb', line 35

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

Class Method Details

.error_for_401(headers) ⇒ Object

Returns most appropriate error for 401 HTTP status code



49
50
51
52
53
54
55
# File 'lib/octokit/error.rb', line 49

def self.error_for_401(headers)
  if Octokit::OneTimePasswordRequired.required_header(headers)
    Octokit::OneTimePasswordRequired
  else
    Octokit::Unauthorized
  end
end

.error_for_403(body) ⇒ Object

Returns most appropriate error for 403 HTTP status code



59
60
61
62
63
64
65
66
67
# File 'lib/octokit/error.rb', line 59

def self.error_for_403(body)
  if body =~ /rate limit exceeded/i
    Octokit::TooManyRequests
  elsif body =~ /login attempts exceeded/i
    Octokit::TooManyLoginAttempts
  else
    Octokit::Forbidden
  end
end

.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
# File 'lib/octokit/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 Octokit::BadRequest
              when 401      then error_for_401(headers)
              when 403      then error_for_403(body)
              when 404      then Octokit::NotFound
              when 406      then Octokit::NotAcceptable
              when 409      then Octokit::Conflict
              when 415      then Octokit::UnsupportedMediaType
              when 422      then Octokit::UnprocessableEntity
              when 400..499 then Octokit::ClientError
              when 500      then Octokit::InternalServerError
              when 501      then Octokit::NotImplemented
              when 502      then Octokit::BadGateway
              when 503      then Octokit::ServiceUnavailable
              when 500..599 then Octokit::ServerError
              end
    klass.new(response)
  end
end

Instance Method Details

#documentation_urlString

Documentation URL returned by the API for some errors

Returns:

  • (String)


43
44
45
# File 'lib/octokit/error.rb', line 43

def documentation_url
  data[:documentation_url] if data
end

#errorsArray<Hash>

Array of validation errors

Returns:

  • (Array<Hash>)

    Error info



71
72
73
74
75
76
77
# File 'lib/octokit/error.rb', line 71

def errors
  if data && data.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end