Rework handling of esplugin errors

The error scenario represented by PluginNotLoadedError is distinguished from all other esplugin errors by LOOT, so it's worth handling separately.
This commit is contained in:
Oliver Hamlet
2025-06-08 20:26:07 +01:00
parent 05c984c769
commit 11bd321861
20 changed files with 81 additions and 321 deletions
+1 -2
View File
@@ -68,7 +68,6 @@ set(LIBLOOT_SRC_API_CPP_FILES
"${CMAKE_SOURCE_DIR}/src/api/convert.cpp"
"${CMAKE_SOURCE_DIR}/src/api/database.cpp"
"${CMAKE_SOURCE_DIR}/src/api/exception/cyclic_interaction_error.cpp"
"${CMAKE_SOURCE_DIR}/src/api/exception/error_categories.cpp"
"${CMAKE_SOURCE_DIR}/src/api/exception/exception.cpp"
"${CMAKE_SOURCE_DIR}/src/api/exception/undefined_group_error.cpp"
"${CMAKE_SOURCE_DIR}/src/api/metadata/conditional_metadata.cpp"
@@ -89,8 +88,8 @@ set(LIBLOOT_INCLUDE_H_FILES
"${CMAKE_SOURCE_DIR}/include/loot/api.h"
"${CMAKE_SOURCE_DIR}/include/loot/api_decorator.h"
"${CMAKE_SOURCE_DIR}/include/loot/database_interface.h"
"${CMAKE_SOURCE_DIR}/include/loot/exception/error_categories.h"
"${CMAKE_SOURCE_DIR}/include/loot/exception/cyclic_interaction_error.h"
"${CMAKE_SOURCE_DIR}/include/loot/exception/plugin_not_loaded_error.h"
"${CMAKE_SOURCE_DIR}/include/loot/exception/undefined_group_error.h"
"${CMAKE_SOURCE_DIR}/include/loot/enum/edge_type.h"
"${CMAKE_SOURCE_DIR}/include/loot/enum/game_type.h"
+3 -8
View File
@@ -95,13 +95,8 @@ Exceptions
.. doxygenclass:: loot::CyclicInteractionError
:members:
.. doxygenclass:: loot::UndefinedGroupError
.. doxygenclass:: loot::PluginNotLoadedError
:members:
Error Categories
================
LOOT uses error category objects to identify errors with codes that originate in
lower-level libraries.
.. doxygenfunction:: loot::esplugin_category
.. doxygenclass:: loot::UndefinedGroupError
:members:
+1 -1
View File
@@ -35,7 +35,7 @@
#include "loot/enum/game_type.h"
#include "loot/enum/log_level.h"
#include "loot/exception/cyclic_interaction_error.h"
#include "loot/exception/error_categories.h"
#include "loot/exception/plugin_not_loaded_error.h"
#include "loot/exception/undefined_group_error.h"
#include "loot/game_interface.h"
#include "loot/loot_version.h"
@@ -22,21 +22,20 @@
<https://www.gnu.org/licenses/>.
*/
#ifndef LOOT_ERROR_CATEGORIES
#define LOOT_ERROR_CATEGORIES
#ifndef LOOT_EXCEPTION_PLUGIN_NOT_LOADED_ERROR
#define LOOT_EXCEPTION_PLUGIN_NOT_LOADED_ERROR
#include <system_error>
#include "loot/api_decorator.h"
#include <stdexcept>
namespace loot {
/**
* @brief Get the error category that can be used to identify system_error
* exceptions that are due to esplugin errors.
* @returns A reference to the static object of unspecified runtime type,
* derived from std::error_category.
* @brief An exception class thrown if a plugin that must be loaded hasn't been
* loaded.
*/
LOOT_API const std::error_category& esplugin_category();
class PluginNotLoadedError : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
}
#endif
@@ -1,46 +0,0 @@
/* LOOT
A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and
Fallout: New Vegas.
Copyright (C) 2012-2016 WrinklyNinja
This file is part of LOOT.
LOOT is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
LOOT is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LOOT. If not, see
<https://www.gnu.org/licenses/>.
*/
#include "loot/exception/error_categories.h"
#include <string>
namespace loot {
namespace detail {
class esplugin_category : public std::error_category {
const char* name() const noexcept override { return "esplugin"; }
std::string message(int) const override { return "esplugin error"; }
bool equivalent(const std::error_code& code, int) const noexcept override {
return code.category().name() == name();
}
};
}
LOOT_API const std::error_category& esplugin_category() {
static detail::esplugin_category instance;
return instance;
}
}
+6 -21
View File
@@ -3,7 +3,7 @@
#include <charconv>
#include "loot/exception/cyclic_interaction_error.h"
#include "loot/exception/error_categories.h"
#include "loot/exception/plugin_not_loaded_error.h"
#include "loot/exception/undefined_group_error.h"
#include "loot/vertex.h"
@@ -15,7 +15,8 @@ using loot::Vertex;
constexpr std::string_view CYCLIC_ERROR_PREFIX = "CyclicInteractionError: "sv;
constexpr std::string_view UNDEFINED_GROUP_ERROR_PREFIX =
"UndefinedGroupError: "sv;
constexpr std::string_view ESPLUGIN_ERROR_PREFIX = "EspluginError: "sv;
constexpr std::string_view PLUGIN_NOT_LOADED_ERROR_PREFIX =
"PluginNotLoadedError: "sv;
constexpr std::string_view INVALID_ARGUMENT_PREFIX = "InvalidArgument: "sv;
bool startsWith(std::string_view str, std::string_view prefix) {
@@ -112,20 +113,6 @@ std::string getErrorSuffix(std::string_view what) {
return std::string(what.substr(sepPos + 2));
}
std::pair<int, std::string> parseSystemError(std::string_view whatSuffix) {
const auto sepPos = whatSuffix.find(": ");
int code;
const auto result =
std::from_chars(whatSuffix.data(), whatSuffix.data() + sepPos, code);
if (result.ec != std::errc{}) {
std::string err = "Could not parse error code from string: ";
err += whatSuffix;
throw std::runtime_error(err);
}
return std::make_pair(code, std::string(whatSuffix.substr(sepPos + 2)));
}
}
namespace loot {
@@ -136,12 +123,10 @@ std::exception_ptr mapError(const ::rust::Error& error) {
} else if (startsWith(error.what(), UNDEFINED_GROUP_ERROR_PREFIX)) {
return std::make_exception_ptr(
UndefinedGroupError(getErrorSuffix(error.what())));
} else if (startsWith(error.what(), ESPLUGIN_ERROR_PREFIX)) {
const auto [code, details] = parseSystemError(
std::string_view(error.what()).substr(ESPLUGIN_ERROR_PREFIX.size()));
} else if (startsWith(error.what(), PLUGIN_NOT_LOADED_ERROR_PREFIX)) {
return std::make_exception_ptr(
std::system_error(code, esplugin_category(), details));
PluginNotLoadedError("The plugin \"" + getErrorSuffix(error.what()) +
"\" has not been loaded"));
} else if (startsWith(error.what(), INVALID_ARGUMENT_PREFIX)) {
return std::make_exception_ptr(
std::invalid_argument(getErrorSuffix(error.what())));
+11 -24
View File
@@ -1,8 +1,5 @@
use crate::game::NotValidUtf8;
use libloot_ffi_errors::{
SystemError, SystemErrorCategory, UnsupportedEnumValueError, fmt_error_chain,
variant_box_from_error,
};
use libloot_ffi_errors::{UnsupportedEnumValueError, fmt_error_chain, variant_box_from_error};
use libloot::{
error::{
@@ -19,7 +16,7 @@ use libloot::{
pub enum VerboseError {
CyclicInteractionError(Vec<libloot::Vertex>),
UndefinedGroupError(String),
SystemError(SystemError),
PluginNotLoadedError(String),
InvalidArgument(String),
Other(Box<dyn std::error::Error>),
}
@@ -41,13 +38,7 @@ impl std::fmt::Display for VerboseError {
Self::UndefinedGroupError(group) => {
write!(f, "UndefinedGroupError: {group}",)
}
Self::SystemError(e) => {
let prefix = match e.category() {
SystemErrorCategory::Esplugin => "EspluginError",
_ => "UnknownCategoryError",
};
write!(f, "{}: {}: {}", prefix, e.code(), e.message())
}
Self::PluginNotLoadedError(plugin) => write!(f, "PluginNotLoadedError: {plugin}"),
Self::InvalidArgument(s) => write!(f, "InvalidArgument: {s}"),
Self::Other(e) => fmt_error_chain(e.as_ref(), f),
}
@@ -65,6 +56,7 @@ variant_box_from_error!(ConditionEvaluationError, VerboseError::Other);
variant_box_from_error!(MetadataRetrievalError, VerboseError::Other);
variant_box_from_error!(LoadOrderError, VerboseError::Other);
variant_box_from_error!(LoadOrderStateError, VerboseError::Other);
variant_box_from_error!(PluginDataError, VerboseError::Other);
impl From<GameHandleCreationError> for VerboseError {
fn from(value: GameHandleCreationError) -> Self {
@@ -78,11 +70,12 @@ impl From<GameHandleCreationError> for VerboseError {
impl From<LoadPluginsError> for VerboseError {
fn from(value: LoadPluginsError) -> Self {
match value {
LoadPluginsError::PluginDataError(e) => e.into(),
LoadPluginsError::PluginNotLoaded(p) => Self::PluginNotLoadedError(p),
LoadPluginsError::PluginValidationError(_) => Self::InvalidArgument(value.to_string()),
LoadPluginsError::DatabaseLockPoisoned | LoadPluginsError::IoError(_) | _ => {
Self::Other(Box::new(value))
}
LoadPluginsError::DatabaseLockPoisoned
| LoadPluginsError::IoError(_)
| LoadPluginsError::PluginDataError(_)
| _ => Self::Other(Box::new(value)),
}
}
}
@@ -92,11 +85,11 @@ impl From<SortPluginsError> for VerboseError {
match value {
SortPluginsError::UndefinedGroup(g) => Self::UndefinedGroupError(g),
SortPluginsError::CycleFound(cycle) => Self::CyclicInteractionError(cycle),
SortPluginsError::PluginDataError(e) => e.into(),
SortPluginsError::PluginNotLoaded(p) => Self::PluginNotLoadedError(p),
SortPluginsError::DatabaseLockPoisoned
| SortPluginsError::PluginNotLoaded(_)
| SortPluginsError::CycleFoundInvolving(_)
| SortPluginsError::PathfindingError(_)
| SortPluginsError::PluginDataError(_)
| _ => Self::Other(Box::new(value)),
}
}
@@ -112,12 +105,6 @@ impl From<GroupsPathError> for VerboseError {
}
}
impl From<PluginDataError> for VerboseError {
fn from(value: PluginDataError) -> Self {
Self::SystemError(SystemError::from(value))
}
}
#[derive(Clone, Copy, Debug)]
pub struct EmptyOptionalError;
@@ -370,13 +370,8 @@ TEST_P(
if (GetParam() == GameType::tes3 || GetParam() == GameType::openmw ||
GetParam() == GameType::starfield) {
try {
handle_->LoadPlugins({blankMasterDependentEsm}, false);
FAIL();
} catch (const std::system_error& e) {
EXPECT_EQ(ESP_ERROR_PLUGIN_METADATA_NOT_FOUND, e.code().value());
EXPECT_EQ(esplugin_category(), e.code().category());
}
EXPECT_THROW(handle_->LoadPlugins({blankMasterDependentEsm}, false),
PluginNotLoadedError);
} else {
handle_->LoadPlugins({blankMasterDependentEsm}, false);
@@ -389,13 +384,8 @@ TEST_P(
loadPluginsShouldThrowIfAPluginHasAMasterThatIsNotInTheInputAndIsNotAlreadyLoadedAndGameIsMorrowindOrStarfield) {
if (GetParam() == GameType::tes3 || GetParam() == GameType::openmw ||
GetParam() == GameType::starfield) {
try {
handle_->LoadPlugins({blankMasterDependentEsm}, false);
FAIL();
} catch (const std::system_error& e) {
EXPECT_EQ(ESP_ERROR_PLUGIN_METADATA_NOT_FOUND, e.code().value());
EXPECT_EQ(esplugin_category(), e.code().category());
}
EXPECT_THROW(handle_->LoadPlugins({blankMasterDependentEsm}, false),
PluginNotLoadedError);
} else {
handle_->LoadPlugins({blankMasterDependentEsm}, false);
@@ -476,7 +466,7 @@ TEST_P(GameInterfaceTest,
TEST_P(GameInterfaceTest, sortPluginsShouldThrowIfAGivenPluginIsNotLoaded) {
std::vector<std::string> plugins{blankEsp, blankDifferentEsp};
EXPECT_THROW(handle_->SortPlugins(plugins), std::runtime_error);
EXPECT_THROW(handle_->SortPlugins(plugins), PluginNotLoadedError);
}
TEST_P(GameInterfaceTest, clearLoadedPluginsShouldClearThePluginsCache) {