Sha256: d395caf3375fb32302208f3ffa1ab77a5c1efd971c591b3fa6d4529d26bc4a8e

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

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://api-sandbox.kobana.com.br/v1/bank_billets
  #
  #   begin
  #     BoletoSimples::BankBillet.all
  #   rescue BoletoSimples::ResponseError => response
  #     response.status # => 401
  #     response.method # => "GET"
  #     response.url # => "https://api-sandbox.kobana.com.br/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]

      errors = response[:body][:errors]
      @error_message = errors.first[:title] unless errors.blank?

      super
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
boletosimples-2.1.0 lib/boletosimples/response_error.rb
boletosimples-2.0.0 lib/boletosimples/response_error.rb