markFailedForExecution method

  1. @override
Future<TerminalFailureResult> markFailedForExecution(
  1. String runId, {
  2. required String executionId,
  3. required Object error,
  4. required StackTrace stack,
  5. bool terminal = true,
})
override

Records an error only if the captured execution is still current.

With terminal false, only error metadata is changed; status and lease remain unchanged so the worker's retry policy can decide the outcome. With terminal true, the run transitions to failed and loses its lease.

Implementation

@override
Future<TerminalFailureResult> markFailedForExecution(
  String runId, {
  required String executionId,
  required Object error,
  required StackTrace stack,
  bool terminal = true,
}) async {
  final state = _runs[runId];
  if (state == null || state.executionId != executionId) {
    return TerminalFailureResult.superseded;
  }
  if (state.status == WorkflowStatus.failed) {
    return TerminalFailureResult.alreadyFailedForExecution;
  }
  if (state.status == WorkflowStatus.completed ||
      state.status == WorkflowStatus.cancelled) {
    return TerminalFailureResult.superseded;
  }
  if (!terminal) {
    _runs[runId] = state.copyWith(
      lastError: {'error': error.toString(), 'stack': stack.toString()},
      updatedAt: _clock.now(),
    );
    return TerminalFailureResult.applied;
  }
  _removeWatcherForRun(runId);
  for (final due in _due.values) {
    due.remove(runId);
  }
  _runs[runId] = state.copyWith(
    status: WorkflowStatus.failed,
    lastError: {'error': error.toString(), 'stack': stack.toString()},
    ownerId: null,
    leaseExpiresAt: null,
    resumeAt: null,
    waitTopic: null,
    updatedAt: _clock.now(),
  );
  return TerminalFailureResult.applied;
}