|
| 1 | +require 'temporal/workflow/errors' |
| 2 | + |
| 3 | +class ErrorWithTwoArgs < StandardError |
| 4 | + def initialize(message, another_argument); end |
| 5 | +end |
| 6 | + |
| 7 | +class SomeError < StandardError; end |
| 8 | + |
| 9 | +describe Temporal::Workflow::Errors do |
| 10 | + describe '.generate_error' do |
| 11 | + it "instantiates properly when the client has the error" do |
| 12 | + message = "An error message" |
| 13 | + stack_trace = ["a fake backtrace"] |
| 14 | + failure = Fabricate( |
| 15 | + :api_application_failure, |
| 16 | + message: message, |
| 17 | + backtrace: stack_trace, |
| 18 | + error_class: SomeError.to_s |
| 19 | + ) |
| 20 | + |
| 21 | + e = Temporal::Workflow::Errors.generate_error(failure) |
| 22 | + expect(e).to be_a(SomeError) |
| 23 | + expect(e.message).to eq(message) |
| 24 | + expect(e.backtrace).to eq(stack_trace) |
| 25 | + |
| 26 | + end |
| 27 | + |
| 28 | + it "falls back to StandardError when the client doesn't have the error class" do |
| 29 | + allow(Temporal.logger).to receive(:error) |
| 30 | + |
| 31 | + message = "An error message" |
| 32 | + stack_trace = ["a fake backtrace"] |
| 33 | + failure = Fabricate( |
| 34 | + :api_application_failure, |
| 35 | + message: message, |
| 36 | + backtrace: stack_trace, |
| 37 | + error_class: 'NonexistentError', |
| 38 | + ) |
| 39 | + |
| 40 | + e = Temporal::Workflow::Errors.generate_error(failure) |
| 41 | + expect(e).to be_a(StandardError) |
| 42 | + expect(e.message).to eq("NonexistentError: An error message") |
| 43 | + expect(e.backtrace).to eq(stack_trace) |
| 44 | + expect(Temporal.logger) |
| 45 | + .to have_received(:error) |
| 46 | + .with( |
| 47 | + 'Could not find original error class. Defaulting to StandardError.', |
| 48 | + {original_error: "NonexistentError"}, |
| 49 | + ) |
| 50 | + |
| 51 | + end |
| 52 | + |
| 53 | + |
| 54 | + it "falls back to StandardError when the client can't initialize the error class" do |
| 55 | + allow(Temporal.logger).to receive(:error) |
| 56 | + |
| 57 | + message = "An error message" |
| 58 | + stack_trace = ["a fake backtrace"] |
| 59 | + failure = Fabricate( |
| 60 | + :api_application_failure, |
| 61 | + message: message, |
| 62 | + backtrace: stack_trace, |
| 63 | + error_class: ErrorWithTwoArgs.to_s, |
| 64 | + ) |
| 65 | + |
| 66 | + e = Temporal::Workflow::Errors.generate_error(failure) |
| 67 | + expect(e).to be_a(StandardError) |
| 68 | + expect(e.message).to eq("ErrorWithTwoArgs: An error message") |
| 69 | + expect(e.backtrace).to eq(stack_trace) |
| 70 | + expect(Temporal.logger) |
| 71 | + .to have_received(:error) |
| 72 | + .with( |
| 73 | + 'Could not instantiate original error. Defaulting to StandardError.', |
| 74 | + { |
| 75 | + original_error: "ErrorWithTwoArgs", |
| 76 | + instantiation_error_message: "wrong number of arguments (given 1, expected 2)", |
| 77 | + }, |
| 78 | + ) |
| 79 | + end |
| 80 | + |
| 81 | + end |
| 82 | +end |
0 commit comments