batch_aint_one/
error.rs

1use std::fmt::Display;
2
3use thiserror::Error;
4use tokio::sync::{mpsc::error::SendError, oneshot::error::RecvError};
5
6/// An error that occurred while trying to batch.
7#[derive(Error, Debug)]
8#[non_exhaustive]
9pub enum BatchError<E: Display> {
10    /// Something went wrong while submitting an input for processing.
11    ///
12    /// Unrecoverable.
13    #[error("Unable to send item to the worker for batching: channel closed")]
14    Tx,
15
16    /// Something went wrong while waiting for the output of a batch.
17    ///
18    /// Unrecoverable.
19    #[error("Error while waiting for batch results: channel closed. {}", .0)]
20    Rx(RecvError),
21
22    /// The current batch is full so the item was rejected.
23    ///
24    /// Recoverable.
25    #[error("Batch item rejected: {0}")]
26    Rejected(RejectionReason),
27
28    /// Something went wrong while processing a batch.
29    #[error("The entire batch failed: {}", .0)]
30    BatchFailed(E),
31}
32
33#[derive(Debug)]
34#[non_exhaustive]
35pub enum RejectionReason {
36    /// The batch is full and still waiting to be processed.
37    BatchFull,
38    /// The batch is full and no more batches can be processed concurrently.
39    MaxConcurrency,
40}
41
42impl Display for RejectionReason {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        f.write_str(match self {
45            RejectionReason::BatchFull => "the batch is full",
46            RejectionReason::MaxConcurrency => "the key has reached maximum concurrency",
47        })
48    }
49}
50
51pub type BatchResult<T, E> = std::result::Result<T, BatchError<E>>;
52
53impl<E: Display> From<RecvError> for BatchError<E> {
54    fn from(rx_err: RecvError) -> Self {
55        BatchError::Rx(rx_err)
56    }
57}
58
59impl<T, E: Display> From<SendError<T>> for BatchError<E> {
60    fn from(_tx_err: SendError<T>) -> Self {
61        BatchError::Tx
62    }
63}
64
65impl<E> BatchError<E>
66where
67    E: Display,
68{
69    /// Get the inner error for general batch failures, otherwise self.
70    pub fn inner(self) -> BatchResult<E, E> {
71        match self {
72            BatchError::BatchFailed(e) => Ok(e),
73            _ => Err(self),
74        }
75    }
76}