Exception: BoletoSimples::ResponseError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/boletosimples/response_error.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ ResponseError

Returns a new instance of ResponseError.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/boletosimples/response_error.rb', line 27

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



25
26
27
# File 'lib/boletosimples/response_error.rb', line 25

def body
  @body
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



25
26
27
# File 'lib/boletosimples/response_error.rb', line 25

def error_message
  @error_message
end

#methodObject (readonly)

Returns the value of attribute method.



25
26
27
# File 'lib/boletosimples/response_error.rb', line 25

def method
  @method
end

#responseObject (readonly)

Returns the value of attribute response.



25
26
27
# File 'lib/boletosimples/response_error.rb', line 25

def response
  @response
end

#statusObject (readonly)

Returns the value of attribute status.



25
26
27
# File 'lib/boletosimples/response_error.rb', line 25

def status
  @status
end

#urlObject (readonly)

Returns the value of attribute url.



25
26
27
# File 'lib/boletosimples/response_error.rb', line 25

def url
  @url
end

Instance Method Details

#to_sObject



41
42
43
44
45
46
# File 'lib/boletosimples/response_error.rb', line 41

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