8000 feat: `Promise#rescue` can now take a list of error class filters. by arthurschreiber · Pull Request #32 · lgierth/promise.rb · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Promise#rescue can now take a list of error class filters. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/reek.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ NestedIterators:
enabled: true
exclude: []
max_allowed_nesting: 1
ignore_iterators: []
ignore_iterators: [lambda]
NilCheck:
enabled: true
exclude: []
Expand Down
13 changes: 12 additions & 1 deletion lib/promise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,18 @@ def then(on_fulfill = nil, on_reject = nil, &block)
next_promise
end

def rescue(&block)
def rescue(*args)
raise ArgumentError, 'no block given' unless block_given?

if args.empty?
block = Proc.new
else
block = lambda do |error|
raise error unless args.any? { |error_class| error.is_a?(error_class) }
yield error
end
end

self.then(nil, block)
end
alias_method :catch, :rescue
Expand Down
105 changes: 105 additions & 0 deletions spec/unit/promise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,85 @@
expect(result).to eq(reason)
expect(subject.reason).to eq(reason)
end

it 'allows specifying a list of error types' do
result = nil
p = subject.rescue(StandardError) do |reason|
result = reason
end

subject.reject(reason)
expect(result).to eq(reason)
expect(subject.reason).to eq(reason)
expect(p).to be_fulfilled

result = nil
p = subject.rescue(RuntimeError) do |reason|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like second test of three that got grouped together in a single test.

result = reason
end

subject.reject(reason)
expect(result).to be_nil
expect(subject.reason).to eq(reason)
expect(p).to be_rejected

result = nil
p = subject.rescue(RuntimeError, StandardError) do |reason|
result = reason
end

subject.reject(reason)
expect(result).to eq(reason)
expect(subject.reason).to eq(reason)
expect(p).to be_fulfilled
end

it 'can not be called without a block' do
p1 = Promise.new

expect {
p1.rescue
}.to raise_error(ArgumentError, 'no block given')
end

it 'can be called with with a block not taking an error argument' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the ability to specify a block or proc without all the arguments just a property of ruby? E.g.

irb> proc { :foo }.call(1)
=> :foo

It looks like this is an unnecessary test.

p1 = Promise.new
p2 = p1.rescue { :foo }

p1.reject(RuntimeError.new('fail'))

expect(p2).to be_fulfilled
expect(p2.value).to equal(:foo)
end

it 'correctly handles error type subclasses' do
rescued = []

promise = Promise.new
promise.rescue(StandardError) do
rescued << StandardError
end
promise.rescue(RuntimeError) do
rescued << RuntimeError
end

promise.reject(RuntimeError.new('fail'))

expect(rescued).to eq([StandardError, RuntimeError])
end

it 'does not handle unspecified error types' do
p1 = Promise.new
p2 = p1.rescue(RuntimeError) { raise 'fail' }
p3 = p2.rescue do |error|
expect(error).to be_an_instance_of(StandardError)
expect(error.message).to eq('testing')
end

p1.reject(StandardError.new('testing'))
expect(p2).to be_rejected
expect(p3).to be_fulfilled
end
end

describe '#catch' do
Expand All @@ -747,6 +826,32 @@
expect(result).to eq(reason)
expect(subject.reason).to eq(reason)
end

it 'allows specifying a list of error types' do
result = nil
p = subject.catch(StandardError) { |reas| result = reas }

subject.reject(reason)
expect(result).to eq(reason)
expect(subject.reason).to eq(reason)
expect(p).to be_fulfilled

result = nil
p = subject.catch(RuntimeError) { |reas| result = reas }

subject.reject(reason)
expect(result).to be_nil
expect(subject.reason).to eq(reason)
expect(p).to be_rejected

result = nil
p = subject.catch(RuntimeError, StandardError) { |reas| result = reas }

subject.reject(reason)
expect(result).to eq(reason)
expect(subject.reason).to eq(reason)
expect(p).to be_fulfilled
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these tests for catch are necessary, since it is just an alias for rescue

end

describe '#progress' do
Expand Down
0