Sha256: 9aae503da19bcf6a27afeb6d5a05498b7704a7f3ed7ba64c3ecdb233d17d5e89

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module Diffend
  module HandleErrors
    # Module responsible for reporting errors to diffend
    module Report
      class << self
        # Execute request to Diffend
        #
        # @param exception [Exception] expection that was raised
        # @param payload [Hash] with versions to check
        # @param config [OpenStruct] Diffend config
        # @param message [Symbol] message that we want to display
        # @param report [Boolean] if true we will report the issue to diffend
        #
        # @return [Net::HTTPResponse] response from Diffend
        def call(exception:, payload: {}, config:, message:, report: false)
          exception_payload = prepare_exception_payload(exception, payload)

          Bundler.ui.error(Diffend::HandleErrors::Messages::PAYLOAD_DUMP)
          Bundler.ui.error(Diffend::HandleErrors::Messages.const_get(message.to_s.upcase))

          if report
            Diffend::Request.call(
              config,
              errors_url(config.project_id),
              exception_payload
            )
          end

          exit 1
        end

        # Prepare exception payload and display it to stdout
        #
        # @param exception [Exception] expection that was raised
        # @param payload [Hash] with versions to check
        #
        # @return [Hash]
        def prepare_exception_payload(exception, payload)
          Diffend::HandleErrors::BuildExceptionPayload
            .call(exception, payload)
            .tap(&Diffend::HandleErrors::DisplayToStdout.method(:call))
        end

        # Provides diffend errors endpoint url
        #
        # @param project_id [String] diffend project_id
        #
        # @return [String] diffend endpoint
        def errors_url(project_id)
          return ENV['DIFFEND_ERROR_URL'] if ENV.key?('DIFFEND_ERROR_URL')

          "https://my.diffend.io/api/projects/#{project_id}/errors"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
diffend-0.2.23 lib/diffend/handle_errors/report.rb