inertia_rust/
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::{error::Error, fmt, io};

use crate::node_process::NodeJsError;

#[derive(Debug, Clone)]
pub enum InertiaError {
    SerializationError(String),
    HeaderError(String),
    SsrError(String),
    RenderError(String),
    NodeJsError(NodeJsError),
}

impl fmt::Display for InertiaError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Inertia Error: {}", self.get_cause())
    }
}

pub trait IntoInertiaError {
    fn into_inertia_error(self) -> InertiaError;
}

impl Error for InertiaError {}

impl InertiaError {
    pub fn get_cause(&self) -> String {
        match self {
            InertiaError::HeaderError(err) => err.clone(),
            InertiaError::NodeJsError(node_err) => {
                format!("{} ({})", node_err.get_cause(), node_err.get_description())
            }
            InertiaError::SerializationError(err) => err.clone(),
            InertiaError::SsrError(err) => err.clone(),
            InertiaError::RenderError(err) => err.clone(),
        }
    }

    pub fn to_io_error(self) -> io::Error {
        io::Error::new(io::ErrorKind::Other, self.get_cause())
    }
}

impl IntoInertiaError for InertiaError {
    fn into_inertia_error(self) -> InertiaError {
        self
    }
}

impl IntoInertiaError for serde_json::Error {
    fn into_inertia_error(self) -> InertiaError {
        InertiaError::SerializationError(self.to_string())
    }
}