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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



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

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



4
5
6
# File 'lib/octokit/error.rb', line 4

def context
  @context
end

Class Method Details

.error_for_401(headers) ⇒ Object

Returns most appropriate error for 401 HTTP status code



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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/octokit/error.rb', line 68

def self.error_for_403(body)
  if body =~ /rate limit exceeded/i
    Octokit::TooManyRequests
  elsif body =~ /login attempts exceeded/i
    Octokit::TooManyLoginAttempts
  elsif body =~ /returns blobs up to [0-9]+ MB/i
    Octokit::TooLargeContent
  elsif body =~ /abuse/i
    Octokit::AbuseDetected
  elsif body =~ /repository access blocked/i
    Octokit::RepositoryUnavailable
  elsif body =~ /email address must be verified/i
    Octokit::UnverifiedEmail
  elsif body =~ /account was suspended/i
    Octokit::AccountSuspended
  elsif body =~ /billing issue/i
    Octokit::BillingIssue
  elsif body =~ /Resource protected by organization SAML enforcement/i
    Octokit::SAMLProtected
  elsif body =~ /suspended your access|This installation has been suspended/i
    Octokit::InstallationSuspended
  else
    Octokit::Forbidden
  end
end

.error_for_404(body) ⇒ Object

Return most appropriate error for 404 HTTP status code



96
97
98
99
100
101
102
# File 'lib/octokit/error.rb', line 96

def self.error_for_404(body)
  if body =~ /Branch not protected/i
    Octokit::BranchNotProtected
  else
    Octokit::NotFound
  end
end

.error_for_422(body) ⇒ Object

Return most appropriate error for 422 HTTP status code



106
107
108
109
110
111
112
# File 'lib/octokit/error.rb', line 106

def self.error_for_422(body)
  if body =~ /PullRequestReviewComment/i && body =~ /(commit_id|end_commit_oid) is not part of the pull request/i
    Octokit::CommitIsNotPartOfPullRequest
  else
    Octokit::UnprocessableEntity
  end
end

.from_response(response) ⇒ Octokit::Error

Returns the appropriate Octokit::Error subclass 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
  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 error_for_404(body)
              when 405      then Octokit::MethodNotAllowed
              when 406      then Octokit::NotAcceptable
              when 409      then Octokit::Conflict
              when 415      then Octokit::UnsupportedMediaType
              when 422      then error_for_422(body)
              when 451      then Octokit::UnavailableForLegalReasons
              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

#build_error_contextObject



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

def build_error_context
  if RATE_LIMITED_ERRORS.include?(self.class)
    @context = Octokit::RateLimit.from_response(@response)
  end
end

#documentation_urlString

Documentation URL returned by the API for some errors

Returns:

  • (String)


52
53
54
# File 'lib/octokit/error.rb', line 52

def documentation_url
  data[:documentation_url] if data.is_a? Hash
end

#errorsArray<Hash>

Array of validation errors

Returns:

  • (Array<Hash>)

    Error info



116
117
118
119
120
121
122
# File 'lib/octokit/error.rb', line 116

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

#response_bodyString

Body returned by the GitHub server.

Returns:

  • (String)


141
142
143
# File 'lib/octokit/error.rb', line 141

def response_body
  @response[:body]
end

#response_headersHash

Headers returned by the GitHub server.

Returns:

  • (Hash)


134
135
136
# File 'lib/octokit/error.rb', line 134

def response_headers
  @response[:response_headers]
end

#response_statusInteger

Status code returned by the GitHub server.

Returns:

  • (Integer)


127
128
129
# File 'lib/octokit/error.rb', line 127

def response_status
  @response[:status]
end