intrepid_core/extract/
extractor_error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::convert::Infallible;

use crate::{PathError, StateNotReadyError};

/// Sometimes the frame isn't a message when we expect it to be.
#[derive(Debug, thiserror::Error)]
#[error("Frame is not a message")]
pub struct WrongFrameError;

/// Errors with managing message parts.
#[derive(Debug, thiserror::Error)]
pub enum MessageFrameError {
    /// Wrong frame type.
    #[error(transparent)]
    WrongFrame(#[from] WrongFrameError),
    /// When deserializing message data fails.
    #[error("malformed data field: {0}")]
    MalformedJsonData(#[source] serde_json::Error),
    /// When deserializing message meta fails.
    #[error("malformed meta field: {0}")]
    MalformedJsonMeta(#[source] serde_json::Error),
}

/// An error that occurs when extracting data from a frame.
#[derive(Debug, thiserror::Error)]
pub enum ExtractorError {
    /// Frame is not a message.
    #[error(transparent)]
    MessageFrameError(#[from] MessageFrameError),
    /// When the path extractor fails to extract and deserialize from routes.
    #[error("Path routing failed in extraction: {0}")]
    RoutingFailed(#[from] PathError),
    /// When the state is not ready.
    #[error(transparent)]
    StateNotReady(#[from] StateNotReadyError),
    /// For general box errors.
    #[error(transparent)]
    Boxed(#[from] tower::BoxError),
    /// An error for consumers who just want to use a string.
    #[error("extracting failed: {0}")]
    ExtractionFailed(String),
}

impl From<String> for ExtractorError {
    fn from(error: String) -> Self {
        Self::ExtractionFailed(error)
    }
}

impl From<Infallible> for ExtractorError {
    fn from(_: Infallible) -> Self {
        unreachable!("Infallible error thrown in extractor")
    }
}