Sha256: 4a1c83b8fc648eef9761e0d4d33726fd7e1c072740e02a79f021beebaa14344d

Contents?: true

Size: 611 Bytes

Versions: 3

Compression:

Stored size: 611 Bytes

Contents

# frozen_string_literal: true

module NovaposhtaApi
  module Middlewares
    class ErrorHandling < Faraday::Response::Middleware
      SEPARATOR = ', '

      def on_complete(env)
        return if env.response_body['success']

        fail(NovaposhtaApi::ResponseError, error_message(env.body))
      end

      def error_message(body)
        (from_codes(body['errorCodes']) || body['errors'] || body['message']).join(SEPARATOR)
      end

      def from_codes(codes = [])
        return nil if codes.blank?

        codes.map { |code| I18n.t(code, scope: %i[novaposhta errors]) }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
novaposhta_api-0.1.5 lib/novaposhta_api/middlewares/error_handling.rb
novaposhta_api-0.1.4 lib/novaposhta_api/middlewares/error_handling.rb
novaposhta_api-0.1.3 lib/novaposhta_api/middlewares/error_handling.rb