idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
26#include <expected>
27#include <functional>
28#include <string>
29#include <system_error>
30#include <utility>
31
32typedef int esp_err_t;
33
34namespace idfxx {
35
43enum class errc : esp_err_t {
44 // clang-format off
45 fail = -1,
46 no_mem = 0x101,
47 invalid_arg = 0x102,
48 invalid_state = 0x103,
49 invalid_size = 0x104,
50 not_found = 0x105,
51 not_supported = 0x106,
52 timeout = 0x107,
53 invalid_response = 0x108,
54 invalid_crc = 0x109,
55 invalid_version = 0x10A,
56 invalid_mac = 0x10B,
57 not_finished = 0x10C,
58 not_allowed = 0x10D,
59 // clang-format on
60};
61
70class error_category : public std::error_category {
71public:
77 [[nodiscard]] const char* name() const noexcept override final;
78
86 [[nodiscard]] std::string message(int ec) const override final;
87};
88
96[[nodiscard]] const error_category& default_category() noexcept;
97
99[[nodiscard]] inline std::error_code make_error_code(errc e) noexcept {
100 return {std::to_underlying(e), default_category()};
101}
102
110[[nodiscard]] std::error_code make_error_code(esp_err_t e) noexcept;
111
117template<typename T>
118using result = std::expected<T, std::error_code>;
119
140template<typename E>
141 requires std::is_error_code_enum_v<E>
142[[nodiscard]] constexpr std::unexpected<std::error_code> error(E e) noexcept {
143 return std::unexpected<std::error_code>(e);
144}
145
166template<typename E>
167 requires std::is_error_condition_enum_v<E>
168[[nodiscard]] constexpr std::unexpected<std::error_code> error(E e) noexcept {
169 return std::unexpected<std::error_code>(make_error_code(e));
170}
171
182[[nodiscard]] inline std::unexpected<std::error_code> error(esp_err_t e) noexcept {
183 return std::unexpected<std::error_code>(make_error_code(e));
184}
185
205[[nodiscard]] inline std::unexpected<std::error_code> error(std::error_code ec) noexcept {
206 return std::unexpected<std::error_code>(ec);
207}
208
216[[nodiscard]] inline result<void> wrap(esp_err_t e) {
217 if (e) {
218 return error(e);
219 }
220 return {};
221}
222
223#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
236template<typename T>
237[[nodiscard]] inline T unwrap(result<T> result) {
238 return std::move(result).transform_error([](auto ec) -> std::error_code { throw std::system_error(ec); }).value();
239}
240
241// Specialization for void
242template<>
244 result.transform_error([](auto ec) -> std::error_code { throw std::system_error(ec); });
245}
246#endif
247
264template<typename T, typename Callback>
265 requires std::invocable<Callback, std::error_code> &&
266 std::same_as<std::invoke_result_t<Callback, std::error_code>, void>
267inline void abort_on_error(result<T> result, Callback&& on_error) {
268 result.transform_error([&](auto ec) {
269 // Check if the callback is valid (for types with operator bool)
270 if constexpr (requires { static_cast<bool>(on_error); }) {
271 if (!on_error) {
272 abort();
273 return ec;
274 }
275 }
276 std::forward<Callback>(on_error)(ec);
277 abort();
278 return ec;
279 });
280}
281
288template<typename T>
290 result.transform_error([](auto ec) {
291 abort();
292 return ec;
293 });
294}
295
296} // namespace idfxx
297
299namespace std {
300template<>
301struct is_error_code_enum<idfxx::errc> : true_type {};
302} // namespace std
// end of idfxx_core_error
Error category for IDFXX and ESP-IDF error codes.
Definition error.hpp:70
std::string message(int ec) const override final
Returns a human-readable message for the given error code.
const char * name() const noexcept override final
Returns the name of the error category.
int esp_err_t
Definition error.hpp:32
std::error_code make_error_code(errc e) noexcept
Creates a std::error_code from an idfxx::errc value.
Definition error.hpp:99
void abort_on_error(result< T > result, Callback &&on_error)
Aborts the program if the result contains an error.
Definition error.hpp:267
constexpr std::unexpected< std::error_code > error(E e) noexcept
Creates an unexpected error from an error code enum.
Definition error.hpp:142
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
Definition error.hpp:237
errc
IDFXX error codes.
Definition error.hpp:43
@ invalid_size
Invalid size.
@ invalid_version
Version was invalid.
@ invalid_state
Invalid state.
@ not_allowed
Operation is not allowed.
@ invalid_mac
MAC address was invalid.
@ not_found
Requested resource not found.
@ no_mem
Out of memory.
@ timeout
Operation timed out.
@ not_supported
Operation or feature not supported.
@ invalid_crc
CRC or checksum was invalid.
@ invalid_arg
Invalid argument.
@ not_finished
Operation has not fully completed.
@ fail
Generic failure.
@ invalid_response
Received response was invalid.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:118
result< void > wrap(esp_err_t e)
Wraps an esp_err_t into a result<void>.
Definition error.hpp:216
const error_category & default_category() noexcept
Returns a reference to the IDFXX error category singleton.