writeOrFail method
Writes to the socket via write and fails the completer with a
client error when the write fails, instead of leaving the completer
pending with an unhandled asynchronous error.
onWriteError is called before the completer is failed, e.g. to forget
the command that the completer belongs to.
Implementation
void writeOrFail(
Future<void> Function() write,
Completer<dynamic> completer, {
void Function()? onWriteError,
}) {
unawaited(
write().catchError((Object e, StackTrace s) {
onWriteError?.call();
if (!completer.isCompleted) {
completer.completeError(
createClientError('unable to send command: $e'),
s,
);
}
}),
);
}