class Buildkit::Error

Custom error class for rescuing from all Buildkite errors

Public Class Methods

from_response(response) click to toggle source

Returns the appropriate Buildkit::Error subclass based on status and response message

@param [Hash] response HTTP response @return [Buildkit::Error]

# File lib/buildkit/error.rb, line 11
def self.from_response(response)
  status = response[:status].to_i
  if klass =  case status
              when 400      then Buildkit::BadRequest
              when 401      then Buildkit::Unauthorized
              when 403      then Buildkit::Forbidden
              when 404      then Buildkit::NotFound
              when 405      then Buildkit::MethodNotAllowed
              when 406      then Buildkit::NotAcceptable
              when 409      then Buildkit::Conflict
              when 415      then Buildkit::UnsupportedMediaType
              when 422      then Buildkit::UnprocessableEntity
              when 400..499 then Buildkit::ClientError
              when 500      then Buildkit::InternalServerError
              when 501      then Buildkit::NotImplemented
              when 502      then Buildkit::BadGateway
              when 503      then Buildkit::ServiceUnavailable
              when 500..599 then Buildkit::ServerError
              end
    klass.new(response)
  end
end
new(response = nil) click to toggle source
Calls superclass method
# File lib/buildkit/error.rb, line 34
def initialize(response = nil)
  @response = response
  super(build_error_message)
end

Public Instance Methods

documentation_url() click to toggle source

Documentation URL returned by the API for some errors

@return [String]

# File lib/buildkit/error.rb, line 42
def documentation_url
  data[:documentation_url] if data.is_a? Hash
end
errors() click to toggle source

Array of validation errors @return [Array<Hash>] Error info

# File lib/buildkit/error.rb, line 48
def errors
  if data&.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end

Private Instance Methods

build_error_message() click to toggle source
# File lib/buildkit/error.rb, line 102
    def build_error_message
      return nil if @response.nil?

      documentation_text = ''
      documentation_text = "// See: #{documentation_url}" if documentation_url

      <<~MSG.strip
        #{@response[:method].to_s.upcase} #{redact_url(@response[:url].to_s)}: #{@response[:status]} - #{response_message}#{response_error}#{response_error_summary}
        #{documentation_text}
      MSG
    end
data() click to toggle source
# File lib/buildkit/error.rb, line 58
def data
  @data ||= parse_data
end
parse_data() click to toggle source
# File lib/buildkit/error.rb, line 62
def parse_data
  body = @response[:body]
  return if body.empty?
  return body unless body.is_a?(String)

  headers = @response[:response_headers]
  content_type = headers && headers[:content_type] || ''
  if content_type =~ /json/
    Sawyer::Agent.serializer.decode(body)
  else
    body
  end
end
redact_url(url_string) click to toggle source
# File lib/buildkit/error.rb, line 114
def redact_url(url_string)
  %w[client_secret access_token].each do |token|
    url_string = url_string.gsub(/#{token}=\S+/, "#{token}=(redacted)") if url_string.include? token
  end
  url_string
end
response_error() click to toggle source
# File lib/buildkit/error.rb, line 85
def response_error
  "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error]
end
response_error_summary() click to toggle source
# File lib/buildkit/error.rb, line 89
    def response_error_summary
      return nil unless data.is_a?(Hash) && !Array(data[:errors]).empty?

      errors = data[:errors].map do |hash|
        hash.map { |k, v| "  #{k}: #{v}" }
      end

      <<~MSG.chomp
        Error summary:
        #{errors.join("\n")}
      MSG
    end
response_message() click to toggle source
# File lib/buildkit/error.rb, line 76
def response_message
  case data
  when Hash
    data[:message]
  when String
    data
  end
end