rustapi_view/
error.rs

1//! View error types
2
3use thiserror::Error;
4
5/// Error type for view/template operations
6#[derive(Error, Debug)]
7pub enum ViewError {
8    /// Template not found
9    #[error("Template not found: {0}")]
10    TemplateNotFound(String),
11
12    /// Template rendering failed
13    #[error("Template rendering failed: {0}")]
14    RenderError(String),
15
16    /// Template parsing failed
17    #[error("Template parsing failed: {0}")]
18    ParseError(String),
19
20    /// Context serialization failed
21    #[error("Context serialization failed: {0}")]
22    SerializationError(String),
23
24    /// Template engine not initialized
25    #[error("Template engine not initialized")]
26    NotInitialized,
27
28    /// IO error
29    #[error("IO error: {0}")]
30    IoError(#[from] std::io::Error),
31
32    /// Tera error
33    #[error("Tera error: {0}")]
34    Tera(#[from] tera::Error),
35}
36
37impl ViewError {
38    /// Create a template not found error
39    pub fn not_found(template: impl Into<String>) -> Self {
40        Self::TemplateNotFound(template.into())
41    }
42
43    /// Create a render error
44    pub fn render_error(msg: impl Into<String>) -> Self {
45        Self::RenderError(msg.into())
46    }
47
48    /// Create a parse error
49    pub fn parse_error(msg: impl Into<String>) -> Self {
50        Self::ParseError(msg.into())
51    }
52
53    /// Create a serialization error
54    pub fn serialization_error(msg: impl Into<String>) -> Self {
55        Self::SerializationError(msg.into())
56    }
57}
58
59impl From<ViewError> for rustapi_core::ApiError {
60    fn from(err: ViewError) -> Self {
61        match err {
62            ViewError::TemplateNotFound(name) => {
63                rustapi_core::ApiError::internal(format!("Template not found: {}", name))
64            }
65            ViewError::NotInitialized => {
66                rustapi_core::ApiError::internal("Template engine not initialized")
67            }
68            _ => rustapi_core::ApiError::internal(err.to_string()),
69        }
70    }
71}