Skip to content

Commit

Permalink
Error codes organized and documented.
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalz800 committed Jan 12, 2022
1 parent 0381c41 commit d52020d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ int main()
}
```

### Error Codes
All of the above methods, use the following error codes internally and can be checked using the comparison
operator from return value based, or by examining the internal error code of `std::system_error` or `zpp::throwing` depending
which one you used:
1. `std::errc::result_out_of_range` - attempting to write or read from a too short buffer.
2. `std::errc::no_buffer_space` - growing buffer would grow beyond the allocation limits or overflow.
3. `std::errc::value_too_large` - varint (variable length integer) encoding is beyond the representation limits.
4. `std::errc::message_size` - message size is beyond the user defined allocation limits.
5. `std::errc::not_supported` - attempt to call an RPC that is not listed as supported.
6. `std::errc::bad_message` - attempt to read a variant of unrecognized type.
7. `std::errc::invalid_argument` - attempting to serialize null pointer or a value-less variant.
8. `std::errc::protocol_error` - attempt to deserialize an invalid protocol message

Serializing Non-Aggregates
--------------------------
For most non-aggregate types, enabling serialization is a one liner. Here is an example of a non-aggregate
Expand Down
2 changes: 1 addition & 1 deletion test/src/test_alloc_limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TEST(test_alloc_limit, output)
static_assert(decltype(out)::allocation_limit == limit);

out(1,2,3,4).or_throw();
EXPECT_EQ((out(std::array<int, 50>{})), std::errc::message_size);
EXPECT_EQ((out(std::array<int, 50>{})), std::errc::no_buffer_space);
}

TEST(test_alloc_limit, input)
Expand Down
20 changes: 10 additions & 10 deletions zpp_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ constexpr auto ZPP_BITS_INLINE serialize(
auto data_size = data.size();
if (data_size < max_size) [[unlikely]] {
if (data_size < varint_size(value)) [[unlikely]] {
return errc{std::errc::value_too_large};
return errc{std::errc::result_out_of_range};
}
}
}
Expand Down Expand Up @@ -1479,7 +1479,7 @@ constexpr auto ZPP_BITS_INLINE serialize(
1 + std::distance(data.data(), &byte_value);
return errc{};
}
return errc{std::errc::value_too_large};
return errc{std::errc::result_out_of_range};
} else {
// clang-format off
auto p = data.data();
Expand Down Expand Up @@ -1643,12 +1643,12 @@ class basic_out
if (additional_size > size - m_position) [[unlikely]] {
auto new_size = (additional_size + size) * 3 / 2;
if (new_size < size) [[unlikely]] {
return std::errc::value_too_large;
return std::errc::no_buffer_space;
}
if constexpr (allocation_limit !=
std::numeric_limits<std::size_t>::max()) {
if (new_size > allocation_limit) [[unlikely]] {
return std::errc::message_size;
return std::errc::no_buffer_space;
}
}
m_data.resize(new_size);
Expand Down Expand Up @@ -1936,7 +1936,7 @@ class basic_out
}
} else if (size_in_bytes > m_data.size() - m_position)
[[unlikely]] {
return std::errc::value_too_large;
return std::errc::result_out_of_range;
}

auto data = m_data.data() + m_position;
Expand Down Expand Up @@ -1993,7 +1993,7 @@ class basic_out
} else if (move_ahead_count >
m_data.size() - current_position)
[[unlikely]] {
return std::errc::value_too_large;
return std::errc::result_out_of_range;
}
auto data = m_data.data();
auto message_start =
Expand Down Expand Up @@ -2370,13 +2370,13 @@ class in
std::same_as<unsigned char,
value_type>)) {
if (size > m_data.size() - m_position) [[unlikely]] {
return std::errc::value_too_large;
return std::errc::result_out_of_range;
}
container = {m_data.data() + m_position, size};
m_position += size;
} else {
if (size > container.size()) [[unlikely]] {
return std::errc::value_too_large;
return std::errc::result_out_of_range;
}
container = {container.data(), size};
}
Expand Down Expand Up @@ -2406,7 +2406,7 @@ class in
}) {
if (type::extent > m_data.size() - m_position)
[[unlikely]] {
return std::errc::value_too_large;
return std::errc::result_out_of_range;
}
container = {m_data.data() + m_position, type::extent};
m_position += type::extent;
Expand Down Expand Up @@ -2686,7 +2686,7 @@ class in

if (size_in_bytes > m_data.size() - m_position)
[[unlikely]] {
return std::errc::value_too_large;
return std::errc::result_out_of_range;
}

auto data = m_data.data() + m_position;
Expand Down

0 comments on commit d52020d

Please sign in to comment.