Sha256: 1ccbb9e7278e91d621ee5dae70a59edb7c5923dcc658cf0d9d8bcb0b9a1c7ba6

Contents?: true

Size: 966 Bytes

Versions: 3

Compression:

Stored size: 966 Bytes

Contents

require 'faraday'

# @private
module Faraday
  # @private
  class Response::RaiseHttp4xx < Response::Middleware

    def on_complete(env)
      case env[:status].to_i
      when 400
        raise Genability::BadRequest, error_message(env)
      when 403
        raise Genability::Forbidden, error_message(env)
      when 404
        raise Genability::NotFound, error_message(env)
      end
    end

    private

    def error_message(env)
      "#{env[:method].to_s.upcase} #{env[:url].to_s} STATUS:#{env[:status]} #{error_body(env[:body])}"
    end

    def error_body(body)
      if body.nil?
        nil
      else
        "ERRORS:#{body.count}#{error_details(body)}"
      end
    end

    def error_details(body)
      msg = ""
      body.results.each_with_index do |result, i|
        msg << " #{i+1}[code:#{result.code} object_name:#{result.objectName} property_name:#{result.propertyName} message:#{result.message}]"
      end
      msg
    end


  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
genability-0.3.0 lib/faraday/response/raise_http_4xx.rb
genability-0.2.0 lib/faraday/response/raise_http_4xx.rb
genability-0.1.0 lib/faraday/response/raise_http_4xx.rb