class Bugsnag::Api::Error

Custom error class for rescuing from all Bugsnag API errors

Public Class Methods

from_response(response) click to toggle source

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

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

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

Private Instance Methods

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

  message =  "#{@response[:method].to_s.upcase} "
  message << @response[:url].to_s + ": "
  message << "#{@response[:status]} - "
  message << "#{response_error}" unless response_error.nil?
  message
end
data() click to toggle source
# File lib/bugsnag/api/error.rb, line 45
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
response_error() click to toggle source
# File lib/bugsnag/api/error.rb, line 61
def response_error
  "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error]
end