Skip to content

Commit

Permalink
Merge pull request #79 from jdelStrother/exception-cause
Browse files Browse the repository at this point in the history
Fix DRbUnknown error with unserializable Exception causes
  • Loading branch information
briandunn authored Feb 29, 2024
2 parents 546b474 + 9d01912 commit ed0a352
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/flatware/serialized_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ class SerializedException
attr_reader :class, :message, :cause
attr_accessor :backtrace

def initialize(klass, message, backtrace, cause = '')
def initialize(klass, message, backtrace, cause = nil)
@class = serialized(klass)
@message = message
@backtrace = backtrace
@cause = cause
@cause = cause && SerializedException.from(cause)
end

def self.from(exception)
Expand Down
29 changes: 24 additions & 5 deletions spec/flatware/rspec/marshalable/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def stub_execution_result(exception)
)
end

it 'caries what is needed to format a backtrace' do
it 'carries what is needed to format a backtrace' do
exception = Exception.new
RSpec::Core::Formatters::ExceptionPresenter.new(
exception,
Expand All @@ -29,20 +29,39 @@ def stub_execution_result(exception)
).fully_formatted(nil)
end

it 'does not cary constant references in exceptions' do
it 'does not carry constant references in exceptions' do
const = stub_const('A::Constant::Not::Likely::Loaded::In::Sink', Class.new(RuntimeError))
wrapper_const = stub_const('Another::Constant::Not::Likely::Loaded::In::Sink', Class.new(RuntimeError))
caught_exception = begin
begin
raise const, 'something bad happened'
rescue RuntimeError => e
# raise a second exception so that Exception#cause is set up
raise wrapper_const, e
end
rescue RuntimeError => e
e
end

message = described_class.new(
instance_double(
RSpec::Core::Example,
execution_result: stub_execution_result(const.new),
execution_result: stub_execution_result(caught_exception),
full_description: nil,
location: nil,
location_rerun_argument: nil,
metadata: {}
)
)

expect(message.execution_result.exception.class.to_s).to eq(const.to_s)
expect(message.execution_result.exception.class).to_not be_an_instance_of(const)
result_exception = message.execution_result.exception

expect(result_exception).to be_an_instance_of(Flatware::SerializedException)
expect(result_exception.class.to_s).to eq(wrapper_const.to_s)
expect(result_exception.class).to_not be_an_instance_of(wrapper_const)

expect(result_exception.cause).to be_an_instance_of(Flatware::SerializedException)
expect(result_exception.cause.class.to_s).to eq(const.to_s)
expect(result_exception.cause.class).to_not be_an_instance_of(const)
end
end

0 comments on commit ed0a352

Please sign in to comment.