Sha256: 8611a2a1699a14c690986cefbb8772d6a9d78490b8a3210172b5bf86edf906a7

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

require_relative 'utils'

class Fanforce::API::Error < StandardError
  attr_accessor :response, :request, :requested_url, :requested_params, :errors

  def initialize(message, response, request, requested_url, requested_params)
    @response = response
    @request = request
    @requested_url = requested_url
    @requested_params = requested_params
    @message = message
    super(message)
  end

  def curl_command
    method = begin @request.method rescue nil end
    Fanforce::Utils.curl_command(method, @requested_url, @requested_params)
  end

  def code;    @response.respond_to?(:code) ? @response.code : 500 end
  def to_s;    @message end
  def to_hash; {}  end
end

class Fanforce::API::ServerError < Fanforce::API::Error

  def initialize(response, request, requested_url, requested_params)
    @response_body = response.to_s

    begin
      @response_hash = Fanforce::Utils.decode_json(response)
      @errors = @response_hash[:errors]
    rescue Exception => e
      raise Fanforce::API::DecodingError.new(e, response, request, requested_url, requested_params)
    end

    super(response.to_s, response, request, requested_url, requested_params)
  end

  def to_hash
    @response_hash
  end

  def curl_command
    method = begin @request.method rescue nil end
    Fanforce::Utils.curl_command(method, @requested_url, @requested_params)
  end

  def code; @response.code end

end

class Fanforce::API::DecodingError < Fanforce::API::Error

  def initialize(e, response, request, requested_url, requested_params)
    @message = "There was a Fanforce gem error while decoding JSON response (#{e.is_a?(String) ? e : e.message}): #{response.body}"
    @errors = [{message: @message}]
    super(@message, response, request, requested_url, requested_params)
  end

  def to_hash
    {errors: @errors}
  end

  def code; 500 end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fanforce-api-0.19.0 lib/fanforce/api/error.rb
fanforce-api-0.18.1 lib/fanforce/api/error.rb