From f5b3f2209c81d4740ce90e49c3abaa5af51398d8 Mon Sep 17 00:00:00 2001 From: Pavel Solodovnikov Date: Sat, 12 Oct 2024 21:09:00 +0300 Subject: [PATCH] netplay: introduce custom error category for zlib errors Signed-off-by: Pavel Solodovnikov --- lib/netplay/error_categories.cpp | 30 ++++++++++++++++++++++++++++++ lib/netplay/error_categories.h | 20 ++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/netplay/error_categories.cpp b/lib/netplay/error_categories.cpp index eb990de3c2f..5be0196bc98 100644 --- a/lib/netplay/error_categories.cpp +++ b/lib/netplay/error_categories.cpp @@ -29,6 +29,8 @@ # include #endif +#include + std::string GenericSystemErrorCategory::message(int ev) const { #if defined(WZ_OS_WIN) @@ -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; @@ -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() }; @@ -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() }; +} diff --git a/lib/netplay/error_categories.h b/lib/netplay/error_categories.h index 87a3fd20079..ddb98ed8815 100644 --- a/lib/netplay/error_categories.h +++ b/lib/netplay/error_categories.h @@ -65,8 +65,28 @@ class GetaddrinfoErrorCategory : public std::error_category std::string message(int ev) const override; }; +/// +/// Custom error category which maps some of the error codes from zlib to +/// the appropriate error messages. +/// +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);