127 lines
4.6 KiB
Rust
127 lines
4.6 KiB
Rust
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
//! Typed validation errors. Every message names the file and says what is wrong in plain English,
|
|
//! so the UI can show it as is.
|
|
|
|
use std::fmt;
|
|
|
|
/// One problem found in the route files.
|
|
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
|
pub enum ValidationError {
|
|
#[error("{file}: the file is not valid TOML{}: {message}", line_suffix(*line))]
|
|
Syntax {
|
|
file: String,
|
|
line: Option<usize>,
|
|
message: String,
|
|
},
|
|
#[error("{file}: unknown field `{field}`")]
|
|
UnknownField { file: String, field: String },
|
|
#[error("{file}: required field `{field}` is missing")]
|
|
MissingField { file: String, field: String },
|
|
#[error("{file}: {message}")]
|
|
InvalidValue { file: String, message: String },
|
|
#[error("{file} appears twice in the routes")]
|
|
DuplicateFile { file: String },
|
|
#[error("{file} is larger than {max} bytes")]
|
|
FileTooLarge { file: String, max: usize },
|
|
#[error("{file} must be named after its hostname: {hostname}.toml")]
|
|
FileNameMismatch { file: String, hostname: String },
|
|
#[error("{file}: at most {max} upstreams per host")]
|
|
TooManyUpstreams { file: String, max: usize },
|
|
#[error("{file}: `{hostname}` is not a valid hostname: {reason}")]
|
|
InvalidHostname {
|
|
file: String,
|
|
hostname: String,
|
|
reason: &'static str,
|
|
},
|
|
#[error("hostname `{hostname}` is defined twice: in {first} and in {second}")]
|
|
DuplicateHostname {
|
|
hostname: String,
|
|
first: String,
|
|
second: String,
|
|
},
|
|
#[error("{file}: at least one upstream is needed")]
|
|
NoUpstreams { file: String },
|
|
#[error("{file}: upstream `{url}` is not usable: {reason}")]
|
|
BadUpstreamUrl {
|
|
file: String,
|
|
url: String,
|
|
reason: &'static str,
|
|
},
|
|
#[error("{file}: upstream `{url}` is listed twice")]
|
|
DuplicateUpstream { file: String, url: String },
|
|
#[error("{file}: secret `{secret}` is used but not declared in the site file")]
|
|
UndefinedSecret { file: String, secret: String },
|
|
#[error(
|
|
"{file}: response header `{header}` may not use a secret; secrets only go to upstreams"
|
|
)]
|
|
SecretInResponseHeader { file: String, header: String },
|
|
#[error(
|
|
"{file}: `{name}` is not a valid secret name (use a-z, 0-9, '_' and '-', at most 64 characters)"
|
|
)]
|
|
InvalidSecretName { file: String, name: String },
|
|
#[error("{file}: `{name}` is not a valid header name")]
|
|
InvalidHeaderName { file: String, name: String },
|
|
#[error("{file}: value for header `{header}` is not allowed: {reason}")]
|
|
InvalidHeaderValue {
|
|
file: String,
|
|
header: String,
|
|
reason: &'static str,
|
|
},
|
|
#[error("{file}: header `{header}` is managed by the proxy and cannot be changed by a rule")]
|
|
ProtectedHeader { file: String, header: String },
|
|
#[error("{file}: header `{header}` has more than one rule in the same list")]
|
|
DuplicateHeaderRule { file: String, header: String },
|
|
#[error("{file}: rule for header `{header}` is incomplete: {reason}")]
|
|
BadHeaderRule {
|
|
file: String,
|
|
header: String,
|
|
reason: &'static str,
|
|
},
|
|
#[error("{file}: health check is not usable: {reason}")]
|
|
BadHealthCheck { file: String, reason: &'static str },
|
|
#[error("{file}: auth = \"forward-auth\" needs a [forward_auth] section in the site file")]
|
|
ForwardAuthNotConfigured { file: String },
|
|
#[error("{file}: forward_auth is not usable: {reason}")]
|
|
BadForwardAuth { file: String, reason: &'static str },
|
|
#[error("{file}: acme is not usable: {reason}")]
|
|
BadAcme { file: String, reason: &'static str },
|
|
}
|
|
|
|
fn line_suffix(line: Option<usize>) -> String {
|
|
match line {
|
|
Some(l) => format!(" (line {l})"),
|
|
None => String::new(),
|
|
}
|
|
}
|
|
|
|
/// All problems found in one load, in a stable order (file by file, then hostname checks).
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct ValidationErrors(pub(crate) Vec<ValidationError>);
|
|
|
|
impl ValidationErrors {
|
|
pub fn errors(&self) -> &[ValidationError] {
|
|
&self.0
|
|
}
|
|
|
|
pub fn into_errors(self) -> Vec<ValidationError> {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for ValidationErrors {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
for (i, e) in self.0.iter().enumerate() {
|
|
if i > 0 {
|
|
writeln!(f)?;
|
|
}
|
|
write!(f, "{e}")?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for ValidationErrors {}
|