inertia_rust/
error.rs

1use std::{error::Error, fmt, io};
2
3use crate::node_process::NodeJsError;
4
5#[derive(Debug, Clone)]
6pub enum InertiaError {
7    SerializationError(String),
8    HeaderError(String),
9    SsrError(String),
10    RenderError(String),
11    NodeJsError(NodeJsError),
12}
13
14impl fmt::Display for InertiaError {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        write!(f, "Inertia Error: {}", self.get_cause())
17    }
18}
19
20pub trait IntoInertiaError {
21    fn into_inertia_error(self) -> InertiaError;
22}
23
24impl Error for InertiaError {}
25
26impl InertiaError {
27    pub fn get_cause(&self) -> String {
28        match self {
29            InertiaError::HeaderError(err) => err.clone(),
30            InertiaError::NodeJsError(node_err) => {
31                format!("{} ({})", node_err.get_cause(), node_err.get_description())
32            }
33            InertiaError::SerializationError(err) => err.clone(),
34            InertiaError::SsrError(err) => err.clone(),
35            InertiaError::RenderError(err) => err.clone(),
36        }
37    }
38
39    pub fn to_io_error(self) -> io::Error {
40        io::Error::other(self.get_cause())
41    }
42}
43
44impl IntoInertiaError for InertiaError {
45    fn into_inertia_error(self) -> InertiaError {
46        self
47    }
48}
49
50impl IntoInertiaError for serde_json::Error {
51    fn into_inertia_error(self) -> InertiaError {
52        InertiaError::SerializationError(self.to_string())
53    }
54}