Skip to content
Merged
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
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
39 changes: 38 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,15 @@ 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 the sort of TypeError that would happen if you wrote
# 1 + message
raise TypeError.new("String can't be coerced into Integer")
end
end

class SomeError < StandardError; end

describe Temporal::Workflow::Errors do
Expand Down Expand Up @@ -51,7 +60,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 +82,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