Skip to content

Commit

Permalink
Reduce nesting of wait for action attempt errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi committed Oct 10, 2024
1 parent 644f08b commit 781f22b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/seam/helpers/action_attempt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def self.wait_until_finished(action_attempt, client, timeout: nil, polling_inter
sleep(polling_interval)
time_waiting += polling_interval

raise Seam::Http::WaitForActionAttempt::TimeoutError.new(action_attempt, timeout) if time_waiting > timeout
raise Seam::ActionAttemptTimeoutError.new(action_attempt, timeout) if time_waiting > timeout

action_attempt = update_action_attempt(action_attempt, client)
end

raise Seam::Http::WaitForActionAttempt::FailedError.new(action_attempt) if action_attempt.status == "error"
raise Seam::ActionAttemptFailedError.new(action_attempt) if action_attempt.status == "error"

action_attempt
end
Expand Down
46 changes: 21 additions & 25 deletions lib/seam/wait_for_action_attempt.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
# frozen_string_literal: true

module Seam
module Http
module WaitForActionAttempt
class ActionAttemptError < StandardError
attr_reader :action_attempt
class ActionAttemptError < StandardError
attr_reader :action_attempt

def initialize(message, action_attempt)
super(message)
@action_attempt = action_attempt
end
def initialize(message, action_attempt)
super(message)
@action_attempt = action_attempt
end

def name
self.class.name
end
end
def name
self.class.name
end
end

class FailedError < ActionAttemptError
attr_reader :code
class ActionAttemptFailedError < ActionAttemptError
attr_reader :code

def initialize(action_attempt)
super(action_attempt.error.message, action_attempt)
@code = action_attempt.error.type
end
end
def initialize(action_attempt)
super(action_attempt.error.message, action_attempt)
@code = action_attempt.error.type
end
end

class TimeoutError < ActionAttemptError
def initialize(action_attempt, timeout)
message = "Timed out waiting for action attempt after #{timeout}s"
super(message, action_attempt)
end
end
class ActionAttemptTimeoutError < ActionAttemptError
def initialize(action_attempt, timeout)
message = "Timed out waiting for action attempt after #{timeout}s"
super(message, action_attempt)
end
end
end
6 changes: 3 additions & 3 deletions spec/seam_client/wait_for_action_attepmt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
action_attempt_id: action_attempt.action_attempt_id,
wait_for_action_attempt: {timeout: 0.1}
)
end.to raise_error(Seam::Http::WaitForActionAttempt::TimeoutError) do |error|
end.to raise_error(Seam::ActionAttemptTimeoutError) do |error|
expect(error.action_attempt.action_attempt_id).to eq(action_attempt.action_attempt_id)
expect(error.action_attempt.status).to eq(action_attempt.status)
end
Expand All @@ -142,7 +142,7 @@
action_attempt_id: action_attempt.action_attempt_id,
wait_for_action_attempt: true
)
end.to raise_error(Seam::Http::WaitForActionAttempt::FailedError) do |error|
end.to raise_error(Seam::ActionAttemptFailedError) do |error|
expect(error.message).to include("Failed")
expect(error.action_attempt.action_attempt_id).to eq(action_attempt.action_attempt_id)
expect(error.action_attempt.status).to eq("error")
Expand All @@ -167,7 +167,7 @@
action_attempt_id: action_attempt.action_attempt_id,
wait_for_action_attempt: {timeout: 0.5, polling_interval: 3}
)
end.to raise_error(Seam::Http::WaitForActionAttempt::TimeoutError) do |error|
end.to raise_error(Seam::ActionAttemptTimeoutError) do |error|
expect(error.action_attempt.action_attempt_id).to eq(action_attempt.action_attempt_id)
expect(error.action_attempt.status).to eq(action_attempt.status)
end
Expand Down

0 comments on commit 781f22b

Please sign in to comment.