use std::fmt;
use super::super::{
ProducerRejectionReason, ProducerStoreError,
ingress::{ProducerPortAdmissionError, ProducerPortPoison, ProducerPortRejectionReason},
};
use super::record::ProducerRecord;
use crate::completion::CompletionRegistryError;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProducerTrySendErrorKind {
EmptyTopic,
MissingExplicitPartition,
NegativeExplicitPartition,
DeadlineUnrepresentable,
TimestampUnrepresentable,
Contended,
CompletionCapacity,
RecordCapacity,
ByteCapacity,
RecordSizeUnrepresentable,
BatchCapacity,
AccumulatorPending,
DeadlineElapsed,
Closed,
LocalIdentityExhausted,
HostPoisoned,
InternalInvariant,
}
#[derive(Debug)]
pub struct ProducerTrySendError {
kind: ProducerTrySendErrorKind,
record: ProducerRecord,
detail: Option<String>,
}
impl ProducerTrySendError {
pub const fn kind(&self) -> ProducerTrySendErrorKind {
self.kind
}
pub fn into_record(self) -> ProducerRecord {
self.record
}
pub fn detail(&self) -> Option<&str> {
self.detail.as_deref()
}
pub(super) fn into_parts(self) -> (ProducerTrySendErrorKind, ProducerRecord, Option<String>) {
(self.kind, self.record, self.detail)
}
pub(super) const fn with_record(
kind: ProducerTrySendErrorKind,
record: ProducerRecord,
) -> Self {
Self {
kind,
record,
detail: None,
}
}
pub(super) fn from_port(error: ProducerPortAdmissionError) -> Self {
match error {
ProducerPortAdmissionError::Rejected(rejected) => {
let kind = map_rejection(rejected.reason());
Self::with_record(kind, ProducerRecord::from_stored(rejected.into_record()))
}
ProducerPortAdmissionError::Poisoned(ProducerPortPoison::BeforeAdmission {
record,
..
}) => Self::with_record(
ProducerTrySendErrorKind::HostPoisoned,
ProducerRecord::from_stored(record),
),
ProducerPortAdmissionError::Poisoned(ProducerPortPoison::BeforeOwnership {
error,
record,
}) => Self {
kind: ProducerTrySendErrorKind::InternalInvariant,
record: ProducerRecord::from_stored(record),
detail: Some(error.to_string()),
},
}
}
}
impl fmt::Display for ProducerTrySendError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.detail {
Some(detail) => write!(
formatter,
"producer try_send failed: {:?}: {detail}",
self.kind
),
None => write!(formatter, "producer try_send failed: {:?}", self.kind),
}
}
}
impl std::error::Error for ProducerTrySendError {}
fn map_rejection(reason: ProducerPortRejectionReason) -> ProducerTrySendErrorKind {
match reason {
ProducerPortRejectionReason::Contended => ProducerTrySendErrorKind::Contended,
ProducerPortRejectionReason::Host(reason) => map_host_rejection(reason),
}
}
fn map_host_rejection(reason: ProducerRejectionReason) -> ProducerTrySendErrorKind {
use kafka_client_core::AdmissionRejection;
match reason {
ProducerRejectionReason::Completion(CompletionRegistryError::Full)
| ProducerRejectionReason::Core(AdmissionRejection::CompletionCapacity) => {
ProducerTrySendErrorKind::CompletionCapacity
}
ProducerRejectionReason::Completion(CompletionRegistryError::NotifierStopped)
| ProducerRejectionReason::Core(AdmissionRejection::Closed) => {
ProducerTrySendErrorKind::Closed
}
ProducerRejectionReason::Store(ProducerStoreError::RecordCapacity) => {
ProducerTrySendErrorKind::RecordCapacity
}
ProducerRejectionReason::Store(ProducerStoreError::ByteCapacity)
| ProducerRejectionReason::Core(AdmissionRejection::ByteCapacity) => {
ProducerTrySendErrorKind::ByteCapacity
}
ProducerRejectionReason::Store(
ProducerStoreError::RetainedSizeOverflow | ProducerStoreError::HeaderCountOutOfRange,
)
| ProducerRejectionReason::Core(AdmissionRejection::ByteCountOverflow) => {
ProducerTrySendErrorKind::RecordSizeUnrepresentable
}
ProducerRejectionReason::Store(ProducerStoreError::BatchCapacity) => {
ProducerTrySendErrorKind::BatchCapacity
}
ProducerRejectionReason::Core(AdmissionRejection::AccumulatorPending) => {
ProducerTrySendErrorKind::AccumulatorPending
}
ProducerRejectionReason::Core(AdmissionRejection::DeadlineElapsed) => {
ProducerTrySendErrorKind::DeadlineElapsed
}
ProducerRejectionReason::HostPoisoned(_) => ProducerTrySendErrorKind::HostPoisoned,
ProducerRejectionReason::Waiting(
kafka_client_core::ProducerWaitingAdmissionError::Closed,
) => ProducerTrySendErrorKind::Closed,
ProducerRejectionReason::Waiting(
kafka_client_core::ProducerWaitingAdmissionError::DeadlineElapsed,
) => ProducerTrySendErrorKind::DeadlineElapsed,
ProducerRejectionReason::Waiting(
kafka_client_core::ProducerWaitingAdmissionError::RecordCapacity,
) => ProducerTrySendErrorKind::RecordCapacity,
ProducerRejectionReason::Waiting(
kafka_client_core::ProducerWaitingAdmissionError::ByteCapacity,
) => ProducerTrySendErrorKind::ByteCapacity,
ProducerRejectionReason::Waiting(
kafka_client_core::ProducerWaitingAdmissionError::ByteCountOverflow,
) => ProducerTrySendErrorKind::RecordSizeUnrepresentable,
ProducerRejectionReason::Waiting(
kafka_client_core::ProducerWaitingAdmissionError::IdentityExhausted,
)
| ProducerRejectionReason::Core(
AdmissionRejection::IdentityExhausted | AdmissionRejection::BatchIdentityExhausted,
)
| ProducerRejectionReason::Store(
ProducerStoreError::PayloadIdentityExhausted
| ProducerStoreError::TopicIdentityExhausted,
) => ProducerTrySendErrorKind::LocalIdentityExhausted,
ProducerRejectionReason::Completion(_)
| ProducerRejectionReason::Store(_)
| ProducerRejectionReason::Core(_) => ProducerTrySendErrorKind::InternalInvariant,
}
}