Write paths in errors as escaped ASCII text

This commit is contained in:
Oliver Hamlet
2025-04-28 20:40:37 +01:00
parent 6c664697a9
commit 3ee1789316
6 changed files with 33 additions and 11 deletions
+3 -1
View File
@@ -1,5 +1,7 @@
use std::path::PathBuf;
use crate::escape_ascii;
#[derive(Debug)]
pub(crate) struct ArchivePathParsingError {
path: PathBuf,
@@ -24,7 +26,7 @@ impl std::fmt::Display for ArchivePathParsingError {
write!(
f,
"failed to parse the archive at \"{}\"",
self.path.display()
escape_ascii(&self.path)
)
}
}
+2 -2
View File
@@ -6,10 +6,10 @@ pub use crate::plugin::error::PluginDataError;
use crate::plugin::error::PluginValidationError;
pub use crate::sorting::error::GroupsPathError;
use crate::Vertex;
use crate::sorting::error::{
BuildGroupsGraphError, PluginGraphValidationError, SortingError, display_cycle,
};
use crate::{Vertex, escape_ascii};
/// Represents an error that occurred while trying to create a [Game][crate::Game].
#[derive(Debug)]
@@ -25,7 +25,7 @@ impl std::fmt::Display for GameHandleCreationError {
Self::NotADirectory(p) => write!(
f,
"the path \"{}\" does not resolve to a directory",
p.display()
escape_ascii(p)
),
Self::LoadOrderError(_) => {
write!(f, "failed to initialise the load order game settings")
+6 -2
View File
@@ -1784,12 +1784,16 @@ mod tests {
Path::new("b").join(BLANK_ESM),
];
let expected_path = if cfg!(windows) {
"b\\\\Blank.esm"
} else {
"b/Blank.esm"
};
match game.load_plugins_common(&[&paths[0], &paths[1]], LoadScope::HeaderOnly) {
Err(LoadPluginsError::PluginValidationError(e)) => {
assert_eq!(
format!(
"the path \"{}\" has a filename that is not unique",
paths[1].display()
"the path \"{expected_path}\" has a filename that is not unique"
),
e.to_string()
);
+6
View File
@@ -119,6 +119,8 @@ mod sorting;
mod tests;
mod version;
use std::{path::Path, slice::EscapeAscii};
use fancy_regex::{Error as RegexImplError, Regex, RegexBuilder};
pub use database::{Database, WriteMode};
@@ -137,3 +139,7 @@ fn case_insensitive_regex(value: &str) -> Result<Regex, Box<RegexImplError>> {
.build()
.map_err(Into::into)
}
fn escape_ascii(path: &Path) -> EscapeAscii {
path.as_os_str().as_encoded_bytes().escape_ascii()
}
+12 -4
View File
@@ -4,7 +4,7 @@ use std::path::PathBuf;
use fancy_regex::Error as RegexImplError;
use saphyr::Marker;
use crate::metadata::MessageContent;
use crate::{escape_ascii, metadata::MessageContent};
use super::yaml::{YamlObjectType, to_unmarked_yaml};
@@ -244,7 +244,11 @@ impl LoadMetadataError {
impl std::fmt::Display for LoadMetadataError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "failed to parse the file at \"{}\"", self.path.display())
write!(
f,
"failed to parse the file at \"{}\"",
escape_ascii(&self.path)
)
}
}
@@ -380,10 +384,14 @@ impl std::fmt::Display for WriteMetadataError {
WriteMetadataErrorReason::ParentDirectoryNotFound => write!(
f,
"the parent directory of the path \"{}\" was not found",
self.path.display()
escape_ascii(&self.path)
),
WriteMetadataErrorReason::PathAlreadyExists => {
write!(f, "the path \"{}\" already exists", self.path.display())
write!(
f,
"the path \"{}\" already exists",
escape_ascii(&self.path)
)
}
WriteMetadataErrorReason::IoError(_) => write!(f, "an I/O error occurred"),
}
+4 -2
View File
@@ -2,6 +2,8 @@ use std::path::PathBuf;
use fancy_regex::Error as RegexImplError;
use crate::escape_ascii;
/// Represents an error that occurred while reading a parsed plugin's data.
#[derive(Debug)]
pub struct PluginDataError(esplugin::Error);
@@ -121,13 +123,13 @@ impl std::fmt::Display for PluginValidationError {
PluginValidationErrorReason::InvalidFilename(i) => write!(
f,
"the path \"{}\" has a filename that {}",
self.path.display(),
escape_ascii(&self.path),
i
),
PluginValidationErrorReason::InvalidPluginHeader => write!(
f,
"the file at \"{}\" does not have a valid plugin header",
self.path.display()
escape_ascii(&self.path)
),
}
}