fluent_templates/
error.rs

1use std::fmt;
2
3use unic_langid::LanguageIdentifier;
4
5/// Errors that can occur when loading or parsing fluent resources.
6#[derive(Debug, thiserror::Error)]
7pub enum LoaderError {
8    /// An `io::Error` occurred while interacting with `path`.
9    #[error("Error with {}\n: {}", path.display(), source)]
10    Fs {
11        /// The path to file with the error.
12        path: std::path::PathBuf,
13        /// The error source.
14        source: std::io::Error,
15    },
16    /// An error was found in the fluent syntax.
17    #[error("Error parsing Fluent\n: {}", source)]
18    Fluent {
19        /// The original parse errors
20        #[from]
21        source: FluentError,
22    },
23    /// An error was found whilst loading a bundle at runtime.
24    #[error("Failed to add FTL resources to the bundle")]
25    FluentBundle {
26        /// The original bundle errors
27        errors: Vec<fluent_bundle::FluentError>,
28    },
29}
30
31/// A wrapper struct around `Vec<fluent_syntax::parser::ParserError>`.
32#[derive(Debug)]
33pub struct FluentError(Vec<fluent_syntax::parser::ParserError>);
34
35impl From<Vec<fluent_syntax::parser::ParserError>> for FluentError {
36    fn from(errors: Vec<fluent_syntax::parser::ParserError>) -> Self {
37        Self(errors)
38    }
39}
40
41impl From<FluentError> for Vec<fluent_syntax::parser::ParserError> {
42    fn from(error: FluentError) -> Self {
43        error.0
44    }
45}
46
47impl fmt::Display for FluentError {
48    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49        for error in &self.0 {
50            write!(f, "{:?}", error)?;
51        }
52
53        Ok(())
54    }
55}
56
57impl std::error::Error for FluentError {}
58
59/// An error that happened while looking up messages
60#[derive(Debug, thiserror::Error)]
61pub enum LookupError {
62    #[error("Couldn't retrieve message with ID `{0}`")]
63    MessageRetrieval(String),
64    #[error("Couldn't find attribute `{attribute}` for message-id `{message_id}`")]
65    AttributeNotFound {
66        message_id: String,
67        attribute: String,
68    },
69    #[error("Language ID `{0}` has not been loaded")]
70    LangNotLoaded(LanguageIdentifier),
71    #[error("Fluent errors: {0:?}")]
72    FluentError(Vec<fluent_bundle::FluentError>),
73}