Skip to content
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

Make error deserialization more resilient #127

Merged
merged 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion lib/temporal/workflow/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ def self.generate_error(failure, default_exception_class = StandardError)

begin
exception = exception_class.new(message)
rescue ArgumentError => deserialization_error
rescue => deserialization_error
# We don't currently support serializing/deserializing exceptions with more than one argument.
message = "#{exception_class}: #{message}"
exception = default_exception_class.new(message)
Temporal.logger.error(
"Could not instantiate original error. Defaulting to StandardError.",
{
original_error: failure.application_failure_info.type,
instantiation_error_class: deserialization_error.class.to_s,
instantiation_error_message: deserialization_error.message,
},
)
Expand Down
38 changes: 37 additions & 1 deletion spec/unit/lib/temporal/workflow/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ class ErrorWithTwoArgs < StandardError
def initialize(message, another_argument); end
end

class ErrorThatRaisesInInitialize < StandardError
def initialize(message)
# This class simulates an error class that has bugs in its initialize method, or where
# the arg isn't a string. It raises TypeError (String can't be coerced into Integer)
1 + message
Copy link
Contributor

Choose a reason for hiding this comment

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

it would be clearer to just explicitly raise the error here

end
end

class SomeError < StandardError; end

describe Temporal::Workflow::Errors do
Expand Down Expand Up @@ -51,7 +59,7 @@ class SomeError < StandardError; end
end


it "falls back to StandardError when the client can't initialize the error class" do
it "falls back to StandardError when the client can't initialize the error class due to arity" do
allow(Temporal.logger).to receive(:error)

message = "An error message"
Expand All @@ -73,10 +81,38 @@ class SomeError < StandardError; end
'Could not instantiate original error. Defaulting to StandardError.',
{
original_error: "ErrorWithTwoArgs",
instantiation_error_class: "ArgumentError",
instantiation_error_message: "wrong number of arguments (given 1, expected 2)",
},
)
end

it "falls back to StandardError when the client can't initialize the error class when initialize doesn't take a string" do
allow(Temporal.logger).to receive(:error)

message = "An error message"
stack_trace = ["a fake backtrace"]
failure = Fabricate(
:api_application_failure,
message: message,
backtrace: stack_trace,
error_class: ErrorThatRaisesInInitialize.to_s,
)

e = Temporal::Workflow::Errors.generate_error(failure)
expect(e).to be_a(StandardError)
expect(e.message).to eq("ErrorThatRaisesInInitialize: An error message")
expect(e.backtrace).to eq(stack_trace)
expect(Temporal.logger)
.to have_received(:error)
.with(
'Could not instantiate original error. Defaulting to StandardError.',
{
original_error: "ErrorThatRaisesInInitialize",
instantiation_error_class: "TypeError",
instantiation_error_message: "String can't be coerced into Integer",
},
)
end
end
end