Sha256: 1477ab790517bdacba0fa9edd62aa6e6881677ebb5a03c74bcee45c21ca5bd2b

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require 'json'

module Fintecture
  class ValidationException < RuntimeError; end

  class CryptoException < RuntimeError; end

  class ApiException
    class << self
      def error(res)
        body = JSON.parse res.body

        # Errors array
        if body['code'] && body['log_id'] && body['errors']
          raise _construct_message_errors(res, body)
        # Single error
        else
          raise _construct_message_error(res, body)
        end
      end

      private

      def _construct_message_errors(res, body)
        status = res.status
        code = body['code']
        log_id = body['log_id']
        errors_array = body['errors']

        error_string = "\nFintecture server errors : "
        error_string += "\n status: #{status} "
        error_string += "\n code: #{code}"
        error_string += "\n id : #{log_id}"

        errors_array.compact.each do |error|
          formated_error = error
          formated_error = error.map { |key, value| "   #{key}: #{value}" }.join("\n") if error.is_a?(Hash)
          error_string += "\n\n#{formated_error}"
        end
        error_string += "\n\n"

        {
          type: 'Fintecture api',
          status: status,
          errors: errors_array,
          error_string: error_string
        }.to_json
      end

      def _construct_message_error(res, body)
        status = res.status
        error = body['meta']

        error_string = "\nFintecture server errors : "
        error_string += "\n status: #{status} "

        formated_error = error
        formated_error = error.map { |key, value| "   #{key}: #{value}" }.join("\n") if error.is_a?(Hash)

        error_string += "\n\n#{formated_error}"

        error_string += "\n\n"

        {
          type: 'Fintecture api',
          status: status,
          errors: [error],
          error_string: error_string
        }.to_json
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fintecture-0.5.0 lib/fintecture/exceptions.rb
fintecture-0.4.1 lib/fintecture/exceptions.rb