Sha256: 067879163803a5e2d61d9e3fa9735719bec64e705580980ceb64fafe4238aaea

Contents?: true

Size: 1.86 KB

Versions: 18

Compression:

Stored size: 1.86 KB

Contents

module MangoPay

  # Generic error superclass for MangoPay specific errors.
  # Currently never instantiated directly.
  # Currently only single subclass used.
  class Error < StandardError
  end

  # See http://docs.mangopay.com/api-references/response-codes-rules/
  # and http://docs.mangopay.com/api-references/error-codes/
  # 
  # Thrown from any MangoPay API call whenever
  # it returns response with HTTP code != 200.
  # Check @details hash for further info.
  # 
  # Two example exceptions with details:
  # 
  # #<MangoPay::ResponseError:
  #   One or several required parameters are missing or incorrect. [...]
  #   Email: The Email field is required.>
  # {"Message"=>"One or several required parameters are missing or incorrect.
  #   An incorrect resource ID also raises this kind of error.",
  #  "Type"=>"param_error",
  #  "Id"=>"66936e92-3f21-4a35-b6cf-f1d17c2fb6e5",
  #  "Date"=>1409047252.0,
  #  "errors"=>{"Email"=>"The Email field is required."},
  #  "Code"=>"400",
  #  "Url"=>"/v2/sdk-unit-tests/users/natural"}
  #  
  # #<MangoPay::ResponseError: Internal Server Error>
  # {"Message"=>"Internal Server Error",
  #  "Type"=>"other",
  #  "Id"=>"7bdc5c6f-2000-4cd3-96f3-2a3fcb746f07",
  #  "Date"=>1409047251.0,
  #  "errors"=>nil,
  #  "Code"=>"500",
  #  "Url"=>"/v2/sdk-unit-tests/payins/3380640/refunds"}  
  class ResponseError < Error

    attr_reader :request_url, :code, :details

    def initialize(request_url, code, details)
      @request_url, @code, @details = request_url, code, details

      @details['Code'] = code
      @details['Url'] = request_url.request_uri

      super(message) if message
    end

    def type;    @details['Type']; end
    def errors;  @details['errors']; end

    def message;
      msg = @details['Message']
      msg += errors.sort.map {|k,v| " #{k}: #{v}"}.join if (errors && errors.is_a?(Hash))
      msg
    end

  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
mangopay-3.3.0 lib/mangopay/errors.rb
mangopay-3.2.0 lib/mangopay/errors.rb
mangopay-3.1.0 lib/mangopay/errors.rb
mangopay-3.0.38 lib/mangopay/errors.rb
mangopay-3.0.37 lib/mangopay/errors.rb
mangopay-3.0.36 lib/mangopay/errors.rb
mangopay-3.0.35 lib/mangopay/errors.rb
mangopay-3.0.34 lib/mangopay/errors.rb
mangopay-3.0.33 lib/mangopay/errors.rb
mangopay-3.0.32 lib/mangopay/errors.rb
mangopay-3.0.31 lib/mangopay/errors.rb
mangopay-3.0.30.1 lib/mangopay/errors.rb
mangopay-3.0.30 lib/mangopay/errors.rb
mangopay-3.0.29 lib/mangopay/errors.rb
mangopay-3.0.28 lib/mangopay/errors.rb
mangopay-3.0.27 lib/mangopay/errors.rb
mangopay-3.0.26 lib/mangopay/errors.rb
mangopay-3.0.25.pre.alpha.pre.20 lib/mangopay/errors.rb