Sha256: 50689cb3be3741a35bfb4e2f4016c9d9d656dce4bed4fee4e63792290a46257c

Contents?: true

Size: 1.34 KB

Versions: 11

Compression:

Stored size: 1.34 KB

Contents

module BoletoSimples
  # BoletoSimples::ResponseError
  # Exception that gets raised if the response is an error (4xx or 5xx)
  #
  # Formats a readable error message including HTTP status, method and requested URL
  #
  # Examples:
  #
  #   BoletoSimples::BankBillet.all
  #   BoletoSimples::ResponseError: 401 POST https://sandbox.boletosimples.com.br/api/v1/bank_billets
  #
  #   begin
  #     BoletoSimples::BankBillet.all
  #   rescue BoletoSimples::ResponseError => response
  #     response.status # => 401
  #     response.method # => "GET"
  #     response.url # => "https://sandbox.boletosimples.com.br/api/v1/bank_billets"
  #     response.body # => "{"error":"VocĂȘ precisa se logar ou registrar antes de prosseguir."}"
  #     response.error_message # => "VocĂȘ precisa se logar ou registrar antes de prosseguir."
  #   end
  #
  class ResponseError < StandardError
    attr_reader :response, :body, :status, :method, :url, :error_message

    def initialize(response = nil)
      @response = response

      @body = response[:body][:data]
      @status = response[:status].to_i
      @method = response[:method].to_s.upcase
      @url = response[:url]
      @error_message = @body[:error]

      super
    end

    def to_s
      msg = "#{status} #{method} #{url}"
      msg << " (#{error_message})" unless error_message.blank?
      msg
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
boletosimples-0.6.0 lib/boletosimples/response_error.rb
boletosimples-0.5.0 lib/boletosimples/response_error.rb
boletosimples-0.4.2 lib/boletosimples/response_error.rb
boletosimples-0.4.1 lib/boletosimples/response_error.rb
boletosimples-0.4.0 lib/boletosimples/response_error.rb
boletosimples-0.3.1 lib/boletosimples/response_error.rb
boletosimples-0.3.0 lib/boletosimples/response_error.rb
boletosimples-0.2.3 lib/boletosimples/response_error.rb
boletosimples-0.2.2 lib/boletosimples/response_error.rb
boletosimples-0.2.0 lib/boletosimples/response_error.rb
boletosimples-0.1.1 lib/boletosimples/response_error.rb