1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! The crate's error (DESIGN 4.5): hand-written `Display` and `Error`, no `thiserror` (2.3).
use std::fmt;
/// What went wrong while building the catalog or switching it.
///
/// A broken or non-object locale file is an error with its path. The JS loader logs it and
/// skips the file; Rust refuses to start with half a catalog instead.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// A file or directory could not be read.
Io {
/// The path as given (a `dir` entry is joined to its directory).
path: String,
/// What the OS said.
source: std::io::Error,
},
/// A locale file, `messages` text or `i18n.json` is not valid JSON.
Json {
/// The file path, or `messages:<locale>/<page>` for text passed in code.
path: String,
/// Byte offset of the problem (a BOM counts).
offset: usize,
/// What is wrong there.
kind: &'static str,
},
/// Valid JSON, but not an object (an array, `null`, a string…).
NotObject {
/// The file path, or `messages:<locale>/<page>`.
path: String,
},
/// A locale that no file, `i18n.json` entry or builder call names.
UnknownLocale(String),
/// A locale `i18n.json` marks `"disabled": true`.
DisabledLocale(String),
/// A page name that is not allowed or not known (`__proto__`, an alias to nothing).
UnknownPage(String),
/// `i18n.json` has the wrong shape.
Config {
/// The config path, or `i18n.json` for `config_json`.
path: String,
/// Which field is wrong.
reason: &'static str,
},
/// A path `add_file` was given that the `remote` manifest does not list (8b.2: names
/// only from the catalog).
UnknownFile(String),
/// `install` was called while an instance is installed already.
AlreadyInstalled,
/// A free function (`set_locale`) needs an instance: none is installed, no scope sets one.
NotInstalled,
/// A named format (`numberFormats` / `datetimeFormats` of `i18n.json`,
/// `Builder::formats` / `Builder::datetime_formats`) uses an `Intl` option outside the
/// supported subset (`signDisplay: "always"`, `style: "unit"`, `timeStyle: "full"`,
/// `timeZoneName`…) or a value `Intl` throws on (`currency: "usdx"`,
/// `minimumFractionDigits` over `maximumFractionDigits`, `month: "numerical"`, a time
/// zone the platform does not know): refused at `build` rather than formatted
/// differently from the front end.
UnsupportedFormat {
/// The locale code the format is listed under.
locale: String,
/// The format's name.
name: String,
/// The option (`signDisplay`, `notation`, `maximumFractionDigits`…).
option: String,
},
/// `Builder::time_zone` names a zone the platform does not know (JS `RangeError`); without
/// the `tz` feature only UTC is known.
UnknownTimeZone(String),
/// The fetch closure of `ensure_async` failed for one file; the catalog is unchanged.
Fetch {
/// The manifest path that was being fetched.
path: String,
/// What the closure's error said (its `Display`).
message: String,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io { path, source } => write!(f, "cannot read {path}: {source}"),
Error::Json { path, offset, kind } => {
write!(f, "{path}: invalid JSON at byte {offset}: {kind}")
}
Error::NotObject { path } => write!(f, "{path}: expected a JSON object"),
Error::UnknownLocale(code) => write!(f, "unknown locale \"{code}\""),
Error::DisabledLocale(code) => write!(f, "locale \"{code}\" is disabled"),
Error::UnknownPage(name) => write!(f, "unknown page \"{name}\""),
Error::Config { path, reason } => write!(f, "{path}: {reason}"),
Error::UnknownFile(path) => write!(f, "unknown file \"{path}\""),
Error::Fetch { path, message } => write!(f, "cannot fetch {path}: {message}"),
Error::UnsupportedFormat {
locale,
name,
option,
} => write!(
f,
"format \"{name}\" of \"{locale}\": option \"{option}\" is not supported"
),
Error::UnknownTimeZone(name) => write!(f, "unknown time zone \"{name}\""),
Error::AlreadyInstalled => f.write_str("an i18n instance is installed already"),
Error::NotInstalled => {
f.write_str("no i18n instance: install() one or set it in a scope")
}
}
}
}
impl Error {
/// A copy for every waiter of one shared fetch (`Error` is not `Clone`: `Io` holds an
/// `io::Error`, which `add_file` never returns; anything else becomes `Fetch` with its
/// text).
pub(crate) fn duplicate(&self, file: &str) -> Error {
match self {
Error::Json { path, offset, kind } => Error::Json {
path: path.clone(),
offset: *offset,
kind,
},
Error::NotObject { path } => Error::NotObject { path: path.clone() },
Error::Fetch { path, message } => Error::Fetch {
path: path.clone(),
message: message.clone(),
},
other => Error::Fetch {
path: file.to_owned(),
message: other.to_string(),
},
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io { source, .. } => Some(source),
_ => None,
}
}
}