mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user