summaryrefslogtreecommitdiffhomepage
path: root/lib/dispatch/adapter/tester/errors.rb
blob: e3a9a6b5d65b4f9c87605622459134b56b100da6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

module Dispatch
  module Adapter
    module Tester
      class Error < StandardError; end

      # Raised when the playbook JSON schema is invalid
      class InvalidPlaybookError < Error; end

      # Raised when chat is called but no more steps remain
      class PlaybookExhaustedError < Error
        attr_reader :total_steps, :calls_made

        def initialize(total_steps:, calls_made:)
          @total_steps = total_steps
          @calls_made = calls_made
          super(
            "Playbook exhausted: all #{total_steps} steps have been consumed, " \
            "but chat was called a #{ordinalize(calls_made)} time"
          )
        end

        private

        def ordinalize(n)
          suffix = case n % 100
                   when 11, 12, 13 then "th"
                   else
                     case n % 10
                     when 1 then "st"
                     when 2 then "nd"
                     when 3 then "rd"
                     else "th"
                     end
                   end
          "#{n}#{suffix}"
        end
      end

      # Raised when not all steps were consumed after a test run
      class UnconsumedStepsError < Error
        attr_reader :total_steps, :consumed, :remaining_step_ids

        def initialize(total_steps:, consumed:, remaining_step_ids:)
          @total_steps = total_steps
          @consumed = consumed
          @remaining_step_ids = remaining_step_ids
          super(
            "Playbook has unconsumed steps: #{consumed}/#{total_steps} steps consumed. " \
            "Remaining steps: #{remaining_step_ids.inspect}"
          )
        end
      end
    end
  end
end