lib/pagarme/errors.rb in pagarme-2.0.2 vs lib/pagarme/errors.rb in pagarme-2.1.0

- old
+ new

@@ -1,41 +1,55 @@ module PagarMe class PagarMeError < StandardError - attr_accessor :message - attr_accessor :parameter_name - attr_accessor :type - attr_accessor :url - attr_accessor :errors + end - def initialize(message = "", parameter_name = "", type = "", url = "") - self.message = message - self.type = type - self.parameter_name = parameter_name - self.errors = [] + class ConnectionError < PagarMeError + attr_reader :error + + def initialize(error) + @error = error + super error.message end + end - def self.fromServerResponse(response = {}) - object = self.new + class RequestError < PagarMeError + end - response['errors'].map do |error| - object.message += error['message'] + ', ' - object.errors << PagarMeError.new(error['message'], error['parameter_name'], error['type'], response['url']) - end + class ResponseError < PagarMeError + attr_reader :request_params, :error - object + def initialize(request_params, error) + @request_params, @error = request_params, error + super @error.message end + end - def to_s - "#{self.class.to_s} - #{message}" + class NotFound < ResponseError + attr_reader :response + def initialize(response, request_params, error) + @response = response + super request_params, error end end - class RequestError < PagarMeError - end + class ValidationError < PagarMeError + attr_reader :response, :errors - class ConnectionError < PagarMeError + def initialize(response) + @response = response + @errors = response['errors'].map do |error| + params = error.values_at('message', 'parameter_name', 'type', 'url') + ParamError.new *params + end + super @errors.map(&:message).join(', ') + end end - class ResponseError < PagarMeError + class ParamError < PagarMeError + attr_reader :parameter_name, :type, :url + + def initialize(message, parameter_name, type, url) + @parameter_name, @type, @url = parameter_name, type, url + super message + end end end -