cpp_error_utils 1.0.0
C++ error handling utilities
Loading...
Searching...
No Matches
error_utils.hpp File Reference

Error handling utilities for C++ applications. More...

Go to the source code of this file.

Classes

class  error_utils::Error
 A wrapper class for system error codes with additional context. More...
 

Namespaces

namespace  error_utils
 Contains utilities for error handling and classification.
 

Macros

#define CPP_ERROR_UTILS_VERSION_MAJOR   1
 cpp_error_utils major version number
 
#define CPP_ERROR_UTILS_VERSION_MINOR   0
 Minor version number.
 
#define CPP_ERROR_UTILS_VERSION_PATCH   0
 Library patch number.
 

Typedefs

template<typename T = void>
using error_utils::Result = std::expected<T, Error>
 A specialization of std::expected for the Error type.
 
using error_utils::VoidResult = Result<>
 Result type for void (std::expected<void, Error>)
 
using error_utils::StringResult = Result<std::string>
 Result type for strings (std::expected<std::string, Error>)
 
using error_utils::IntResult = Result<int>
 Result type for integers (std::expected<int, Error>)
 
using error_utils::BoolResult = Result<bool>
 Result type for booleans (std::expected<bool, Error>)
 
template<typename T = void>
using Result
 A specialization of std::expected for the Error type.
 
using VoidResult
 Result type for void (std::expected<void, Error>)
 
using StringResult
 Result type for strings (std::expected<std::string, Error>)
 
using IntResult
 Result type for integers (std::expected<int, Error>)
 
using BoolResult
 Result type for booleans (std::expected<bool, Error>)
 

Enumerations

enum class  error_utils::ExtraError {
  error_utils::invalid_argument = 1 , error_utils::length_error , error_utils::logic_error , error_utils::value_too_small ,
  error_utils::nonexistent_local_time , error_utils::ambiguous_local_time , error_utils::format_error , error_utils::runtime_error ,
  error_utils::bad_alloc , error_utils::bad_typeid , error_utils::bad_cast , error_utils::bad_optional_access ,
  error_utils::bad_expected_access , error_utils::bad_variant_access , error_utils::bad_weak_ptr , error_utils::bad_function_call ,
  error_utils::bad_exception , error_utils::exception , error_utils::unknown_exception , error_utils::unknown_error
}
 Represents specific error codes for exception handling and error classification. More...
 
enum class  error_utils::ExtraErrorCondition {
  error_utils::logic_error = 1 , error_utils::runtime_error , error_utils::resource_error , error_utils::access_error ,
  error_utils::other_error
}
 Represents categories of error conditions for error handling and classification. More...
 
enum class  ExtraError
 Represents specific error codes for exception handling and error classification. More...
 
enum class  ExtraErrorCondition
 Represents categories of error conditions for error handling and classification. More...
 

Functions

constexpr std::error_code error_utils::make_error_code (ExtraError e)
 Create an error code from an ExtraError enum value.
 
constexpr std::error_condition error_utils::make_error_condition (ExtraErrorCondition e)
 Create an error condition from an ExtraErrorCondition enum value.
 
template<typename T, typename E, typename Ctx = std::string_view>
requires detail::convertible_to_error_code<E>
constexpr Result< T > error_utils::make_error (E &&code, Ctx &&context={})
 Create an error result of the specified type.
 
template<typename T>
constexpr Result< T > error_utils::make_error (const std::error_code &code, const std::string_view context={})
 Create an error result of the specified type from a std::error_code.
 
template<typename T>
constexpr Result< T > error_utils::make_error (const std::regex_constants::error_type code, std::string_view context={})
 Create an error result of the specified type from a std::regex_constants::error_type.
 
std::error_code error_utils::last_error () noexcept
 Retrieve the last system error code and reset errno.
 
template<typename T>
Result< T > error_utils::make_error_from_errno (const std::string_view context={})
 Create an error result from the current errno value.
 
template<typename Func, typename R = std::invoke_result_t<Func>>
auto error_utils::with_errno (Func &&func, const std::string_view error_context={}) -> Result< R >
 Execute a function that may set errno, capturing the result and any error.
 
template<typename Func>
requires std::is_nothrow_invocable_v<Func>
IntResult error_utils::invoke_with_syscall_api (Func &&func, const std::string_view error_context={}) noexcept
 Execute a function that may set errno, capturing the result and any error.
 
template<typename Func, typename R = std::invoke_result_t<Func>>
constexpr auto error_utils::try_catch (Func &&func, std::string_view context={}) -> Result< R >
 Execute a function and catch common exceptions, converting them to errors.
 
template<typename T>
constexpr Result< T > error_utils::first_of (std::initializer_list< Result< T > > results)
 Return first success result from multiple alternatives.
 

Detailed Description

Error handling utilities for C++ applications.

This module provides various utilities for error handling, including error codes and conditions that can be used throughout C++ applications.

Note
This module is designed to be extensible for future error handling needs.

Macro Definition Documentation

◆ CPP_ERROR_UTILS_VERSION_MAJOR

#define CPP_ERROR_UTILS_VERSION_MAJOR   1

cpp_error_utils major version number

◆ CPP_ERROR_UTILS_VERSION_MINOR

#define CPP_ERROR_UTILS_VERSION_MINOR   0

Minor version number.

◆ CPP_ERROR_UTILS_VERSION_PATCH

#define CPP_ERROR_UTILS_VERSION_PATCH   0

Library patch number.

Typedef Documentation

◆ BoolResult

Result type for booleans (std::expected<bool, Error>)

◆ IntResult

Result type for integers (std::expected<int, Error>)

◆ Result

template<typename T = void>
using error_utils::Result

A specialization of std::expected for the Error type.

Template Parameters
TThe type of the expected value. Defaults to void

◆ StringResult

Result type for strings (std::expected<std::string, Error>)

◆ VoidResult

Result type for void (std::expected<void, Error>)

Enumeration Type Documentation

◆ ExtraError

enum class error_utils::ExtraError
strong

Represents specific error codes for exception handling and error classification.

This scoped enumeration defines detailed error codes that map to specific C++ standard library exceptions, providing a unified way to handle various error types.

It is designed to work with the standard error handling facilities and integrates with std::error_code and std::error_condition

◆ ExtraErrorCondition

Represents categories of error conditions for error handling and classification.

This enumeration defines broad categories of errors that can occur in the system, which are used to group specific error codes into more general error conditions.