Improve error messages

This commit is contained in:
TennesseeTrash 2025-06-09 01:42:30 +02:00
parent c1e776064d
commit 9dc5054fc0

View file

@ -52,16 +52,18 @@ namespace Garbage
class ReadError : public SimpleConfError class ReadError : public SimpleConfError
{ {
public: public:
ReadError(std::string_view message) ReadError(std::string_view path, std::string_view message)
: SimpleConfError(SimpleConfErrorKind::ReadError, message) : SimpleConfError(SimpleConfErrorKind::ReadError,
std::format("[Path {}] {}", path, message))
{} {}
}; };
class ConversionError : public SimpleConfError class ConversionError : public SimpleConfError
{ {
public: public:
ConversionError(std::string_view message) ConversionError(std::string_view key, std::string_view message)
: SimpleConfError(SimpleConfErrorKind::ConversionError, message) : SimpleConfError(SimpleConfErrorKind::ConversionError,
std::format("[Key {}] {}", key, message))
{} {}
}; };
@ -84,7 +86,7 @@ namespace Garbage
template <typename T> template <typename T>
constexpr constexpr
T Convert(std::string_view rawValue) T Convert(std::string_view key, std::string_view rawValue)
{ {
if constexpr (std::is_same_v<T, std::string>) { if constexpr (std::is_same_v<T, std::string>) {
return std::string(rawValue); return std::string(rawValue);
@ -97,22 +99,22 @@ namespace Garbage
result); result);
if (ptr != rawValue.data() + rawValue.size()) { 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) { 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) { 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()) { else if (ec != std::errc()) {
throw ConversionError("Unknown error"); throw ConversionError(key, "Unknown error");
} }
return result; 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<std::string_view, std::string_view>; using PairsType = std::unordered_map<std::string_view, std::string_view>;
@ -245,20 +247,20 @@ namespace Garbage
std::error_code ec; std::error_code ec;
if (!fs::exists(path, ec) || 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) { 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); std::size_t fileSize = fs::file_size(path, ec);
if (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); std::ifstream file(path, std::ios::binary);
if (!file) { 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'); mConfContent = std::string(fileSize, '\0');
@ -297,7 +299,7 @@ namespace Garbage
} }
if constexpr (std::is_same_v<RequireValue, Required>) { if constexpr (std::is_same_v<RequireValue, Required>) {
throw LookupError("Required config key not found"); throw LookupError(std::format("Required config key \"{}\" not found", key));
} }
return defaultValue; return defaultValue;