class Breacan::Error

Public Class Methods

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

Public Instance Methods

errors() click to toggle source
# File lib/breacan/error.rb, line 34
def errors
  if data && data.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end

Private Instance Methods

build_error_message() click to toggle source
# File lib/breacan/error.rb, line 73
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
end
data() click to toggle source
# File lib/breacan/error.rb, line 44
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
redact_url(url_string) click to toggle source
# File lib/breacan/error.rb, line 84
def redact_url(url_string)
  %w[token 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/breacan/error.rb, line 69
def response_error
  "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error]
end
response_message() click to toggle source
# File lib/breacan/error.rb, line 60
def response_message
  case data
  when Hash
    data[:message]
  when String
    data
  end
end