pushStreamErrorEvent function
void
pushStreamErrorEvent(
- AssistantMessageEventStream eventStream,
- ProviderStreamState state,
- Object error,
- CancelToken? cancelToken,
Converts a caught error into the terminal ErrorEvent
(errors-as-events invariant): aborts get StopReason.aborted, everything
else StopReason.error.
Implementation
void pushStreamErrorEvent(
AssistantMessageEventStream eventStream,
ProviderStreamState state,
Object error,
CancelToken? cancelToken,
) {
final aborted =
error is AbortedError ||
error is CancelledException ||
(cancelToken?.isCancelled ?? false);
final reason = aborted ? StopReason.aborted : StopReason.error;
state.stopReason = reason;
state.errorMessage = aborted
? 'Request was aborted'
: formatProviderError(error);
// Issue #867: the structured 429 rides on the finalized message so every
// surface renders from the structure; the raw payload stays inside.
state.rateLimit = aborted || error is! ProviderHttpError
? null
: error.rateLimit;
final retryAfter = !aborted && error is ProviderHttpError
? error.retryAfter
: null;
eventStream.push(
ErrorEvent(
reason: reason,
error: state.snapshot(finalize: true),
retryAfter: retryAfter,
),
);
}