Skip to content

Commit

Permalink
netplay: introduce custom error category for getaddrinfo() error codes
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 e2718f9 commit 656751a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/netplay/error_categories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,29 @@ std::error_condition GenericSystemErrorCategory::default_error_condition(int ev)
return std::system_category().default_error_condition(ev);
}

std::string GetaddrinfoErrorCategory::message(int ev) const
{
return gai_strerror(ev);
}

const std::error_category& generic_system_error_category()
{
static GenericSystemErrorCategory instance;
return instance;
}

const std::error_category& getaddrinfo_error_category()
{
static GetaddrinfoErrorCategory instance;
return instance;
}

std::error_code make_network_error_code(int ev)
{
return { ev, generic_system_error_category() };
}

std::error_code make_getaddrinfo_error_code(int ev)
{
return { ev, getaddrinfo_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 @@ -47,6 +47,26 @@ class GenericSystemErrorCategory : public std::error_category
std::error_condition default_error_condition(int ev) const noexcept override;
};

/// <summary>
/// Custom error category which maps error codes from `getaddrinfo()` function to
/// the appropriate error messages.
/// </summary>
class GetaddrinfoErrorCategory : public std::error_category
{
public:

constexpr GetaddrinfoErrorCategory() = default;

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

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

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

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

0 comments on commit 656751a

Please sign in to comment.