replyErrBytes method
Sends an error reply to this query with a ZBytes payload.
The payload is consumed by this call (ownership transferred to zenoh).
Optionally specify an encoding for the payload. Error replies carry a
payload + encoding ONLY -- there is no attachment parameter (zenoh-c's
z_query_reply_err_options_t has no attachment field).
Throws StateError if the query has been disposed. Throws ZenohException if the reply fails.
Implementation
void replyErrBytes(ZBytes payload, {Encoding? encoding}) {
// PRE-move early-return guard: a disposed query throws StateError here,
// BEFORE any z_bytes_move, so the caller retains ownership of payload and
// we must NOT mark it consumed on this path (mirrors replyBytes).
_ensureNotDisposed();
Pointer<Utf8> encodingNative = nullptr;
if (encoding != null) {
encodingNative = encoding.mimeType.toNativeUtf8();
}
try {
final rc = bindings.zd_query_reply_err(
Pointer.fromAddress(_handle).cast(),
payload.nativePtr.cast(),
encoding != null ? encodingNative.cast() : nullptr,
);
// Mark the payload ZBytes consumed UNCONDITIONALLY: once we reach this
// FFI call the pre-move guard above has passed, so zd_query_reply_err has
// moved the payload into zenoh-c regardless of the return code (its
// encoding-error path drops the already-moved bytes). Marking before the
// rc-throw prevents a later use-after-move. The encoding is a MIME string
// (not a caller-owned ZBytes), so it needs no Dart-side markConsumed.
payload.markConsumed();
if (rc != 0) {
throw ZenohException('Failed to send error reply to query', rc);
}
} finally {
if (encoding != null) {
calloc.free(encodingNative);
}
}
}