carrot_cake/consumers/error.rs
1use std::fmt;
2
3/// The error type returned by message handlers.
4#[derive(Debug)]
5pub struct HandlerError<E> {
6 /// The underlying error type returned by the message handler.
7 pub inner_error: E,
8 /// `error_type` distinguishes two classes of errors:
9 /// - transient errors; message processing might succeed if retried after a short delay
10 /// - fatal errors; no matter how many times you retry, processing will never succeed
11 ///
12 /// Check out [`ErrorType`]'s documentation for more details.
13 pub error_type: ErrorType,
14}
15
16impl<E: std::error::Error + 'static> std::error::Error for HandlerError<E> {
17 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
18 Some(&self.inner_error)
19 }
20}
21
22impl<E: fmt::Display> fmt::Display for HandlerError<E> {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 write!(
25 f,
26 "Handling of a message failed due to a {} issue - ",
27 self.error_type
28 )?;
29 write!(f, ".\n{}", self.inner_error)
30 }
31}
32
33/// Types of failure when handling a message.
34/// Used by the pub sub framework to handle retries/nacks/dead letter queues/etc.
35#[derive(Debug, Clone, Eq, PartialEq)]
36pub enum ErrorType {
37 /// Message processing might succeed if retried after a short delay.
38 ///
39 /// E.g. the message handler encountered a time out when trying to call an API to fulfil
40 /// the message processing requirements.
41 ///
42 /// The pubsub framework will execute the [transient error hook](crate::consumers::ConsumerTransientErrorHook)
43 /// before nacking the message.
44 Transient,
45 /// Message processing will never succeed, no matter how many times you retry or how long
46 /// you wait.
47 ///
48 /// E.g. the message payload is malformed and cannot be deserialized.
49 ///
50 /// The message will be nacked.
51 Fatal,
52}
53
54impl fmt::Display for ErrorType {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 match self {
57 Self::Transient => write!(f, "transient"),
58 Self::Fatal => write!(f, "fatal"),
59 }
60 }
61}