Verified Commit a94f449d authored by Hercules Merscher's avatar Hercules Merscher
Browse files

fix: Do not raise errors -- safely ignore operations

parent 6ef9b109
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -149,7 +149,7 @@ experience.checkpoint
experience.complete
```

**Note:** The `resume` method loads the start time from the Labkit context. If no covered experience data exists in the context, it behaves the same as calling methods on an unstarted experience (raises `UnstartedError` in development/test environments, or safely ignores in other environments).
**Note:** The `resume` method loads the start time from the Labkit context. If no covered experience data exists in the context, it behaves the same as calling methods on an unstarted experience and safely ignores the operation.

### Error Handling

@@ -182,4 +182,4 @@ end

- In `development` and `test` environments, accessing a non-existent covered experience will raise a `NotFoundError`
- In other environments, a null object is returned that safely ignores all method calls
- Attempting to checkpoint or complete an unstarted experience will raise an `UnstartedError` in `development` and `test` environments
- Attempting to checkpoint or complete an unstarted experience will safely ignore the operation
+0 −2
Original line number Diff line number Diff line
@@ -3,8 +3,6 @@
module Labkit
  module CoveredExperience
    CoveredExperienceError = Class.new(StandardError)
    UnstartedError = Class.new(CoveredExperienceError)
    CompletedError = Class.new(CoveredExperienceError)
    NotFoundError = Class.new(CoveredExperienceError)
    ReservedKeywordError = Class.new(CoveredExperienceError)
  end
+6 −10
Original line number Diff line number Diff line
@@ -90,7 +90,6 @@ module Labkit
      # Checkpoint the Covered Experience.
      #
      # @param extra [Hash] Additional data to include in the log event
      # @raise [UnstartedError] If the experience has not been started and RAILS_ENV is development or test.
      # @return [self]
      def checkpoint(**extra)
        return unless ensure_started!
@@ -119,7 +118,6 @@ module Labkit
      # Complete the Covered Experience.
      #
      # @param extra [Hash] Additional data to include in the log event
      # @raise [UnstartedError] If the experience has not been started and RAILS_ENV is development or test.
      # @return [self]
      def complete(**extra)
        return unless ensure_started!
@@ -166,10 +164,8 @@ module Labkit
      def ensure_started!
        return @start_time unless @start_time.nil?

        err = UnstartedError.new("Covered Experience #{@definition.covered_experience} not started")

        warn(err)
        raise(err) if %w[development test].include?(ENV['RAILS_ENV'])
        warn("Covered Experience #{@definition.covered_experience} not started")
        false
      end

      def completable(**extra, &)
@@ -188,10 +184,8 @@ module Labkit
      def ensure_incomplete!
        return true if @end_time.nil?

        err = CompletedError.new("Covered Experience #{@definition.covered_experience} already completed")

        warn(err)
        raise(err) if %w[development test].include?(ENV['RAILS_ENV'])
        warn("Covered Experience #{@definition.covered_experience} already completed")
        false
      end

      def urgency_threshold
@@ -199,6 +193,8 @@ module Labkit
      end

      def elapsed_time
        return 0 unless @start_time

        last_time = @end_time || @checkpoint_time || @start_time
        last_time - @start_time
      end
+13 −122
Original line number Diff line number Diff line
@@ -271,29 +271,9 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do
    end

    context 'when experience is not started' do
      context 'when RAILS_ENV is production' do
        before do
          stub_env('RAILS_ENV', 'production')
        end

        it 'does not raise error' do
      it 'does not raise error and safely ignores the operation' do
        expect { experience.resume }.not_to raise_error
        end
      end

      %w[test development].each do |env|
        context "when RAILS_ENV is #{env}" do
          before do
            stub_env('RAILS_ENV', env)
          end

          it 'raises UnstartedError' do
            expect { experience.resume }.to raise_error(
              Labkit::CoveredExperience::UnstartedError,
              "Covered Experience #{definition.covered_experience} not started"
            )
          end
        end
        expect { experience.resume }.not_to resume_covered_experience(:testing_sample)
      end
    end
  end
@@ -323,30 +303,9 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do
    end

    context 'when not started' do
      context 'when RAILS_ENV is production' do
        before do
          stub_env('RAILS_ENV', 'production')
        end

        it { expect { checkpoint }.not_to raise_error }
        it { expect { checkpoint }.not_to checkpoint_covered_experience(:testing_sample) }
      end

      context 'when RAILS_ENV is unset' do
      it { expect { checkpoint }.not_to raise_error }
      it { expect { checkpoint }.not_to checkpoint_covered_experience(:testing_sample) }
    end

      %w[test development].each do |env|
        context "when RAILS_ENV is #{env}" do
          before do
            stub_env('RAILS_ENV', env)
          end

          it { expect { checkpoint }.to raise_error(Labkit::CoveredExperience::UnstartedError, "Covered Experience #{definition.covered_experience} not started") }
        end
      end
    end
  end

  describe '#complete' do
@@ -395,62 +354,18 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do
    end

    context 'when not started' do
      context 'when RAILS_ENV is production' do
        before do
          stub_env('RAILS_ENV', 'production')
        end

      it { expect { complete }.not_to raise_error }
      it { expect { complete }.not_to complete_covered_experience(:testing_sample) }
    end

      context 'when RAILS_ENV is unset' do
        it { expect { complete }.not_to raise_error }
        it { expect { complete }.not_to complete_covered_experience(:testing_sample) }
      end

      %w[test development].each do |env|
        context "when RAILS_ENV is #{env}" do
          before do
            stub_env('RAILS_ENV', env)
          end

          it { expect { complete }.to raise_error(Labkit::CoveredExperience::UnstartedError, "Covered Experience #{definition.covered_experience} not started") }
        end
      end
    end

    context 'when already completed' do
      context 'when RAILS_ENV is production' do
        before do
          stub_env('RAILS_ENV', 'production')
          complete
        end

        it { expect { complete }.not_to raise_error }
        it { expect { complete }.not_to complete_covered_experience(:testing_sample) }
      end

      context 'when RAILS_ENV is unset' do
      before do
          complete
        experience.start.complete
      end

      it { expect { complete }.not_to raise_error }
      it { expect { complete }.not_to complete_covered_experience(:testing_sample) }
    end

      %w[test development].each do |env|
        context "when RAILS_ENV is #{env}" do
          before do
            stub_env('RAILS_ENV', env)
            experience.start.complete
          end

          it { expect { complete }.to raise_error(Labkit::CoveredExperience::CompletedError, "Covered Experience #{definition.covered_experience} already completed") }
        end
      end
    end
  end

  describe '#error!' do
@@ -466,35 +381,11 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do

  describe '#to_h' do
    context 'when experience has not been started' do
      context 'when RAILS_ENV is production' do
        before do
          stub_env('RAILS_ENV', 'production')
        end

        it 'returns an empty hash' do
          expect(experience.to_h).to eq({})
        end
      end

      context 'when RAILS_ENV is unset' do
      it 'returns an empty hash' do
        expect(experience.to_h).to eq({})
      end
    end

      %w[test development].each do |env|
        context "when RAILS_ENV is #{env}" do
          before do
            stub_env('RAILS_ENV', env)
          end

          it 'raises an error' do
            expect { experience.to_h }.to raise_error(Labkit::CoveredExperience::UnstartedError)
          end
        end
      end
    end

    context 'when experience has been started' do
      before do
        experience.start