intrepid_core/extract/
extractor_error.rs

1use std::convert::Infallible;
2
3use crate::{PathError, StateNotReadyError};
4
5/// Sometimes the frame isn't a message when we expect it to be.
6#[derive(Debug, thiserror::Error)]
7#[error("Frame is not a message")]
8pub struct WrongFrameError;
9
10/// If the frame doesn't have the right meta content.
11#[derive(Debug, thiserror::Error)]
12#[error("Frame has wrong meta content: {0}")]
13pub struct MetaMismatchError(pub String);
14
15/// Errors with managing message parts.
16#[derive(Debug, thiserror::Error)]
17pub enum MessageFrameError {
18    /// Wrong frame type.
19    #[error(transparent)]
20    WrongFrame(#[from] WrongFrameError),
21
22    /// When the frame doesn't have the right meta content.
23    #[error(transparent)]
24    MetaMismatch(#[from] MetaMismatchError),
25
26    /// When deserializing message data fails.
27    #[error("malformed data field: {0}")]
28    MalformedJsonData(#[source] serde_json::Error),
29    /// When deserializing message meta fails.
30    #[error("malformed meta field: {0}")]
31    MalformedJsonMeta(#[source] serde_json::Error),
32}
33
34/// An error that occurs when extracting data from a frame.
35#[derive(Debug, thiserror::Error)]
36pub enum ExtractorError {
37    /// Frame is not a message.
38    #[error(transparent)]
39    MessageFrameError(#[from] MessageFrameError),
40    /// When the path extractor fails to extract and deserialize from routes.
41    #[error("Path routing failed in extraction: {0}")]
42    RoutingFailed(#[from] PathError),
43    /// When the state is not ready.
44    #[error(transparent)]
45    StateNotReady(#[from] StateNotReadyError),
46    /// For general box errors.
47    #[error(transparent)]
48    Boxed(#[from] tower::BoxError),
49    /// An error for consumers who just want to use a string.
50    #[error("extracting failed: {0}")]
51    ExtractionFailed(String),
52}
53
54impl From<String> for ExtractorError {
55    fn from(error: String) -> Self {
56        Self::ExtractionFailed(error)
57    }
58}
59
60impl From<Infallible> for ExtractorError {
61    fn from(_: Infallible) -> Self {
62        unreachable!("Infallible error thrown in extractor")
63    }
64}