Class: Temporalio::Converters::FailureConverter
- Inherits:
-
Object
- Object
- Temporalio::Converters::FailureConverter
- Defined in:
- lib/temporalio/converters/failure_converter.rb
Overview
Base class for converting Ruby errors to/from Temporal failures.
Instance Attribute Summary collapse
-
#encode_common_attributes ⇒ Boolean
readonly
If
true
, the message and stack trace of the failure will be moved into the encoded attribute section of the failure which can be encoded with a codec.
Class Method Summary collapse
-
.default ⇒ FailureConverter
Default failure converter.
Instance Method Summary collapse
-
#from_failure(failure, converter) ⇒ Error::Failure
Convert a Temporal failure to a Ruby error.
-
#initialize(encode_common_attributes: false) ⇒ FailureConverter
constructor
Create failure converter.
-
#to_failure(error, converter) ⇒ Api::Failure::V1::Failure
Convert a Ruby error to a Temporal failure.
Constructor Details
#initialize(encode_common_attributes: false) ⇒ FailureConverter
Create failure converter.
24 25 26 |
# File 'lib/temporalio/converters/failure_converter.rb', line 24 def initialize(encode_common_attributes: false) @encode_common_attributes = encode_common_attributes end |
Instance Attribute Details
#encode_common_attributes ⇒ Boolean (readonly)
Returns If true
, the message and stack trace of the failure will be moved into the encoded attribute section of the failure which can be encoded with a codec.
18 19 20 |
# File 'lib/temporalio/converters/failure_converter.rb', line 18 def encode_common_attributes @encode_common_attributes end |
Class Method Details
.default ⇒ FailureConverter
Returns Default failure converter.
12 13 14 |
# File 'lib/temporalio/converters/failure_converter.rb', line 12 def self.default @default ||= Ractor.make_shareable(FailureConverter.new) end |
Instance Method Details
#from_failure(failure, converter) ⇒ Error::Failure
Convert a Temporal failure to a Ruby error.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/temporalio/converters/failure_converter.rb', line 109 def from_failure(failure, converter) # If encoded attributes have any of the fields we expect, try to decode # but ignore any error unless failure.encoded_attributes.nil? begin attrs = converter.from_payload(failure.encoded_attributes) if attrs.is_a?(Hash) # Shallow dup failure here to avoid affecting caller failure = failure.dup failure. = attrs['message'] if attrs.key?('message') failure.stack_trace = attrs['stack_trace'] if attrs.key?('stack_trace') end rescue StandardError # Ignore failures end end # Convert error = if failure.application_failure_info Error::ApplicationError.new( Internal::ProtoUtils.string_or(failure., 'Application error'), *converter.from_payloads(failure.application_failure_info.details), type: Internal::ProtoUtils.string_or(failure.application_failure_info.type), non_retryable: failure.application_failure_info.non_retryable, next_retry_delay: failure.application_failure_info.next_retry_delay&.to_f ) elsif failure.timeout_failure_info Error::TimeoutError.new( Internal::ProtoUtils.string_or(failure., 'Timeout'), type: Internal::ProtoUtils.enum_to_int(Api::Enums::V1::TimeoutType, failure.timeout_failure_info.timeout_type), last_heartbeat_details: converter.from_payloads( failure.timeout_failure_info.last_heartbeat_details ) ) elsif failure.canceled_failure_info Error::CanceledError.new( Internal::ProtoUtils.string_or(failure., 'Canceled'), details: converter.from_payloads(failure.canceled_failure_info.details) ) elsif failure.terminated_failure_info Error::TerminatedError.new( Internal::ProtoUtils.string_or(failure., 'Terminated'), details: [] ) elsif failure.server_failure_info Error::ServerError.new( Internal::ProtoUtils.string_or(failure., 'Server error'), non_retryable: failure.server_failure_info.non_retryable ) elsif failure.activity_failure_info Error::ActivityError.new( Internal::ProtoUtils.string_or(failure., 'Activity error'), scheduled_event_id: failure.activity_failure_info.scheduled_event_id, started_event_id: failure.activity_failure_info.started_event_id, identity: failure.activity_failure_info.identity, activity_type: failure.activity_failure_info.activity_type.name, activity_id: failure.activity_failure_info.activity_id, retry_state: Internal::ProtoUtils.enum_to_int( Api::Enums::V1::RetryState, failure.activity_failure_info.retry_state, zero_means_nil: true ) ) elsif failure.child_workflow_execution_failure_info Error::ChildWorkflowError.new( Internal::ProtoUtils.string_or(failure., 'Child workflow error'), namespace: failure.child_workflow_execution_failure_info.namespace, workflow_id: failure.child_workflow_execution_failure_info.workflow_execution.workflow_id, run_id: failure.child_workflow_execution_failure_info.workflow_execution.run_id, workflow_type: failure.child_workflow_execution_failure_info.workflow_type.name, initiated_event_id: failure.child_workflow_execution_failure_info.initiated_event_id, started_event_id: failure.child_workflow_execution_failure_info.started_event_id, retry_state: Internal::ProtoUtils.enum_to_int( Api::Enums::V1::RetryState, failure.child_workflow_execution_failure_info.retry_state, zero_means_nil: true ) ) else Error::Failure.new(Internal::ProtoUtils.string_or(failure., 'Failure error')) end Error._with_backtrace_and_cause( error, backtrace: failure.stack_trace.split("\n"), cause: failure.cause ? from_failure(failure.cause, converter) : nil ) end |
#to_failure(error, converter) ⇒ Api::Failure::V1::Failure
Convert a Ruby error to a Temporal failure.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/temporalio/converters/failure_converter.rb', line 33 def to_failure(error, converter) failure = Api::Failure::V1::Failure.new( message: error., stack_trace: error.backtrace&.join("\n") ) cause = error.cause failure.cause = to_failure(cause, converter) if cause # Convert specific error type details case error when Error::ApplicationError failure.application_failure_info = Api::Failure::V1::ApplicationFailureInfo.new( type: error.type, non_retryable: error.non_retryable, details: converter.to_payloads(error.details), next_retry_delay: Internal::ProtoUtils.seconds_to_duration(error.next_retry_delay) ) when Error::TimeoutError failure.timeout_failure_info = Api::Failure::V1::TimeoutFailureInfo.new( timeout_type: error.type, last_heartbeat_details: converter.to_payloads(error.last_heartbeat_details) ) when Error::CanceledError failure.canceled_failure_info = Api::Failure::V1::CanceledFailureInfo.new( details: converter.to_payloads(error.details) ) when Error::TerminatedError failure.terminated_failure_info = Api::Failure::V1::TerminatedFailureInfo.new when Error::ServerError failure.server_failure_info = Api::Failure::V1::ServerFailureInfo.new( non_retryable: error.non_retryable ) when Error::ActivityError failure.activity_failure_info = Api::Failure::V1::ActivityFailureInfo.new( scheduled_event_id: error.scheduled_event_id, started_event_id: error.started_event_id, identity: error.identity, activity_type: Api::Common::V1::ActivityType.new(name: error.activity_type), activity_id: error.activity_id, retry_state: error.retry_state ) when Error::ChildWorkflowError failure.child_workflow_execution_failure_info = Api::Failure::V1::ChildWorkflowExecutionFailureInfo.new( namespace: error.namespace, workflow_execution: Api::Common::V1::WorkflowExecution.new( workflow_id: error.workflow_id, run_id: error.run_id ), workflow_type: Api::Common::V1::WorkflowType.new(name: error.workflow_type), initiated_event_id: error.initiated_event_id, started_event_id: error.started_event_id, retry_state: error.retry_state ) else failure.application_failure_info = Api::Failure::V1::ApplicationFailureInfo.new( type: error.class.name.split('::').last ) end # If encoding common attributes, move message and stack trace if @encode_common_attributes failure.encoded_attributes = converter.to_payload( { message: failure., stack_trace: failure.stack_trace } ) failure. = 'Encoded failure' failure.stack_trace = '' end failure end |