Skip to content

Commit

Permalink
Improve esplugin error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ortham committed Jun 23, 2024
1 parent c0a79c3 commit 2cae218
Showing 1 changed file with 46 additions and 67 deletions.
113 changes: 46 additions & 67 deletions src/api/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ std::vector<std::filesystem::path> FindAssociatedArchives(
}
}

void HandleEspluginError(const std::string& operation,
unsigned int returnCode) {
if (returnCode == ESP_OK) {
return;
}

auto err = "esplugin failed to " + operation +
". Error code: " + std::to_string(returnCode);

const char* e = nullptr;
esp_get_error_message(&e);
if (e == nullptr) {
err += ". Details could not be fetched.";
} else {
err += ". Details: " + std::string(e);
}

auto logger = getLogger();
if (logger) {
logger->error(err);
}

throw FileAccessError(err);
}

Plugin::Plugin(const GameType gameType,
const GameCache& gameCache,
std::filesystem::path pluginPath,
Expand All @@ -194,11 +219,7 @@ Plugin::Plugin(const GameType gameType,
Load(pluginPath, gameType, headerOnly);

auto ret = esp_plugin_is_empty(esPlugin.get(), &isEmpty_);
if (ret != ESP_OK) {
throw FileAccessError(
"Error checking if \"" + pluginPath.u8string() +
"\" is empty. esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("check if \"" + name_ + "\" is empty", ret);

archivePaths_ = FindAssociatedArchives(gameType, gameCache, pluginPath);

Expand Down Expand Up @@ -240,10 +261,7 @@ std::optional<float> Plugin::GetHeaderVersion() const {
float version = 0.0f;

const auto ret = esp_plugin_header_version(esPlugin.get(), &version);
if (ret != ESP_OK) {
throw FileAccessError(name_ +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("get the header version of \"" + name_ + "\"", ret);

if (std::isnan(version)) {
return std::nullopt;
Expand All @@ -260,10 +278,7 @@ std::vector<std::string> Plugin::GetMasters() const {
char** masters = nullptr;
size_t numMasters = 0;
const auto ret = esp_plugin_masters(esPlugin.get(), &masters, &numMasters);
if (ret != ESP_OK) {
throw FileAccessError(name_ +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("get the masters of \"" + name_ + "\"", ret);

// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
std::vector<std::string> mastersVec(masters, masters + numMasters);
Expand All @@ -279,21 +294,15 @@ std::optional<uint32_t> Plugin::GetCRC() const { return crc_; }
bool Plugin::IsMaster() const {
bool isMaster = false;
const auto ret = esp_plugin_is_master(esPlugin.get(), &isMaster);
if (ret != ESP_OK) {
throw FileAccessError(name_ +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("check if \"" + name_ + "\" is a master", ret);

return isMaster;
}

bool Plugin::IsLightPlugin() const {
bool isLightPlugin = false;
const auto ret = esp_plugin_is_light_plugin(esPlugin.get(), &isLightPlugin);
if (ret != ESP_OK) {
throw FileAccessError(name_ +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("check if \"" + name_ + "\" is a light plugin", ret);

return isLightPlugin;
}
Expand All @@ -302,10 +311,7 @@ bool Plugin::IsOverridePlugin() const {
bool isOverridePlugin = false;
const auto ret =
esp_plugin_is_override_plugin(esPlugin.get(), &isOverridePlugin);
if (ret != ESP_OK) {
throw FileAccessError(name_ +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("check if \"" + name_ + "\" is an override plugin", ret);

return isOverridePlugin;
}
Expand All @@ -314,10 +320,8 @@ bool Plugin::IsValidAsLightPlugin() const {
bool isValid = false;
const auto ret =
esp_plugin_is_valid_as_light_plugin(esPlugin.get(), &isValid);
if (ret != ESP_OK) {
throw FileAccessError(name_ +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("check if \"" + name_ + "\" is valid as a light plugin",
ret);

return isValid;
}
Expand All @@ -326,10 +330,8 @@ bool Plugin::IsValidAsOverridePlugin() const {
bool isValid = false;
const auto ret =
esp_plugin_is_valid_as_override_plugin(esPlugin.get(), &isValid);
if (ret != ESP_OK) {
throw FileAccessError(name_ +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError(
"check if \"" + name_ + "\" is valid as an override plugin", ret);

return isValid;
}
Expand All @@ -345,10 +347,9 @@ bool Plugin::DoRecordsOverlap(const PluginInterface& plugin) const {
bool doPluginsOverlap = false;
const auto ret = esp_plugin_do_records_overlap(
esPlugin.get(), otherPlugin.esPlugin.get(), &doPluginsOverlap);
if (ret != ESP_OK) {
throw FileAccessError(name_ +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("check if \"" + name_ + "\" and \"" +
otherPlugin.GetName() + "\" overlap",
ret);

return doPluginsOverlap;
} catch (std::bad_cast&) {
Expand Down Expand Up @@ -391,10 +392,7 @@ size_t Plugin::GetOverlapSize(
size_t overlapSize = 0;
const auto ret = esp_plugin_records_overlap_size(
esPlugin.get(), esPlugins.data(), esPlugins.size(), &overlapSize);
if (ret != ESP_OK) {
throw FileAccessError("Error getting overlap size for \"" + name_ +
"\". esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("get overlap size for \"" + name_ + "\"", ret);

return overlapSize;
}
Expand All @@ -403,10 +401,7 @@ size_t Plugin::GetOverrideRecordCount() const {
size_t overrideRecordCount;
const auto ret =
esp_plugin_count_override_records(esPlugin.get(), &overrideRecordCount);
if (ret != ESP_OK) {
throw FileAccessError(name_ +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("count override records in \"" + name_ + "\"", ret);

return overrideRecordCount;
}
Expand All @@ -415,11 +410,7 @@ uint32_t Plugin::GetRecordAndGroupCount() const {
uint32_t recordAndGroupCount = 0;
const auto ret =
esp_plugin_record_and_group_count(esPlugin.get(), &recordAndGroupCount);
if (ret != ESP_OK) {
throw FileAccessError("Error getting record and group count for \"" +
name_ +
"\". esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("get record and group count for \"" + name_ + "\"", ret);

return recordAndGroupCount;
}
Expand Down Expand Up @@ -484,28 +475,20 @@ void Plugin::Load(const std::filesystem::path& path,
::Plugin* plugin = nullptr;
auto ret = esp_plugin_new(
&plugin, GetEspluginGameId(gameType), path.u8string().c_str());
if (ret != ESP_OK) {
throw FileAccessError(path.u8string() +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("load plugin \"" + path.u8string() + "\"", ret);

esPlugin = std::unique_ptr<::Plugin, decltype(&esp_plugin_free)>(
plugin, esp_plugin_free);

ret = esp_plugin_parse(esPlugin.get(), headerOnly);
if (ret != ESP_OK) {
throw FileAccessError(path.u8string() +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("parse plugin \"" + path.u8string() + "\"", ret);
}

std::string Plugin::GetDescription() const {
char* description = nullptr;
const auto ret = esp_plugin_description(esPlugin.get(), &description);
if (ret != ESP_OK) {
throw FileAccessError(name_ +
" : esplugin error code: " + std::to_string(ret));
}
HandleEspluginError("read the description of \"" + name_ + "\"", ret);

if (description == nullptr) {
return "";
}
Expand Down Expand Up @@ -533,11 +516,7 @@ Plugin::GetPluginsMetadata(std::vector<const Plugin*> plugins) {
Vec_PluginMetadata* pluginsMetadata = nullptr;
const auto ret = esp_get_plugins_metadata(
esPlugins.data(), esPlugins.size(), &pluginsMetadata);
if (ret != ESP_OK) {
throw FileAccessError(
"Failed to get plugins metadata: esplugin error code: " +
std::to_string(ret));
}
HandleEspluginError("get plugins metadata", ret);

return std::unique_ptr<Vec_PluginMetadata,
decltype(&esp_plugins_metadata_free)>(
Expand Down

0 comments on commit 2cae218

Please sign in to comment.