class Nearmiss::Error

Custom error class for rescuing from all GitHub errors

Public Class Methods

from_response(response) click to toggle source

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

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

# File lib/nearmiss-ruby/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 Nearmiss::BadRequest
              when 401      then Nearmiss::Unauthorized
              when 403      then Nearmiss::Forbidden
              when 404      then Nearmiss::NotFound
              when 405      then Nearmiss::MethodNotAllowed
              when 406      then Nearmiss::NotAcceptable
              when 409      then Nearmiss::Conflict
              when 415      then Nearmiss::UnsupportedMediaType
              when 422      then Nearmiss::UnprocessableEntity
              when 400..499 then Nearmiss::ClientError
              when 500      then Nearmiss::InternalServerError
              when 501      then Nearmiss::NotImplemented
              when 502      then Nearmiss::BadGateway
              when 503      then Nearmiss::ServiceUnavailable
              when 500..599 then Nearmiss::ServerError
              end
    klass.new(response)
  end
end
new(response=nil) click to toggle source
Calls superclass method
# File lib/nearmiss-ruby/error.rb, line 38
def initialize(response=nil)
  @response = response
  super(build_error_message)
end

Public Instance Methods

build_error_message() click to toggle source
# File lib/nearmiss-ruby/error.rb, line 95
def build_error_message
  return nil if @response.nil?



  message =  "#{@response[:method].to_s.upcase} "
  message << redact_url(@response[:url].to_s) + ": "
  message << "#{@response[:status]} - "
  message << "#{response_message}" unless response_message.nil?
  message << "#{response_error}" unless response_error.nil?
  message << "#{response_error_summary}" unless response_error_summary.nil?
  # message << MultiJson.dump(@response)
  message
end
data() click to toggle source

private

# File lib/nearmiss-ruby/error.rb, line 55
def data
  @data ||=
    if (body = @response[:body]) && !body.empty?
      if body.is_a?(String) &&
        @response[:response_headers] &&
        @response[:response_headers][:content_type] =~ /json/

        Sawyer::Agent.serializer.decode(body)
      else
        body
      end
    else
      nil
    end
end
errors() click to toggle source

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

# File lib/nearmiss-ruby/error.rb, line 45
def errors
  if data && data.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end
redact_url(url_string) click to toggle source
# File lib/nearmiss-ruby/error.rb, line 110
def redact_url(url_string)
  %w[client_secret access_token].each do |token|
    url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)") if url_string.include? token
  end
  url_string
end
response_error() click to toggle source
# File lib/nearmiss-ruby/error.rb, line 80
def response_error
  "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error]
end
response_error_summary() click to toggle source
# File lib/nearmiss-ruby/error.rb, line 84
def response_error_summary
  return nil unless data.is_a?(Hash) && !Array(data[:errors]).empty?

  summary = "\nError summary:\n"
  # summary << data[:errors].map do |hash|
    # hash.map { |k,v| "  #{k}: #{v}" }
  # end.join("\n")
  summary << data[:errors].join("\n")
  summary
end
response_message() click to toggle source
# File lib/nearmiss-ruby/error.rb, line 71
def response_message
  case data
  when Hash
    data[:message]
  when String
    data
  end
end