Files
libloot/cpp/src/error.rs
T

121 lines
4.7 KiB
Rust
Raw Normal View History

2025-03-17 22:01:35 +00:00
use crate::game::NotValidUtf8;
2025-05-16 17:16:58 +01:00
use libloot_ffi_errors::{UnsupportedEnumValueError, fmt_error_chain, variant_box_from_error};
2025-03-17 22:01:35 +00:00
use libloot::{
error::{
ConditionEvaluationError, DatabaseLockPoisonError, GameHandleCreationError,
GroupsPathError, LoadOrderError, LoadOrderStateError, LoadPluginsError,
MetadataRetrievalError, PluginDataError, SortPluginsError,
},
metadata::error::{
LoadMetadataError, MultilingualMessageContentsError, RegexError, WriteMetadataError,
},
};
#[derive(Debug)]
pub enum VerboseError {
CyclicInteractionError(Vec<libloot::Vertex>),
UndefinedGroupError(String),
2025-05-16 17:16:58 +01:00
PluginNotLoadedError(String),
InvalidArgument(Box<dyn std::error::Error>),
2025-03-17 22:01:35 +00:00
Other(Box<dyn std::error::Error>),
}
impl std::fmt::Display for VerboseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::CyclicInteractionError(cycle) => {
write!(f, "CyclicInteractionError: ")?;
for vertex in cycle {
2025-08-06 09:15:40 +01:00
let name = vertex.name().replace('\\', "\\\\").replace('>', "\\>");
2025-03-17 22:01:35 +00:00
match vertex.out_edge_type() {
2025-08-06 09:15:40 +01:00
Some(e) => write!(f, "{name} > {e} > ")?,
None => write!(f, "{name}")?,
2025-03-17 22:01:35 +00:00
}
}
Ok(())
}
Self::UndefinedGroupError(group) => {
write!(f, "UndefinedGroupError: {group}",)
2025-03-17 22:01:35 +00:00
}
2025-05-16 17:16:58 +01:00
Self::PluginNotLoadedError(plugin) => write!(f, "PluginNotLoadedError: {plugin}"),
Self::InvalidArgument(e) => {
write!(f, "InvalidArgument: ")?;
fmt_error_chain(e.as_ref(), f)
}
2025-04-22 18:39:13 +01:00
Self::Other(e) => fmt_error_chain(e.as_ref(), f),
2025-03-17 22:01:35 +00:00
}
}
}
2025-04-22 18:39:13 +01:00
variant_box_from_error!(UnsupportedEnumValueError, VerboseError::Other);
variant_box_from_error!(NotValidUtf8, VerboseError::Other);
variant_box_from_error!(DatabaseLockPoisonError, VerboseError::Other);
variant_box_from_error!(MultilingualMessageContentsError, VerboseError::Other);
variant_box_from_error!(RegexError, VerboseError::Other);
2025-05-16 08:39:13 +01:00
variant_box_from_error!(LoadMetadataError, VerboseError::Other);
variant_box_from_error!(WriteMetadataError, VerboseError::Other);
2025-05-16 08:44:36 +01:00
variant_box_from_error!(ConditionEvaluationError, VerboseError::Other);
variant_box_from_error!(MetadataRetrievalError, VerboseError::Other);
2025-05-16 08:57:07 +01:00
variant_box_from_error!(LoadOrderError, VerboseError::Other);
variant_box_from_error!(LoadOrderStateError, VerboseError::Other);
2025-05-16 17:16:58 +01:00
variant_box_from_error!(PluginDataError, VerboseError::Other);
2025-04-22 18:39:13 +01:00
2025-03-17 22:01:35 +00:00
impl From<GameHandleCreationError> for VerboseError {
fn from(value: GameHandleCreationError) -> Self {
match value {
GameHandleCreationError::NotADirectory(_) => Self::InvalidArgument(Box::new(value)),
2025-05-16 08:57:07 +01:00
GameHandleCreationError::LoadOrderError(_) | _ => Self::Other(Box::new(value)),
2025-03-17 22:01:35 +00:00
}
}
}
impl From<LoadPluginsError> for VerboseError {
fn from(value: LoadPluginsError) -> Self {
match value {
2025-05-16 17:16:58 +01:00
LoadPluginsError::PluginNotLoaded(p) => Self::PluginNotLoadedError(p),
LoadPluginsError::PluginValidationError(_) => Self::InvalidArgument(Box::new(value)),
2025-05-16 17:16:58 +01:00
LoadPluginsError::DatabaseLockPoisoned
| LoadPluginsError::IoError(_)
| LoadPluginsError::PluginDataError(_)
| _ => Self::Other(Box::new(value)),
2025-03-17 22:01:35 +00:00
}
}
}
impl From<SortPluginsError> for VerboseError {
fn from(value: SortPluginsError) -> Self {
match value {
SortPluginsError::UndefinedGroup(g) => Self::UndefinedGroupError(g),
SortPluginsError::CycleFound(cycle) => Self::CyclicInteractionError(cycle),
2025-05-16 17:16:58 +01:00
SortPluginsError::PluginNotLoaded(p) => Self::PluginNotLoadedError(p),
SortPluginsError::DatabaseLockPoisoned
| SortPluginsError::CycleFoundInvolving(_)
| SortPluginsError::PathfindingError(_)
2025-05-16 17:16:58 +01:00
| SortPluginsError::PluginDataError(_)
| _ => Self::Other(Box::new(value)),
2025-03-17 22:01:35 +00:00
}
}
}
impl From<GroupsPathError> for VerboseError {
fn from(value: GroupsPathError) -> Self {
match value {
GroupsPathError::UndefinedGroup(g) => Self::UndefinedGroupError(g),
GroupsPathError::CycleFound(cycle) => Self::CyclicInteractionError(cycle),
GroupsPathError::PathfindingError(_) | _ => Self::Other(Box::new(value)),
2025-03-17 22:01:35 +00:00
}
}
}
#[derive(Clone, Copy, Debug)]
2025-03-17 22:01:35 +00:00
pub struct EmptyOptionalError;
impl std::fmt::Display for EmptyOptionalError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "optional is empty")
}
}
impl std::error::Error for EmptyOptionalError {}