laravel_template_engine/
error.rs1use std::error::Error;
4use std::fmt;
5
6use laravel::errors::Status;
7
8use crate::Issue;
9
10#[derive(Debug, Clone, PartialEq, Eq, Status)]
12#[status(422)]
13#[non_exhaustive]
14pub enum TemplateError {
15 Invalid(Vec<Issue>),
17 UnknownType(String),
19 NotFound(i64),
21 NoDocument {
24 type_key: String,
26 found: usize,
28 },
29 Render(String),
31 PdfDisabled,
33 NoNotification {
35 type_key: String,
37 channel: String,
39 },
40}
41
42impl TemplateError {
43 #[must_use]
45 pub fn issues(&self) -> &[Issue] {
46 match self {
47 Self::Invalid(issues) => issues,
48 _ => &[],
49 }
50 }
51}
52
53impl fmt::Display for TemplateError {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 match self {
56 Self::Invalid(issues) => {
57 f.write_str("The template has mistakes:")?;
58 for issue in issues {
59 write!(f, "\n- {issue}")?;
60 }
61 Ok(())
62 }
63 Self::UnknownType(key) => write!(f, "There's no template type `{key}`."),
64 Self::NotFound(id) => write!(f, "The template `{id}` doesn't exist."),
65 Self::NoDocument { type_key, found: 0 } => {
66 write!(f, "`{type_key}` has no active document template.")
67 }
68 Self::NoDocument { type_key, found } => write!(
69 f,
70 "`{type_key}` has {found} active document templates; choose one with `named` or `template`."
71 ),
72 Self::Render(message) => write!(f, "The template didn't render: {message}"),
73 Self::PdfDisabled => f.write_str("PDF documents need the `pdf` feature of laravel-template-engine."),
74 Self::NoNotification { type_key, channel } => {
75 write!(f, "`{type_key}` has no active notification template for `{channel}`.")
76 }
77 }
78 }
79}
80
81impl Error for TemplateError {}