Sha256: c5c52fef087b8f6478190bc115149a1f1e989e46eb47a4371de6de7014536d11

Contents?: true

Size: 1.47 KB

Versions: 7

Compression:

Stored size: 1.47 KB

Contents

require_relative 'utils'

class Fanforce
  class Error < StandardError
    attr_accessor :path, :query_params, :request, :response, :request_url, :http_status

    def initialize(response, request, path, query_params)
      @query_params = query_params
      @path = path
      @request = request
      begin
        @response = MultiJson.load(response, :symbolize_keys => true)
      rescue Exception => e
        @response = "Error decoding JSON in Fanforce gem: #{e.message}"
      end
      @request_url = "#{@path}?#{Fanforce::Utils.to_query_string(@query_params)}"
      super(@response.is_a?(Hash) ? @response[:msg] : @response)
    end

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

    def code; end
  end

  class BadRequestError          < Error
    def code; @http_status = 400     end
  end

  class Unauthorized             < Error
    def code; @http_status = 401     end
  end

  class ForbiddenError           < Error
    def code; @http_status = 403     end
  end

  class NotFoundError            < Error
    def code; @http_status = 404     end
  end

  class UnprocessableEntityError < Error
    def code; @http_status = 422     end
  end

  class UnknownError             < Error
    def code; @http_status ||= 500; 500 end
    def initialize(response, request, path, query_params)
      @http_status = response.code
      super(response, request, path, query_params)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fanforce-0.5.10 lib/fanforce/errors.rb
fanforce-0.5.8 lib/fanforce/errors.rb
fanforce-0.5.7 lib/fanforce/errors.rb
fanforce-0.5.6 lib/fanforce/errors.rb
fanforce-0.5.5 lib/fanforce/errors.rb
fanforce-0.5.4 lib/fanforce/errors.rb
fanforce-0.5.3 lib/fanforce/errors.rb