GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Error.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_NETWORKING_ERROR_HPP
5#define GREM_NETWORKING_ERROR_HPP
6
8
9#include <GREM/core/Error.hpp>
11
12#include <string> // std::string
13#include <system_error> // std::error_code, std::error_condition, std::error_category, std::is_error_code_enum, std::is_error_condition_enum
14#include <type_traits> // std::true_type
15
16namespace grem::networking {
17
23 [[nodiscard]] GREM_API(networking) static std::error_code getLastErrorCode() noexcept;
24
25 using grem::Error::Error;
26
27 explicit Error(std::error_code errorCode)
28 : Error(errorCode.message()) {}
29};
30
31} // namespace grem::networking
32
33#ifdef _WIN32
34
35namespace grem::networking {
36
40struct WSAError {
41 int value{};
42};
43
44} // namespace grem::networking
45
46template <>
47struct std::is_error_code_enum<grem::networking::WSAError> : std::true_type {};
48
49namespace grem::networking {
50
54struct WSAErrorCategory : std::error_category {
55 [[nodiscard]] GREM_API(networking) const char* name() const noexcept override;
56 [[nodiscard]] GREM_API(networking) std::string message(int condition) const override;
57
63 [[nodiscard]] static const WSAErrorCategory& getInstance() noexcept {
64 static const WSAErrorCategory category{};
65 return category;
66 }
67
68private:
69 WSAErrorCategory() noexcept = default;
70};
71
80[[nodiscard]] inline std::error_code make_error_code(WSAError error) {
81 return std::error_code{static_cast<int>(error.value), WSAErrorCategory::getInstance()};
82}
83
84} // namespace grem::networking
85
86#endif
87
88namespace grem::networking {
89
94enum class EndpointError : int { // NOLINT(performance-enum-size)
95 AGAIN = EAI_AGAIN,
96 BADFLAGS = EAI_BADFLAGS,
97 FAIL = EAI_FAIL,
98 FAMILY = EAI_FAMILY,
99 MEMORY = EAI_MEMORY,
100 NODATA = EAI_NODATA,
101 NONAME = EAI_NONAME,
102 SERVICE = EAI_SERVICE,
103 SOCKTYPE = EAI_SOCKTYPE,
104#ifdef __linux__
105 ADDRFAMILY = EAI_ADDRFAMILY,
106 SYSTEM = EAI_SYSTEM,
107 INPROGRESS = EAI_INPROGRESS,
108 CANCELED = EAI_CANCELED,
109 NOTCANCELED = EAI_NOTCANCELED,
110 ALLDONE = EAI_ALLDONE,
111 INTR = EAI_INTR,
112 IDN_ENCODE = EAI_IDN_ENCODE,
113#endif
114};
115
116} // namespace grem::networking
117
118template <>
119struct std::is_error_code_enum<grem::networking::EndpointError> : std::true_type {};
120
121namespace grem::networking {
122
127struct EndpointErrorCategory : std::error_category {
128 [[nodiscard]] GREM_API(networking) const char* name() const noexcept override;
129 [[nodiscard]] GREM_API(networking) std::string message(int condition) const override;
130
136 [[nodiscard]] static const EndpointErrorCategory& getInstance() noexcept {
137 static const EndpointErrorCategory category{};
138 return category;
139 }
140
141private:
142 EndpointErrorCategory() noexcept = default;
143};
144
153[[nodiscard]] inline std::error_code make_error_code(EndpointError error) {
154 return std::error_code{static_cast<int>(error), EndpointErrorCategory::getInstance()};
155}
156
160enum class SocketError : int { // NOLINT(performance-enum-size)
161 // The value 0 is reserved for success.
162 WAIT = 1,
166};
167
168} // namespace grem::networking
169
170template <>
171struct std::is_error_condition_enum<grem::networking::SocketError> : std::true_type {};
172
173namespace grem::networking {
174
178struct SocketErrorCategory : std::error_category {
179 [[nodiscard]] GREM_API(networking) const char* name() const noexcept override;
180 [[nodiscard]] GREM_API(networking) std::string message(int condition) const override;
181 [[nodiscard]] GREM_API(networking) bool equivalent(const std::error_code& code, int condition) const noexcept override;
182
188 [[nodiscard]] static const SocketErrorCategory& getInstance() noexcept {
189 static const SocketErrorCategory category{};
190 return category;
191 }
192
193private:
194 SocketErrorCategory() noexcept = default;
195};
196
205[[nodiscard]] inline std::error_code make_error_code(SocketError error) {
206 return std::error_code{static_cast<int>(error), SocketErrorCategory::getInstance()};
207}
208
217[[nodiscard]] inline std::error_condition make_error_condition(SocketError error) {
218 return std::error_condition{static_cast<int>(error), SocketErrorCategory::getInstance()};
219}
220
221} // namespace grem::networking
222
223#endif
Base exception type for runtime errors.
Definition Error.hpp:38
Definition Endpoint.hpp:28
std::error_code make_error_code(EndpointError error)
Create a std::error_code from an EndpointError value.
Definition Error.hpp:153
std::error_condition make_error_condition(SocketError error)
Create a std::error_condition from a SocketError value.
Definition Error.hpp:217
SocketError
Error condition type for error codes originating from the Socket API.
Definition Error.hpp:160
@ DISCONNECTED
The socket disconnected.
Definition Error.hpp:164
@ FAILED
The operation failed.
Definition Error.hpp:165
@ WAIT
Try again later.
Definition Error.hpp:162
@ PARTIAL
The data was partially transmitted.
Definition Error.hpp:163
EndpointError
Error code value type for error codes originating from the getaddrinfo() API.
Definition Error.hpp:94
@ SOCKTYPE
EAI_SOCKTYPE.
Definition Error.hpp:103
@ FAMILY
EAI_FAMILY.
Definition Error.hpp:98
@ AGAIN
EAI_AGAIN.
Definition Error.hpp:95
@ MEMORY
EAI_MEMORY.
Definition Error.hpp:99
@ BADFLAGS
EAI_BADFLAGS.
Definition Error.hpp:96
@ NONAME
EAI_NONAME.
Definition Error.hpp:101
@ FAIL
EAI_FAIL.
Definition Error.hpp:97
@ NODATA
EAI_NODATA.
Definition Error.hpp:100
@ SERVICE
EAI_SERVICE.
Definition Error.hpp:102
Definition aliases.hpp:9
Error category type for error codes originating from the getaddrinfo() API.
Definition Error.hpp:127
std::string message(int condition) const override
const char * name() const noexcept override
static const EndpointErrorCategory & getInstance() noexcept
Get the canonical global instance of this error category.
Definition Error.hpp:136
static std::error_code getLastErrorCode() noexcept
Error(std::error_code errorCode)
Definition Error.hpp:27
Error category type for error codes originating from the Socket API.
Definition Error.hpp:178
static const SocketErrorCategory & getInstance() noexcept
Get the canonical global instance of this error category.
Definition Error.hpp:188
bool equivalent(const std::error_code &code, int condition) const noexcept override
std::string message(int condition) const override
const char * name() const noexcept override