bracket/error/
render.rs

1//! Errors generated when rendering templates.
2use crate::error::{HelperError, IoError};
3use std::fmt;
4use thiserror::Error;
5
6/// Errors generated during rendering.
7#[derive(Error)]
8pub enum RenderError {
9    /// Error when a partial could not be found.
10    #[error("Partial '{0}' not found")]
11    PartialNotFound(String),
12
13    /// Error when a variable could not be resolved.
14    #[error("Variable '{0}' not found in {1}, check the variable path and verify the template data")]
15    VariableNotFound(String, String),
16
17    /// Error when a helper could not be found.
18    #[error("Helper '{0}' not found, check the name")]
19    HelperNotFound(String),
20
21    /// Error when evaluating a path and a syntax error occurs.
22    ///
23    /// Paths can be dynamically evaluated when the
24    /// [evaluate()](crate::render::Render#method.evaluate) function is called
25    /// inside a helper.
26    #[error("Syntax error while evaluating path '{0}'")]
27    EvaluatePath(String),
28
29    /// Error when a cycle is detected whilst handling a partial.
30    #[error("Cycle detected whilst processing partial '{0}'")]
31    PartialCycle(String),
32
33    /// Error when a cycle is detected whilst handling a helper.
34    #[error("Cycle detected whilst processing helper '{0}'")]
35    HelperCycle(String),
36
37    /// Error when a partial is not a simple identifier.
38    #[error("Partial names must be simple identifiers, got path '{0}'")]
39    PartialIdentifier(String),
40    /// Error when a block is not a simple identifier.
41    #[error("Block names must be simple identifiers, got path '{0}'")]
42    BlockIdentifier(String),
43    /// Error attempting to invoke a sub-expression outside of a partial target context.
44    #[error("Block target sub expressions are only supported for partials")]
45    BlockTargetSubExpr,
46
47    /// Wrap a helper error.
48    #[error(transparent)]
49    Helper(#[from] HelperError),
50
51    /// Wrap a syntax error.
52    //#[error(transparent)]
53    //Syntax(#[from] Box<SyntaxError>),
54
55    /// Proxy for IO errors.
56    #[error(transparent)]
57    Io(#[from] IoError),
58
59    /// Proxy for JSON errors.
60    #[error(transparent)]
61    Json(#[from] serde_json::Error),
62}
63
64impl fmt::Debug for RenderError {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        write!(f, "{}", self.to_string())
67        // TODO: support source code snippets
68    }
69}
70
71impl From<std::io::Error> for RenderError {
72    fn from(err: std::io::Error) -> Self {
73        Self::Io(IoError::Io(err))
74    }
75}
76
77impl PartialEq for RenderError {
78    fn eq(&self, other: &Self) -> bool {
79        match (self, other) {
80            (Self::PartialNotFound(ref s), Self::PartialNotFound(ref o)) => {
81                s == o
82            }
83            _ => false,
84        }
85    }
86}
87
88impl Eq for RenderError {}