Перейти к основному содержимому
Эта страница ещё не переведена

Ниже показан английский оригинал.

error

error tells the handler when a publish the runtime makes on its behalf fails.

Three publishes are the runtime's: the delayed copy of a retry_after, the copy that goes to a declared dead letter, and the reply of an out_reply. A publish in the handler body returns a Result; for these three, .error gives the handler the same control.

The continuation receives Failures, one Failed per message that did not go out. error() is the cause. When the publisher fails, it is the publisher's own error: downcast it to the broker's error type to tell a transient failure, such as a refused connection, from a permanent one, such as a message the broker rejected. A reply that fails to encode never reaches the publisher, and its cause is the encode error, such as the codec's CodecError. On the embedded runtime, a delayed retry copy that finds the mount's store of copies full fails with NoFreeRecordError. message() is the message the runtime tried to send, with its destination, bytes and headers. It is a Serialized value, so another Out republishes it as it was. Its bytes are None where the publisher took the payload buffer and did not give it back, and such a message cannot be republished. Without alloc the refused message waits in a place of the subscription's, and its bytes are None as well where every place was taken; the embedded runtime page shows how many places a mount keeps.

The continuation runs once, after the delivery is settled. The handler never runs again because a runtime publish failed.

Without .error, the failure is logged with the subscription, the destination, the message type and the error, the delivery stays acknowledged, and the message is lost.

On a subscription whose broker moves deliveries itself (BrokerMoves), the runtime publishes no retry or dead-letter copy, so no copy's failure reaches the continuation. A reply is still published there, and a failed reply reaches .error as on any other subscription. Each outcome that carries .error there logs a warning naming the subscription.

.error combines with and_after on an outcome and with then on the context: the success half runs once the publish succeeded, .error once it failed. The embedded runtime's forms, rooms and costs are in the rustdoc of HandlerOutcome::error and After::error.

Example​

On the outcome​

The handler retries once at its cap, so the retry becomes a dead letter, and the broker refuses the dead-letter copy.

const RECHECK: Duration = Duration::from_secs(5);

#[subscriber("payments")]
async fn reconcile(payment: &Payment, ctx: &mut Context<'_, (), Alerts>) -> HandlerOutcome {
let id = payment.id;
let alerts = Arc::clone(ctx.state());
HandlerOutcome::retry_after(RECHECK).error(async move |failures: Failures| {
for failed in failures {
// The cause is the publisher's own error: a refused connection may pass later,
// anything else will not.
let transient = failed
.error()
.downcast_ref::<io::Error>()
.is_some_and(|err| err.kind() == io::ErrorKind::ConnectionRefused);
let (err, lost) = failed.into_parts();
// The dead-letter copy never left: record where it was going and why.
tracing::error!(id, %err, destination = lost.destination(), "dead letter lost");
alerts.lock().expect("alerts").push(Alert {
destination: lost.destination().to_owned(),
cause: err.to_string(),
transient,
});
}
})
}

A batch​

A batch handler that returns one outcome per element attaches .error to each; its Failures holds that element's failure. A #[subscriber] batch body that returns a single HandlerOutcome runs its .error once, with the failure of every element whose copy failed, in the batch's order.

const RECHECK: Duration = Duration::from_secs(5);

#[subscriber("payments")]
async fn reconcile_all(
payments: &[Payment],
ctx: &mut Context<'_, (), Lost>,
) -> Vec<HandlerOutcome> {
payments
.iter()
.map(|payment| {
let (id, lost) = (payment.id, Arc::clone(ctx.state()));
// Each element's `.error` receives the failure of that element's copy.
HandlerOutcome::retry_after(RECHECK).error(async move |failures: Failures| {
for failed in failures {
tracing::error!(id, error = %failed.error(), "the copy is lost");
lost.lock().expect("lost").push(id);
}
})
})
.collect()
}

A reply​

A reply has no outcome to carry .error, so the handler registers it on the context: ctx.after(outcome).error(..). When the replies of a batch fail to publish, the continuation receives every one of them.

#[subscriber("prices", reply)]
async fn quote(req: &PriceRequest, ctx: &mut Context<'_, (), Notes>) -> Quote {
let id = req.id;
let notes = Arc::clone(ctx.state());
ctx.after(HandlerOutcome::ack())
.error(async move |failures: Failures| {
for failed in failures {
tracing::warn!(id, error = %failed.error(), "the quote was not sent");
notes
.lock()
.expect("notes")
.push(format!("{id}: {}", failed.error()));
}
});
Quote { id }
}