Skip to content

Commit

Permalink
netplay: introduce custom error category for zlib errors
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Solodovnikov <[email protected]>
  • Loading branch information
ManManson authored and past-due committed Oct 19, 2024
1 parent 656751a commit f5b3f22
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/netplay/error_categories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
# include <netdb.h>
#endif

#include <zlib.h>

std::string GenericSystemErrorCategory::message(int ev) const
{
#if defined(WZ_OS_WIN)
Expand Down Expand Up @@ -103,6 +105,23 @@ std::string GetaddrinfoErrorCategory::message(int ev) const
return gai_strerror(ev);
}

std::string ZlibErrorCategory::message(int ev) const
{
switch (ev)
{
case Z_STREAM_ERROR:
return "Z_STREAM_ERROR";
case Z_NEED_DICT:
return "Z_NEED_DICT";
case Z_DATA_ERROR:
return "Z_DATA_ERROR";
case Z_MEM_ERROR:
return "Z_MEM_ERROR";
default:
return "Unknown zlib error";
}
}

const std::error_category& generic_system_error_category()
{
static GenericSystemErrorCategory instance;
Expand All @@ -115,6 +134,12 @@ const std::error_category& getaddrinfo_error_category()
return instance;
}

const std::error_category& zlib_error_category()
{
static ZlibErrorCategory instance;
return instance;
}

std::error_code make_network_error_code(int ev)
{
return { ev, generic_system_error_category() };
Expand All @@ -124,3 +149,8 @@ std::error_code make_getaddrinfo_error_code(int ev)
{
return { ev, getaddrinfo_error_category() };
}

std::error_code make_zlib_error_code(int ev)
{
return { ev, zlib_error_category() };
}
20 changes: 20 additions & 0 deletions lib/netplay/error_categories.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,28 @@ class GetaddrinfoErrorCategory : public std::error_category
std::string message(int ev) const override;
};

/// <summary>
/// Custom error category which maps some of the error codes from zlib to
/// the appropriate error messages.
/// </summary>
class ZlibErrorCategory : public std::error_category
{
public:

constexpr ZlibErrorCategory() = default;

const char* name() const noexcept override
{
return "zlib";
}

std::string message(int ev) const override;
};

const std::error_category& generic_system_error_category();
const std::error_category& getaddrinfo_error_category();
const std::error_category& zlib_error_category();

std::error_code make_network_error_code(int ev);
std::error_code make_getaddrinfo_error_code(int ev);
std::error_code make_zlib_error_code(int ev);

0 comments on commit f5b3f22

Please sign in to comment.