Improve error messages
This commit is contained in:
parent
c1e776064d
commit
9dc5054fc0
1 changed files with 17 additions and 15 deletions
|
@ -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 <typename T>
|
||||
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>) {
|
||||
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<std::string_view, std::string_view>;
|
||||
|
@ -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<RequireValue, Required>) {
|
||||
throw LookupError("Required config key not found");
|
||||
throw LookupError(std::format("Required config key \"{}\" not found", key));
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue