diff --git a/lib/uber_task/retry_task.rb b/lib/uber_task/retry_task.rb index d80efa2..29d927e 100644 --- a/lib/uber_task/retry_task.rb +++ b/lib/uber_task/retry_task.rb @@ -7,10 +7,19 @@ class RetryTask < Exception # rubocop:disable Lint/InheritException attr_accessor :reason, :wait - def initialize(reason: nil, wait: nil) + def initialize(reason: nil, wait: 0) + validate_wait_value!(wait) + super('Requested to retry the task.') @reason = reason @wait = wait end + + private + + def validate_wait_value!(value) + raise ArgumentError, '`wait` is not numberic' unless value.is_a?(Numeric) + raise ArgumentError, '`wait` cannot be negative' if value.negative? + end end end diff --git a/spec/uber_task/retry_task_spec.rb b/spec/uber_task/retry_task_spec.rb index 6feb91f..4e2a945 100644 --- a/spec/uber_task/retry_task_spec.rb +++ b/spec/uber_task/retry_task_spec.rb @@ -20,7 +20,25 @@ expect(exception.wait).to eq(42) end + it 'has 0 as a default wait attribute value' do + expect(described_class.new.wait).to eq(0) + end + it 'has a message' do expect(exception.message).to eq('Requested to retry the task.') end + + describe 'wait attribute validations' do + it 'does not allow non-numeric value' do + expect do + described_class.new(wait: 'string') + end.to raise_error(ArgumentError, '`wait` is not numberic') + end + + it 'does not allow negative value' do + expect do + described_class.new(wait: -1) + end.to raise_error(ArgumentError, '`wait` cannot be negative') + end + end end