refactor: subdividing IOException.

This commit is contained in:
2025-08-20 14:43:52 +08:00
parent 93c79d850a
commit 15df2fab99
2 changed files with 35 additions and 5 deletions

View File

@@ -17,6 +17,30 @@ public:
constexpr std::string category() const { return "exception.io"; }
};
class OutOfBoundException : public IOException {
public:
explicit OutOfBoundException(size_t op, size_t buffer_size)
: IOException("An attempt was made to move the read pointer outside the "
"buffer bounds.") {
add_context_v_hex("op", op);
add_context_v_hex("buffer_size", buffer_size);
}
constexpr std::string category() const { return "exception.io.outofbound"; }
};
class CorruptedVarIntException : public IOException {
public:
explicit CorruptedVarIntException(std::string_view msg, size_t position)
: IOException("{}", msg) {
add_context_v_hex("position", position);
}
constexpr std::string category() const {
return "exception.io.corruptedvarint";
}
};
class UnableToOpenException : public UnixException {
public:
explicit UnableToOpenException(const fs::path& path) {

View File

@@ -17,7 +17,7 @@ public:
template <TriviallyCopyable T>
constexpr T get() {
if (m_position + sizeof(T) > m_buffer.size()) {
throw IOException("Read attempt is out of buffer bounds.");
throw OutOfBoundException(sizeof(T), m_buffer.size());
}
T value;
@@ -41,10 +41,16 @@ public:
while (true) {
if (m_position >= m_buffer.size()) {
throw IOException("Incomplete VarInt at end of buffer.");
throw CorruptedVarIntException(
"Incomplete VarInt at end of buffer.",
m_position
);
}
if (shift >= max_shift) {
throw IOException("VarInt is too long for the requested type.");
throw CorruptedVarIntException(
"VarInt is too long for the requested type.",
m_position
);
}
auto byte = static_cast<std::byte>(m_buffer[m_position++]);
@@ -87,7 +93,7 @@ public:
constexpr void seek(size_t pos) {
if (pos > m_buffer.size()) {
throw IOException("Seek position is out of buffer bounds.");
throw OutOfBoundException(pos, m_buffer.size());
}
m_position = pos;
}
@@ -98,7 +104,7 @@ public:
constexpr size_t position() const { return m_position; }
std::string const& data() const { return m_buffer; }
std::string& underlying() { return m_buffer; }
private:
std::string m_buffer;