Sha256: 903df4144798fe76bcb78245e9896059d3f0eb6e615b0b346ca5ad1e960072aa

Contents?: true

Size: 1.26 KB

Versions: 20

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true
require 'shopify_cli'

module Extension
  module Tasks
    module UserErrors
      USER_ERRORS_FIELD = 'userErrors'
      MESSAGE_FIELD = 'message'
      USER_ERRORS_PARSE_ERROR = 'Unable to parse errors from server.'

      def abort_if_user_errors(context, response)
        return if response.nil?

        user_errors = response.dig(USER_ERRORS_FIELD)
        output_all_user_errors(context, user_errors)
      end

      private

      def output_all_user_errors(context, user_errors)
        return if user_errors.nil? || user_errors.empty?
        last_user_error = user_errors.pop

        user_errors.each { |user_error| puts_user_error(context, user_error) }
        abort_user_error(context, last_user_error)
      end

      def puts_user_error(context, user_error)
        output_user_error(context, user_error) { |message| context.puts("{{x}} #{message}") }
      end

      def abort_user_error(context, user_error)
        output_user_error(context, user_error) { |message| context.abort(message) }
      end

      def output_user_error(context, user_error)
        if user_error.key?(MESSAGE_FIELD)
          yield(user_error[MESSAGE_FIELD])
        else
          context.abort(USER_ERRORS_PARSE_ERROR)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
shopify-cli-1.6.0 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.5.0 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.4.1 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.4.0 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.3.1 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.3.0 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.2.0 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.1.2 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.1.1 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.1.0 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.0.5 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.0.4 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.0.3 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.0.2 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.0.1 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-1.0.0 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-0.9.3 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-0.9.2 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-0.9.1 lib/project_types/extension/tasks/user_errors.rb
shopify-cli-0.9.0 lib/project_types/extension/tasks/user_errors.rb