Skip to content

Commit

Permalink
Improved logging of type errors during parsing and lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
adya committed Mar 30, 2024
1 parent ef8594d commit 7a31302
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 32 deletions.
21 changes: 11 additions & 10 deletions SPID/include/FormData.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ namespace Forms
/// </summary>
struct MismatchingFormTypeException : std::exception
{
const RE::FormType expectedFformType;
const RE::FormType expectedFormType;
const RE::FormType actualFormType;
const FormOrEditorID formOrEditorID;
const std::string path;

MismatchingFormTypeException(RE::FormType expectedFformType, RE::FormType actualFormType, const FormOrEditorID& formOrEditorID, const std::string& path) :
expectedFformType(expectedFformType),
MismatchingFormTypeException(RE::FormType expectedFormType, RE::FormType actualFormType, const FormOrEditorID& formOrEditorID, const std::string& path) :
expectedFormType(expectedFormType),
actualFormType(actualFormType),
formOrEditorID(formOrEditorID),
path(path)
Expand Down Expand Up @@ -216,8 +216,10 @@ namespace Forms
}

form = as_form(anyForm);
if (!form) {
throw MismatchingFormTypeException(anyForm->GetFormType(), Form::FORMTYPE, FormModPair{ *formID, modName }, path);
if (!form || anyForm->GetFormType() != Form::FORMTYPE) {
// Ideally, we'd want to throw separate exception for unsupported form type,
// so that attempting to distribute, for example, CELL would properly report such error.
throw MismatchingFormTypeException(Form::FORMTYPE, anyForm->GetFormType(), FormModPair{ *formID, modName }, path);
}

if constexpr (std::is_same_v<Form, RE::BGSKeyword>) {
Expand All @@ -232,7 +234,6 @@ namespace Forms
if (editorID.empty()) {
throw MalformedEditorIDException(path);
}
//
if constexpr (std::is_same_v<Form, RE::BGSKeyword>) {
form = find_or_create_keyword(editorID);
} else {
Expand Down Expand Up @@ -322,10 +323,10 @@ namespace Forms
std::visit(overload{
[&](const FormModPair& formMod) {
auto& [formID, modName] = formMod;
buffered_logger::error("\t\t[{}] Filter[0x{:X}] ({}) FAIL - mismatching form type (expected: {}, actual: {})", e.path, *formID, modName.value_or(""), RE::FormTypeToString(e.expectedFformType), RE::FormTypeToString(e.actualFormType));
buffered_logger::error("\t\t[{}] Filter[0x{:X}] ({}) FAIL - mismatching form type (expected: {}, actual: {})", e.path, *formID, modName.value_or(""), RE::FormTypeToString(e.expectedFormType), RE::FormTypeToString(e.actualFormType));
},
[&](std::string editorID) {
buffered_logger::error("\t\t[{}] Filter ({}) FAIL - mismatching form type (expected: {}, actual: {})", e.path, editorID, RE::FormTypeToString(e.expectedFformType), RE::FormTypeToString(e.actualFormType));
buffered_logger::error("\t\t[{}] Filter ({}) FAIL - mismatching form type (expected: {}, actual: {})", e.path, editorID, RE::FormTypeToString(e.expectedFormType), RE::FormTypeToString(e.actualFormType));
} },
e.formOrEditorID);
} catch (const InvalidFormTypeException& e) {
Expand Down Expand Up @@ -631,10 +632,10 @@ void Forms::LookupGenericForm(RE::TESDataHandler* const dataHandler, INI::Data&
std::visit(overload{
[&](const FormModPair& formMod) {
auto& [formID, modName] = formMod;
buffered_logger::error("\t\t[{}] [0x{:X}] ({}) FAIL - mismatching form type (expected: {}, actual: {})", e.path, *formID, modName.value_or(""), RE::FormTypeToString(e.expectedFformType), RE::FormTypeToString(e.actualFormType));
buffered_logger::error("\t\t[{}] [0x{:X}] ({}) FAIL - mismatching form type (expected: {}, actual: {})", e.path, *formID, modName.value_or(""), RE::FormTypeToString(e.expectedFormType), RE::FormTypeToString(e.actualFormType));
},
[&](std::string editorID) {
buffered_logger::error("\t\t[{}] ({}) FAIL - mismatching form type (expected: {}, actual: {})", e.path, editorID, RE::FormTypeToString(e.expectedFformType), RE::FormTypeToString(e.actualFormType));
buffered_logger::error("\t\t[{}] ({}) FAIL - mismatching form type (expected: {}, actual: {})", e.path, editorID, RE::FormTypeToString(e.expectedFormType), RE::FormTypeToString(e.actualFormType));
} },
e.formOrEditorID);
} catch (const Lookup::InvalidFormTypeException& e) {
Expand Down
4 changes: 2 additions & 2 deletions SPID/include/LinkedDistribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ namespace LinkedDistribution
std::visit(overload{
[&](const FormModPair& formMod) {
auto& [formID, modName] = formMod;
buffered_logger::error("\t\t[{}] LinkedForm [0x{:X}] ({}) SKIP - mismatching form type (expected: {}, actual: {})", e.path, *formID, modName.value_or(""), RE::FormTypeToString(e.expectedFformType), RE::FormTypeToString(e.actualFormType));
buffered_logger::error("\t\t[{}] LinkedForm [0x{:X}] ({}) SKIP - mismatching form type (expected: {}, actual: {})", e.path, *formID, modName.value_or(""), RE::FormTypeToString(e.expectedFormType), RE::FormTypeToString(e.actualFormType));
},
[&](std::string editorID) {
buffered_logger::error("\t\t[{}] LinkedForm ({}) SKIP - mismatching form type (expected: {}, actual: {})", e.path, editorID, RE::FormTypeToString(e.expectedFformType), RE::FormTypeToString(e.actualFormType));
buffered_logger::error("\t\t[{}] LinkedForm ({}) SKIP - mismatching form type (expected: {}, actual: {})", e.path, editorID, RE::FormTypeToString(e.expectedFormType), RE::FormTypeToString(e.actualFormType));
} },
e.formOrEditorID);
} catch (const InvalidFormTypeException& e) {
Expand Down
21 changes: 5 additions & 16 deletions SPID/include/LookupConfigs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace RECORD

namespace detail
{
inline static constexpr std::array add{
inline static constexpr std::array names{
"Form"sv,
"Spell"sv,
"Perk"sv,
Expand All @@ -46,25 +46,14 @@ namespace RECORD

inline constexpr std::string_view GetTypeName(const TYPE aType)
{
return detail::add.at(aType);
return detail::names.at(aType);
}

inline constexpr TYPE GetType(const std::string& aType)
template <typename T>
constexpr TYPE GetType(const T& aType)
{
using namespace detail;
return static_cast<TYPE>(std::distance(add.begin(), std::find(add.begin(), add.end(), aType)));
}

inline constexpr TYPE GetType(const std::string_view& aType)
{
using namespace detail;
return static_cast<TYPE>(std::distance(add.begin(), std::find(add.begin(), add.end(), aType)));
}

inline constexpr TYPE GetType(const char* aType)
{
using namespace detail;
return static_cast<TYPE>(std::distance(add.begin(), std::find(add.begin(), add.end(), aType)));
return static_cast<TYPE>(std::distance(names.begin(), std::find(names.begin(), names.end(), aType)));
}
}

Expand Down
6 changes: 4 additions & 2 deletions SPID/src/LinkedDistribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace LinkedDistribution
std::string rawType = a_key.substr(6);
auto type = RECORD::GetType(rawType);
if (type == RECORD::kTotal) {
logger::warn("IGNORED: Invalid Linked Form type: {}"sv, rawType);
logger::warn("IGNORED: Unsupported Linked Form type: {}"sv, rawType);
return true;
}

Expand Down Expand Up @@ -136,13 +136,15 @@ namespace LinkedDistribution
if (std::holds_alternative<RandomCount>(idxOrCount)) {
auto& count = std::get<RandomCount>(idxOrCount);
if (!count.IsExact()) {
logger::warn("Inferred Form is a Package, but specifies a random count instead of index. Min value ({}) of the range will be used as an index.", count.min);
logger::warn("\t[{}] Inferred Form is a Package, but specifies a random count instead of index. Min value ({}) of the range will be used as an index.", path, count.min);
}
packageIndex = count.min;
} else {
packageIndex = std::get<Index>(idxOrCount);
}
packages.Link(form, parentForms, packageIndex, chance, path);
} else {
logger::warn("\t[{}] Unsupported Form type: {}", path, RE::FormTypeToString(type));
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion SPID/src/LookupConfigs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ namespace INI
}

auto type = RECORD::GetType(key.pItem);
if (type == RECORD::kTotal) {
logger::warn("\t\tUnsupported Form type: {}"sv, key.pItem);
continue;
}
auto [data, sanitized_str] = detail::parse_ini(type, entry, truncatedPath);

configs[type].emplace_back(data);
Expand All @@ -335,7 +339,7 @@ namespace INI
oldFormatMap.emplace(key, std::make_pair(entry, *sanitized_str));
}
} catch (...) {
logger::warn("\t\tFailed to parse entry [{} = {}]", key.pItem, entry);
logger::warn("\t\tFailed to parse entry [{} = {}]"sv, key.pItem, entry);
shouldLogErrors = true;
}
}
Expand Down
4 changes: 3 additions & 1 deletion SPID/src/LookupForms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ bool LookupDistributables(RE::TESDataHandler* const dataHandler)
if (std::holds_alternative<RandomCount>(idxOrCount)) {
auto& count = std::get<RandomCount>(idxOrCount);
if (!count.IsExact()) {
logger::warn("Inferred Form is a Package, but specifies a random count instead of index. Min value ({}) of the range will be used as an index.", count.min);
logger::warn("\t[{}] Inferred Form is a Package, but specifies a random count instead of index. Min value ({}) of the range will be used as an index.", path, count.min);
}
packageIndex = count.min;
} else {
packageIndex = std::get<Index>(idxOrCount);
}
packages.EmplaceForm(isValid, form, packageIndex, filters, path);
} else {
logger::warn("\t[{}] Unsupported Form type: {}", path, RE::FormTypeToString(type));
}
}
});
Expand Down

0 comments on commit 7a31302

Please sign in to comment.