class Trell::Error

Public Class Methods

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

Public Instance Methods

errors() click to toggle source
# File lib/trell/error.rb, line 29
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/trell/error.rb, line 68
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/trell/error.rb, line 39
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/trell/error.rb, line 79
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/trell/error.rb, line 64
def response_error
  "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error]
end
response_message() click to toggle source
# File lib/trell/error.rb, line 55
def response_message
  case data
  when Hash
    data[:message]
  when String
    data
  end
end