Skip to main content

fluent_templates/
error.rs

1use std::fmt;
2
3use unic_langid::{LanguageIdentifier, LanguageIdentifierError};
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    /// A locale directory was not a valid language identifier.
30    #[error("Error parsing LanguageIdentifier\n: {}", source)]
31    InvalidLanguageId {
32        /// The original parse error
33        #[from]
34        source: LanguageIdentifierError,
35    },
36}
37
38/// A wrapper struct around `Vec<fluent_syntax::parser::ParserError>`.
39#[derive(Debug)]
40pub struct FluentError(Vec<fluent_syntax::parser::ParserError>);
41
42impl From<Vec<fluent_syntax::parser::ParserError>> for FluentError {
43    fn from(errors: Vec<fluent_syntax::parser::ParserError>) -> Self {
44        Self(errors)
45    }
46}
47
48impl From<FluentError> for Vec<fluent_syntax::parser::ParserError> {
49    fn from(error: FluentError) -> Self {
50        error.0
51    }
52}
53
54impl fmt::Display for FluentError {
55    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56        for error in &self.0 {
57            write!(f, "{:?}", error)?;
58        }
59
60        Ok(())
61    }
62}
63
64impl std::error::Error for FluentError {}
65
66/// An error that happened while looking up messages
67#[derive(Debug, thiserror::Error)]
68pub enum LookupError {
69    #[error("Couldn't retrieve message with ID `{0}`")]
70    MessageRetrieval(String),
71    #[error("Couldn't find attribute `{attribute}` for message-id `{message_id}`")]
72    AttributeNotFound {
73        message_id: String,
74        attribute: String,
75    },
76    #[error("Language ID `{0}` has not been loaded")]
77    LangNotLoaded(LanguageIdentifier),
78    #[error("Fluent errors: {0:?}")]
79    FluentError(Vec<fluent_bundle::FluentError>),
80}