utils/github: add ValidationFailedError.

These have specific errors returned so would be more useful to output.
This commit is contained in:
Mike McQuaid 2018-10-15 10:39:47 -07:00
parent 473155e12c
commit 056dd588b2
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70

View File

@ -45,7 +45,7 @@ module GitHub
class AuthenticationFailedError < Error
def initialize(github_message)
@github_message = github_message
message = "GitHub #{github_message}\n"
message = "GitHub #{github_message}:"
if ENV["HOMEBREW_GITHUB_API_TOKEN"]
message << <<~EOS
HOMEBREW_GITHUB_API_TOKEN may be invalid or expired; check:
@ -65,6 +65,18 @@ module GitHub
end
end
class ValidationFailedError < Error
def initialize(github_message, errors)
@github_message = if errors.empty?
github_message
else
"#{github_message}: #{errors}"
end
super(@github_message)
end
end
def api_credentials
@api_credentials ||= begin
if ENV["HOMEBREW_GITHUB_API_TOKEN"]
@ -231,6 +243,9 @@ module GitHub
raise AuthenticationFailedError, message
when "404"
raise HTTPNotFoundError, message
when "422"
errors = json&.[]("errors") || []
raise ValidationFailedError.new(message, errors)
else
raise Error, message
end