diff --git a/Garbage/Include/Garbage/SimpleConf.hpp b/Garbage/Include/Garbage/SimpleConf.hpp index 979c2ce..636cc5f 100644 --- a/Garbage/Include/Garbage/SimpleConf.hpp +++ b/Garbage/Include/Garbage/SimpleConf.hpp @@ -52,16 +52,18 @@ namespace Garbage class ReadError : public SimpleConfError { public: - ReadError(std::string_view message) - : SimpleConfError(SimpleConfErrorKind::ReadError, message) + ReadError(std::string_view path, std::string_view message) + : SimpleConfError(SimpleConfErrorKind::ReadError, + std::format("[Path {}] {}", path, message)) {} }; class ConversionError : public SimpleConfError { public: - ConversionError(std::string_view message) - : SimpleConfError(SimpleConfErrorKind::ConversionError, message) + ConversionError(std::string_view key, std::string_view message) + : SimpleConfError(SimpleConfErrorKind::ConversionError, + std::format("[Key {}] {}", key, message)) {} }; @@ -84,7 +86,7 @@ namespace Garbage template constexpr - T Convert(std::string_view rawValue) + T Convert(std::string_view key, std::string_view rawValue) { if constexpr (std::is_same_v) { return std::string(rawValue); @@ -97,22 +99,22 @@ namespace Garbage result); if (ptr != rawValue.data() + rawValue.size()) { - throw ConversionError("Could not parse the whole value"); + throw ConversionError(key, "Could not parse the whole value"); } else if (ec == std::errc::invalid_argument) { - throw ConversionError("The value is not a valid number"); + throw ConversionError(key, "The value is not a valid number"); } else if (ec == std::errc::result_out_of_range) { - throw ConversionError("The value is too large to store in the requested type"); + throw ConversionError(key, "The value is out of range of the requested type"); } else if (ec != std::errc()) { - throw ConversionError("Unknown error"); + throw ConversionError(key, "Unknown error"); } return result; } - throw ConversionError("Can't convert the raw value to the requested type"); + throw ConversionError(key, "Can't convert the raw value to the requested type"); } using PairsType = std::unordered_map; @@ -245,20 +247,20 @@ namespace Garbage std::error_code ec; if (!fs::exists(path, ec) || ec) { - throw ReadError("The specified file does not exist"); + throw ReadError(path.string(), "The specified file does not exist"); } if (!fs::is_regular_file(path, ec) || ec) { - throw ReadError("The the specified path is not a regular file"); + throw ReadError(path.string(), "The the specified path is not a regular file"); } std::size_t fileSize = fs::file_size(path, ec); if (ec) { - throw ReadError("Could not read the file size"); + throw ReadError(path.string(), "Could not read the file size"); } std::ifstream file(path, std::ios::binary); if (!file) { - throw ReadError("Could not open the file for reading"); + throw ReadError(path.string(), "Could not open the file for reading"); } mConfContent = std::string(fileSize, '\0'); @@ -297,7 +299,7 @@ namespace Garbage } if constexpr (std::is_same_v) { - throw LookupError("Required config key not found"); + throw LookupError(std::format("Required config key \"{}\" not found", key)); } return defaultValue;