Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
Error.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** R-Type
4** File description:
5** Error.hpp, Error class declaration
6*/
7
8/*
9** MIT License
10**
11** Copyright (c) 2025 Robin Toillon
12**
13** Permission is hereby granted, free of charge, to any person obtaining
14** a copy of this software and associated documentation files (the
15** "Software"), to deal in the Software without restriction, including
16** without limitation the rights to use, copy, modify, merge, publish,
17** distribute, sublicense, and/or sell copies of the Software, and to
18** permit persons to whom the Software is furnished to do so, subject to
19** the following conditions:
20**
21** The above copyright notice and this permission notice shall be
22** included in all copies or substantial portions of the Software.
23**
24** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31*/
32
45#ifndef RTYPE_CORE_ERROR_HPP_
46 #define RTYPE_CORE_ERROR_HPP_
47
48 #include "RType/LogLevel.hpp"
49
50 #include <cstdint>
51 #include <format>
52 #include <string>
53 #include <string_view>
54 #include <system_error>
55 #include <utility>
56
57namespace rtp
58{
68 enum class ErrorCode : std::uint16_t {
69 Success = 0,
70
71 // --- Core ---
72 Unknown = 1,
76 // --- Network ---
77 ConnectionFailed = 100,
79 Timeout,
82 // --- IO
83 FileNotFound = 200,
88 // --- ECS ---
89 ComponentMissing = 300,
92 };
93
99 constexpr std::string_view toString(ErrorCode e) noexcept;
100
106 std::error_code make_error_code(ErrorCode e) noexcept;
107}
108
109namespace std
110{
116 template <>
117 struct is_error_code_enum<rtp::ErrorCode> : true_type {};
118}
119
120template <>
121struct std::formatter<rtp::ErrorCode> : std::formatter<std::string_view> {
128 auto format(rtp::ErrorCode e, std::format_context &ctx) const
129 {
130 return std::formatter<std::string_view>::format(rtp::toString(e), ctx);
131 }
132};
133
134namespace rtp {
135
141 const std::error_category &rtype_category(void) noexcept;
142
156 class Error final {
157 public:
166 template <typename ...Args>
167 [[nodiscard]]
168 static auto failure(ErrorCode code,
169 std::format_string<Args...> fmt,
170 Args &&...args) -> Error;
171
180 template <typename ...Args>
181 [[nodiscard]]
182 static auto warning(ErrorCode code,
183 std::format_string<Args...> fmt,
184 Args &&...args) -> Error;
185
194 template <typename ...Args>
195 [[nodiscard]]
196 static auto fatal(ErrorCode code,
197 std::format_string<Args...> fmt,
198 Args &&...args) -> Error;
199
204 [[nodiscard]]
205 ErrorCode code(void) const noexcept;
206
211 [[nodiscard]]
212 log::Level severity(void) const noexcept;
213
218 [[nodiscard]]
219 std::uint8_t retryCount(void) const noexcept;
220
225 [[nodiscard]]
226 auto message(void) const noexcept
227 -> std::string_view;
228
233 std::uint8_t incrementRetryCount(void) noexcept;
234
235 private:
238 std::uint8_t _retryCount{0};
239 std::string _message;
241 private:
248 Error(ErrorCode code, log::Level severity, std::string_view msg);
249
250 };
251}
252
258template <>
259struct std::formatter<rtp::Error> : std::formatter<std::string> {
266 inline auto format(const rtp::Error &e, format_context &ctx) const
267 {
268 return std::format_to(ctx.out(), "[{}] {}: {}",
269 e.severity(),
270 rtp::toString(e.code()),
271 e.message());
272 }
273};
274
275 #include "Error.inl" /* Inline/Constexpr implementations */
276 #include "Error.tpp" /* Template implementations */
277
278#endif /* !RTYPE_CORE_ERROR_HPP_ */
Log severity levels for the logging system.
Comprehensive error object with severity and retry tracking.
Definition Error.hpp:156
ErrorCode _code
The error code.
Definition Error.hpp:236
static auto warning(ErrorCode code, std::format_string< Args... > fmt, Args &&...args) -> Error
Create a warning-level error.
std::uint8_t retryCount(void) const noexcept
Get the current retry count.
Definition Error.cpp:95
log::Level _severity
Severity level of the error.
Definition Error.hpp:237
static auto failure(ErrorCode code, std::format_string< Args... > fmt, Args &&...args) -> Error
Create a failure-level error.
log::Level severity(void) const noexcept
Get the severity level of this error.
Definition Error.cpp:90
auto message(void) const noexcept -> std::string_view
Get the error message.
Definition Error.cpp:100
std::uint8_t _retryCount
Number of retry attempts.
Definition Error.hpp:238
std::string _message
Formatted error message.
Definition Error.hpp:239
ErrorCode code(void) const noexcept
Get the error code.
Definition Error.cpp:85
std::uint8_t incrementRetryCount(void) noexcept
Increment the retry counter.
Definition Error.cpp:106
static auto fatal(ErrorCode code, std::format_string< Args... > fmt, Args &&...args) -> Error
Create a fatal-level error.
Level
Severity levels for log messages.
Definition LogLevel.hpp:57
ErrorCode
Enumeration of all possible error codes in the RType engine.
Definition Error.hpp:68
@ FileNotFound
Requested file does not exist.
@ InvalidParameter
Invalid function parameter provided.
@ LibraryLoadFailed
Failed to load dynamic library.
@ InternalRuntimeError
Internal runtime error.
@ ComponentMissing
Requested component not found on entity.
@ Unknown
Unknown or unspecified error.
@ EntityInvalid
Entity identifier is invalid or stale.
@ ConnectionFailed
Failed to establish network connection.
@ RegistryFull
Entity registry has reached maximum capacity.
@ SymbolNotFound
Symbol not found in dynamic library.
@ Timeout
Network operation timed out.
@ InvalidFormat
Invalid file or data format.
@ Disconnected
Connection was disconnected.
@ PayloadError
Error in network payload data.
std::error_code make_error_code(ErrorCode e) noexcept
Create a std::error_code from an ErrorCode.
Definition Error.cpp:80
const std::error_category & rtype_category(void) noexcept
Get the RType error category for std::error_code integration.
Definition Error.cpp:74
constexpr std::string_view toString(ErrorCode e) noexcept
Convert an ErrorCode to its string representation.
STL namespace.
auto format(rtp::ErrorCode e, std::format_context &ctx) const
Format an ErrorCode enum value.
Definition Error.hpp:128
auto format(const rtp::Error &e, format_context &ctx) const
Format an Error object.
Definition Error.hpp:266