Skip to content

Commit

Permalink
Expose loot-condition-interpreter error codes to callers
Browse files Browse the repository at this point in the history
Make ConditionSyntaxError extend from system_error instead of directly from runtime_error, so that callers can use the exception's code to distinguish between different causes.
  • Loading branch information
Ortham committed Aug 23, 2024
1 parent 6f47b0c commit 20decc4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions include/loot/exception/condition_syntax_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ namespace loot {
* @brief An exception class thrown if invalid syntax is encountered when
* parsing a metadata condition.
*/
class ConditionSyntaxError : public std::runtime_error {
class ConditionSyntaxError : public std::system_error {
public:
using std::runtime_error::runtime_error;
using std::system_error::system_error;
};
}

Expand Down
8 changes: 8 additions & 0 deletions include/loot/exception/error_categories.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ LOOT_API const std::error_category& esplugin_category();
* derived from std::error_category.
*/
LOOT_API const std::error_category& libloadorder_category();

/**
* @brief Get the error category that can be used to identify system_error
* exceptions that are due to loot condition interpreter errors.
* @returns A reference to the static object of unspecified runtime type,
* derived from std::error_category.
*/
LOOT_API const std::error_category& loot_condition_interpreter_category();
}

#endif
15 changes: 15 additions & 0 deletions src/api/error_categories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ class libloadorder_category : public std::error_category {
return code.category().name() == name();
}
};

class loot_condition_interpreter_category : public std::error_category {
const char* name() const noexcept override { return "loot condition interpreter"; }

std::string message(int) const override { return "loot condition interpreter 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() {
Expand All @@ -58,4 +68,9 @@ LOOT_API const std::error_category& libloadorder_category() {
static detail::libloadorder_category instance;
return instance;
}

LOOT_API const std::error_category& loot_condition_interpreter_category() {
static detail::loot_condition_interpreter_category instance;
return instance;
}
}
3 changes: 2 additions & 1 deletion src/api/metadata/condition_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "api/helpers/crc.h"
#include "api/helpers/logging.h"
#include "loot/exception/condition_syntax_error.h"
#include "loot/exception/error_categories.h"

namespace loot {
void HandleError(const std::string operation, int returnCode) {
Expand All @@ -50,7 +51,7 @@ void HandleError(const std::string operation, int returnCode) {
logger->error(err);
}

throw ConditionSyntaxError(err);
throw ConditionSyntaxError(returnCode, loot_condition_interpreter_category(), err);
}

int mapGameType(GameType gameType) {
Expand Down

0 comments on commit 20decc4

Please sign in to comment.