Sha256: 5b36597814e1b8f163738fd94ea80cdc324f65a486527ac964036e3734300ba6

Contents?: true

Size: 595 Bytes

Versions: 1

Compression:

Stored size: 595 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']).join(SEPARATOR)
      end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
novaposhta_api-0.1.0 lib/novaposhta_api/middlewares/error_handling.rb