Sha256: 8cd6b14391f1cfa44a63b712661eef3dcd8bcdfd8688e1cc4f97fe5d4255b915

Contents?: true

Size: 1.98 KB

Versions: 7

Compression:

Stored size: 1.98 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

    if Fanforce::Utils.is_present?(response.body)
      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
    else
      @errors = [{message: "(Response body from #{response.code} error was empty: #{response.body})"}]
    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

7 entries across 7 versions & 1 rubygems

Version Path
fanforce-api-0.31.0 lib/fanforce/api/error.rb
fanforce-api-0.30.0 lib/fanforce/api/error.rb
fanforce-api-0.23.0 lib/fanforce/api/error.rb
fanforce-api-0.22.0 lib/fanforce/api/error.rb
fanforce-api-0.21.0 lib/fanforce/api/error.rb
fanforce-api-0.20.0 lib/fanforce/api/error.rb
fanforce-api-0.19.2 lib/fanforce/api/error.rb