aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src/error.rs
diff options
context:
space:
mode:
authorLibravatar René Kijewski <Kijewski@users.noreply.github.com>2022-01-28 10:48:46 +0100
committerLibravatar GitHub <noreply@github.com>2022-01-28 10:48:46 +0100
commit85ad2e6ba3a205e9b648431318aac0e75c027a82 (patch)
tree5e186e2b9d4efcc97666a9d58410dfda238f5735 /askama_shared/src/error.rs
parentcb351fe6b1dda644a4ec023dc850cdfe83732503 (diff)
downloadaskama-85ad2e6ba3a205e9b648431318aac0e75c027a82.tar.gz
askama-85ad2e6ba3a205e9b648431318aac0e75c027a82.tar.bz2
askama-85ad2e6ba3a205e9b648431318aac0e75c027a82.zip
Implement error propagation expression: `?` (#590)
This change allows using the operator `?` in askama expressions. It works like the same operator in Rust: if a `Result` is `Ok`, it is unwrapped. If it is an error, then the `render()` method fails with this error value.
Diffstat (limited to '')
-rw-r--r--askama_shared/src/error.rs15
1 files changed, 10 insertions, 5 deletions
diff --git a/askama_shared/src/error.rs b/askama_shared/src/error.rs
index 4394eea..98f2703 100644
--- a/askama_shared/src/error.rs
+++ b/askama_shared/src/error.rs
@@ -1,6 +1,6 @@
use std::fmt::{self, Display};
-pub type Result<I> = ::std::result::Result<I, Error>;
+pub type Result<I, E = Error> = ::std::result::Result<I, E>;
/// askama error type
///
@@ -28,6 +28,9 @@ pub enum Error {
/// formatting error
Fmt(fmt::Error),
+ /// an error raised by using `?` in a template
+ Custom(Box<dyn std::error::Error + Send + Sync>),
+
/// json conversion error
#[cfg(feature = "serde_json")]
Json(::serde_json::Error),
@@ -41,6 +44,7 @@ impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> {
match *self {
Error::Fmt(ref err) => err.source(),
+ Error::Custom(ref err) => Some(err.as_ref()),
#[cfg(feature = "serde_json")]
Error::Json(ref err) => err.source(),
#[cfg(feature = "serde_yaml")]
@@ -51,12 +55,13 @@ impl std::error::Error for Error {
impl Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
- match *self {
- Error::Fmt(ref err) => write!(formatter, "formatting error: {}", err),
+ match self {
+ Error::Fmt(err) => write!(formatter, "formatting error: {}", err),
+ Error::Custom(err) => write!(formatter, "{}", err),
#[cfg(feature = "serde_json")]
- Error::Json(ref err) => write!(formatter, "json conversion error: {}", err),
+ Error::Json(err) => write!(formatter, "json conversion error: {}", err),
#[cfg(feature = "serde_yaml")]
- Error::Yaml(ref err) => write!(formatter, "yaml conversion error: {}", err),
+ Error::Yaml(err) => write!(formatter, "yaml conversion error: {}", err),
}
}
}