Sha256: cadb592f3285a7343a7a1b422c143270b304da3561f489cdb7f0c0b177292a86

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Karafka
  module Web
    module Ui
      module Lib
        # Class used to execute code that can fail but we do not want to fail the whole operation.
        # The primary use-case is for displaying deserialized data. We always need to assume, that
        # part of the data can be corrupted and it should not crash the whole UI.
        #
        # It caches the result and does not run the code twice (only once)
        class SafeRunner
          attr_reader :error, :result

          # @param block [Proc] code we want to safe-guard
          def initialize(&block)
            @code = block
            @executed = false
            @success = false
            @error = nil
            @result = nil
          end

          # @return [Boolean] was the code execution successful or not
          def success?
            return @success if executed?

            call

            @success
          end

          # @return [Boolean] was the code execution failed or not
          def failure?
            !success?
          end

          # Runs the execution and returns block result
          def call
            return @result if executed?

            @executed = true
            @result = @code.call
            @success = true
            @result
          rescue StandardError => e
            @error = e
            @success = false
          end

          # @return [Boolean] was the code executed already or not yet
          def executed?
            @executed
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
karafka-web-0.9.1 lib/karafka/web/ui/lib/safe_runner.rb
karafka-web-0.9.0 lib/karafka/web/ui/lib/safe_runner.rb
karafka-web-0.9.0.rc3 lib/karafka/web/ui/lib/safe_runner.rb
karafka-web-0.9.0.rc2 lib/karafka/web/ui/lib/safe_runner.rb
karafka-web-0.9.0.rc1 lib/karafka/web/ui/lib/safe_runner.rb