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

Contains utilities for error handling and classification. More...

Classes

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

Typedefs

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

Enumerations

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

Functions

constexpr std::error_code make_error_code (ExtraError e)
 Create an error code from an ExtraError enum value.
 
constexpr std::error_condition 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 > make_error (E &&code, Ctx &&context={})
 Create an error result of the specified type.
 
template<typename T>
constexpr Result< T > 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 > 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 last_error () noexcept
 Retrieve the last system error code and reset errno.
 
template<typename T>
Result< T > 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 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 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 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 > first_of (std::initializer_list< Result< T > > results)
 Return first success result from multiple alternatives.
 

Detailed Description

Contains utilities for error handling and classification.

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 = std::expected<T, Error>

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

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

◆ StringResult

using error_utils::StringResult = Result<std::string>

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

Enumerator
invalid_argument 

std::invalid_argument exception.

length_error 

std::length_error exception.

logic_error 

std::logic_error base exception.

value_too_small 

std::underflow_error exception.

nonexistent_local_time 

std::chrono::nonexistent_local_time exception.

ambiguous_local_time 

std::chrono::ambiguous_local_time exception.

format_error 

std::format_error exception.

runtime_error 

std::runtime_error base exception.

bad_alloc 

std::bad_alloc exception.

bad_typeid 

std::bad_typeid exception.

bad_cast 

std::bad_cast exception.

bad_optional_access 

std::bad_optional_access exception.

bad_expected_access 

std::bad_expected_access exception.

bad_variant_access 

std::bad_variant_access exception.

bad_weak_ptr 

std::bad_weak_ptr exception.

bad_function_call 

std::bad_function_call exception.

bad_exception 

std::bad_exception exception.

exception 

all std::exception exceptions.

unknown_exception 

catch-all for any other exceptions.

unknown_error 

Unknown error (not related to exceptions).

◆ 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.

Enumerator
logic_error 

Errors related to program logic and invalid operations.

runtime_error 

Errors occurring during program execution.

resource_error 

Errors related to resource allocation and management.

access_error 

Errors related to invalid access of data structures.

other_error 

Other errors that do not fit into the above categories.

Function Documentation

◆ first_of()

template<typename T>
Result< T > error_utils::first_of ( std::initializer_list< Result< T > > results)
nodiscardconstexpr

Return first success result from multiple alternatives.

Parameters
resultsMultiple results of the same type
Template Parameters
TThe type of the result
Returns
First successful result or combined error

◆ invoke_with_syscall_api()

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 = {} )
nodiscardnoexcept

Execute a function that may set errno, capturing the result and any error.

This function is intended for use with system calls that return an integer result, where a return value of -1 indicates an error.

Parameters
funcFunction that may set errno. Expected to return an integral type convertible to int.
Template Parameters
FuncType of the function to execute
Parameters
error_contextContext to use if an error occurs
Template Parameters
FuncThe type of the function to execute
Returns
Result of the function or an error if errno was set
Note
The errno value is reset before and after the function call.
The function must be noexcept to ensure that it does not throw exceptions.
Notice that the function must be invocable with no arguments.
Use a lambda or std::bind to wrap the function.

◆ last_error()

std::error_code error_utils::last_error ( )
inlinenodiscardnoexcept

Retrieve the last system error code and reset errno.

Returns
The last system error code as std::error_code

◆ make_error() [1/3]

template<typename T>
Result< T > error_utils::make_error ( const std::error_code & code,
const std::string_view context = {} )
nodiscardconstexpr

Create an error result of the specified type from a std::error_code.

Parameters
codeThe std::error_code error code
contextOptional context information
Template Parameters
TThe type of the result
Returns
An unexpected result with the error.

◆ make_error() [2/3]

template<typename T>
Result< T > error_utils::make_error ( const std::regex_constants::error_type code,
std::string_view context = {} )
nodiscardconstexpr

Create an error result of the specified type from a std::regex_constants::error_type.

Parameters
codeThe regex error code
contextOptional context information
Template Parameters
TThe type of the result
Returns
An unexpected result with the regex error.

◆ make_error() [3/3]

template<typename T, typename E, typename Ctx = std::string_view>
requires detail::convertible_to_error_code<E>
Result< T > error_utils::make_error ( E && code,
Ctx && context = {} )
nodiscardconstexpr

Create an error result of the specified type.

Parameters
codeThe error code
contextOptional context information
Template Parameters
TThe type of the result
EThe type of the error code
CtxThe type of the context information
Returns
An unexpected result with the error.

◆ make_error_code()

std::error_code error_utils::make_error_code ( ExtraError e)
constexpr

Create an error code from an ExtraError enum value.

Parameters
eThe ExtraError enum value

◆ make_error_condition()

std::error_condition error_utils::make_error_condition ( ExtraErrorCondition e)
constexpr

Create an error condition from an ExtraErrorCondition enum value.

Parameters
eThe ExtraErrorCondition enum value

◆ make_error_from_errno()

template<typename T>
Result< T > error_utils::make_error_from_errno ( const std::string_view context = {})
nodiscard

Create an error result from the current errno value.

Parameters
contextOptional context information
Template Parameters
TThe type of the result
Returns
An unexpected result with the current errno

◆ try_catch()

template<typename Func, typename R = std::invoke_result_t<Func>>
auto error_utils::try_catch ( Func && func,
std::string_view context = {} ) -> Result<R>
nodiscardconstexpr

Execute a function and catch common exceptions, converting them to errors.

Parameters
funcFunction to execute
contextError context
Template Parameters
FuncThe type of the function to execute
RThe return type of the function. Automatically deduced.
Returns
Result of the function or an error from caught exceptions

◆ with_errno()

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>
nodiscard

Execute a function that may set errno, capturing the result and any error.

Note
The errno value is reset before and after the function call.
Parameters
funcFunction that may set errno
error_contextContext to use if an error occurs
Template Parameters
FuncThe type of the function to execute
RThe return type of the function. Automatically deduced.
Returns
Result of the function or an error if errno was set