intrepid-core 0.1.6

Manage complex async business logic with ease
Documentation
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")
    }
}