Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Do not fail if an Exception has no backtrace #2903

Merged
merged 2 commits into from
Jul 16, 2021
Merged

Conversation

zinovyev
Copy link
Contributor

Currently backtrace of an exception is checked by the empty? method. But it sometimes occur that the Exception class will return nil for backtrace:

StandardError.new.backtrace.empty?
NoMethodError: undefined method `empty?' for nil:NilClass

@JonRowe
Copy link
Member

JonRowe commented Jul 13, 2021

👋 we would need a test indicating how this can occur

@zinovyev
Copy link
Contributor Author

wave we would need a test indicating how this can occur

@JonRowe sure! I've added a test for this case.

@@ -202,6 +202,26 @@ def initialize(message, backtrace = [], cause = nil)
EOS
end

it 'wont fail for the exception with a nil backtrace', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
allow(first_exception).to receive(:backtrace) { nil }
Copy link
Member

Choose a reason for hiding this comment

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

Can you find a real scenario where this would occur? We don't tend to defensively program around "maybes", we would want an actual scenario where this can occur.

Copy link
Member

Choose a reason for hiding this comment

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

begin
  raise StandardError, 'oops', nil
rescue => e
  puts "backtrace: #{e.backtrace}"
end

fails for me with:

backtrace: ["1.rb:2:in `<main>'"]

Maybe a custom exception would do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It won't work with the StandardError because the StandardError doesn't have the cause:

StandardError.new('foo').cause
# => nil

But it does fails for me in the real test when a custom error having the #cause is raised, but the last error in the chain doesn't have a backtrace set.

Copy link
Member

Choose a reason for hiding this comment

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

The best is to use such a custom exception in a scenario, then.

Copy link
Contributor Author

@zinovyev zinovyev Jul 14, 2021

Choose a reason for hiding this comment

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

Sure! I've updated the test: https://github.com/rspec/rspec-core/pull/2903/files#diff-829be18287a3c0ec09a70d0505b7cc835c298063c15edce2e5a4bf20677e42f2R206

I realized that we don't have a scenario for the [] case too. So I've added both scenarios.
Does it make more sense now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @JonRowe @pirj! Please let me know if the proposed changes are fine or should I improve something else here 😄
Also some of the environment specs on CI are failling. That's what I see in the output:

script/functions.sh: line 42: bin/rspec: No such file or directory
Error: Process completed with exit code 127.

Do you think it is fine or is it something that I might have touched with that change?

Copy link
Member

Choose a reason for hiding this comment

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

I suggest you to rebase the PR, the error should disappear.

Will take a look a bit later. Thanks for pushing this forward

@zinovyev zinovyev force-pushed the patch-1 branch 3 times, most recently from cf3f595 to a6270f6 Compare July 14, 2021 12:23
Currently `backtrace` of an exception is checked by the `empty?` method. But it sometimes occur that the Exception class will return `nil` for backtrace:
```
StandardError.new.backtrace.empty?
NoMethodError: undefined method `empty?' for nil:NilClass
```
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Perfect, thank you.
Would you like to add a Changelog entry?

@zinovyev

This comment has been minimized.

@pirj

This comment has been minimized.

@pirj pirj requested a review from JonRowe July 16, 2021 09:37
Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

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

@pirj I'm happy for you to handle this

@zinovyev
Copy link
Contributor Author

Thank you guys for your reviews and for being so helpful!
Do you think that we can merge it now? It looks like I don't have the permission to do it by myself. Or will it just be merged automatically in some time?

@pirj
Copy link
Member

pirj commented Jul 16, 2021

I'll handle this later in the day.

@pirj pirj merged commit 9de11e5 into rspec:main Jul 16, 2021
@pirj
Copy link
Member

pirj commented Jul 16, 2021

Thanks for your contribution!

yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 6, 2021
Do not fail if an Exception has no backtrace

---
This commit was imported from rspec/rspec-core@9de11e5.
JonRowe pushed a commit that referenced this pull request Oct 17, 2021
Do not fail if an Exception has no backtrace
JonRowe pushed a commit that referenced this pull request Oct 17, 2021
Do not fail if an Exception has no backtrace
yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 19, 2021
Do not fail if an Exception has no backtrace

---
This commit was imported from rspec/rspec-core@90a15b2.
yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 19, 2021
Do not fail if an Exception has no backtrace

---
This commit was imported from rspec/rspec-core@6fc2b92.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants