toSafeStream method
Transforms a Stream into a Stream.
Each data event from the original stream is wrapped in an Ok. Each error event is wrapped in an Err.
If cancelOnError is true, the stream will be closed upon the first
error.
Implementation
TResultStream<T> toSafeStream({required bool cancelOnError}) {
return transform(
StreamTransformer.fromHandlers(
handleData: (data, sink) {
sink.add(Ok(data));
},
handleError: (error, stackTrace, sink) {
if (error is Err) {
sink.add(error.transfErr());
} else {
// Forward the trace handed to us by the upstream stream — a
// life-critical audit trail can't reconstruct where the error
// originated from a trace captured at the Err() call site.
sink.add(Err<T>(error, stackTrace: stackTrace));
}
if (cancelOnError) {
sink.close();
}
},
handleDone: (sink) {
sink.close();
},
),
);
}