Skip to content

Commit

Permalink
Fixed exception messages being lost in memory 😒
Browse files Browse the repository at this point in the history
  • Loading branch information
adya committed Apr 12, 2024
1 parent 950fbcb commit ed857b8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 40 deletions.
1 change: 0 additions & 1 deletion SPID/include/LinkedDistribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace LinkedDistribution
{

/// <summary>
/// Scope of a linked form determines which distributions can trigger linked forms.
/// </summary>
Expand Down
34 changes: 9 additions & 25 deletions SPID/include/LookupConfigs.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,59 +91,43 @@ namespace Distribution::INI
const std::string key;

UnsupportedFormTypeException(const std::string& key) :
std::exception(fmt::format("Unsupported form type {}"sv, key).c_str()),
key(key)
{}

const char* what() const noexcept override
{
return fmt::format("Unsupported Form type {}"sv, key).c_str();
}
};

struct InvalidIndexOrCountException : std::exception
{
const std::string entry;

InvalidIndexOrCountException(const std::string& entry) :
std::exception(fmt::format("Invalid index or count {}"sv, entry).c_str()),
entry(entry)
{}

const char* what() const noexcept override
{
return fmt::format("Invalid index or count {}"sv, entry).c_str();
}
};

struct InvalidChanceException : std::exception
{
const std::string entry;

InvalidChanceException(const std::string& entry) :
std::exception(fmt::format("Invalid chance {}"sv, entry).c_str()),
entry(entry)
{}

const char* what() const noexcept override
{
return fmt::format("Invalid chance {}"sv, entry).c_str();
}
};

struct MissingDistributableFormException : std::exception
{
const char* what() const noexcept override
{
return "Missing distributable form";
}
MissingDistributableFormException() :
std::exception("Missing distributable form")
{}
};

struct MissingComponentParserException : std::exception
{
std::string component;

const char* what() const noexcept override
{
return "Missing component parser";
}
MissingComponentParserException() :
std::exception("Missing component parser")
{}
};
}

Expand Down
20 changes: 8 additions & 12 deletions SPID/include/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ struct NotEnoughComponentsException: std::exception
const size_t entrySectionsCount;

NotEnoughComponentsException(size_t componentParsersCount, size_t entrySectionsCount) :
std::exception(fmt::format("Too many sections. Expected at most {}, but got {}"sv, componentParsersCount, entrySectionsCount).c_str()) ,
componentParsersCount(componentParsersCount),
entrySectionsCount(entrySectionsCount)
{}

const char* what() const noexcept override
{
return fmt::format("Too many sections. Expected at most {}, but got {}"sv, componentParsersCount, entrySectionsCount).c_str();
}
};

/// <summary>
Expand All @@ -56,9 +52,15 @@ struct NotEnoughComponentsException: std::exception
template <typename Data, key_component_parser<Data> KeyComponentParser, component_parser<Data>... ComponentParsers>
std::optional<Data> Parse(const std::string& key, const std::string& entry)
{
Data data{};

if (!KeyComponentParser()(key, data)) {
return std::nullopt;
}

constexpr const size_t numberOfComponents = sizeof...(ComponentParsers);

const auto rawSections = string::split(entry, "|");
const auto rawSections = string::split(entry, "|");
const size_t numberOfSections = rawSections.size();

if (numberOfSections > numberOfComponents) {
Expand All @@ -75,12 +77,6 @@ std::optional<Data> Parse(const std::string& key, const std::string& entry)
sections[i] = "";
}

Data data{};

if (!KeyComponentParser()(key, data)) {
return std::nullopt;
}

detail::parse_each<Data, ComponentParsers...>(data, sections, std::index_sequence_for<ComponentParsers...>());

return data;
Expand Down
6 changes: 4 additions & 2 deletions SPID/src/LinkedDistribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ namespace LinkedDistribution
return true;
}
} catch (const Exception::MissingComponentParserException& e) {
logger::warn("SKIPPED: Linked Form must have a form and at least one Parent Form: {} = {}"sv, key, value);
logger::warn("'{} = {}'"sv, key, value);
logger::warn("\t\tSKIPPED: Linked Form must have a form and at least one Parent Form"sv);
} catch (const std::exception& e) {
logger::warn("SKIPPED {}", e.what());
logger::warn("'{} = {}'"sv, key, value);
logger::warn("\t\tSKIPPED: {}"sv, e.what());
}
return false;
}
Expand Down

0 comments on commit ed857b8

Please sign in to comment.