A result type that maintains a chainable sequence of errors for debugging and error reporting.
More...
template<typename T, typename E = FormattableError>
class porytiles2::ChainableResult< T, E >
A result type that maintains a chainable sequence of errors for debugging and error reporting.
ChainableResult extends the concept of std::expected by maintaining a chain of error propagation through multiple layers of the application. Unlike a simple std::expected that only stores the immediate error, ChainableResult maintains a full chain of errors from the originating cause up through each layer that adds context. This is particularly useful for debugging complex failures where understanding the root cause requires knowing the full context of how an error propagated through the system.
The class enforces that error type E must derive from the Error interface, ensuring all errors in the chain can be properly cloned and formatted. The error chain is stored as a vector of unique_ptr<Error> objects, allowing polymorphic error types while maintaining proper ownership semantics.
- Template Parameters
-
| T | The type of the expected success value |
| E | The error type, must be derived from Error interface |
Definition at line 31 of file chainable_result.hpp.
template<typename T , typename E = FormattableError>
Constructs a ChainableResult from a success value.
This constructor allows implicit conversion from a success value of type T to a ChainableResult. The result is stored as a successful value with no error chain. This provides ergonomic construction for success cases, similar to std::expected's implicit construction from T.
- Parameters
-
| value | The success value to store |
Definition at line 44 of file chainable_result.hpp.
template<typename T , typename E = FormattableError>
Constructs a ChainableResult from an error value.
This constructor allows implicit conversion from an error value of type E to a ChainableResult. The error is stored as the initial error in the chain. This provides ergonomic construction for error cases at the bottom level of an error chain, similar to std::expected's construction from std::unexpected.
- Parameters
-
| error | The error value to store |
Definition at line 57 of file chainable_result.hpp.
template<typename T , typename E = FormattableError>
template<typename CauseT , typename CauseE >
Constructs a ChainableResult by chaining a new error with an existing error chain.
This constructor creates a new error result that includes both a new error message and the complete error chain from a cause result. This is the primary mechanism for building error context as errors propagate up through application layers. The new error is added to the beginning of the chain, followed by all errors from the cause result's chain.
- Template Parameters
-
| CauseT | The success type of the cause result (unused but required for template matching) |
| CauseE | The error type of the cause result, must be derived from Error |
- Parameters
-
| error | The new error to add at this level |
| cause_result | The ChainableResult containing the error chain to chain |
Definition at line 78 of file chainable_result.hpp.
template<typename T , typename E = FormattableError>
template<typename OtherT , typename OtherE >
Adds all errors from another ChainableResult's chain to this result's chain.
This method appends the complete error chain from a cause result to the current error chain. Each error in the cause's chain is cloned to maintain proper ownership semantics. The method will panic if the cause_result contains a success value rather than an error, as this would indicate a programming error.
- Template Parameters
-
| OtherT | The success type of the cause result |
| OtherE | The error type of the cause result |
- Parameters
-
Definition at line 107 of file chainable_result.hpp.
template<typename T , typename E = FormattableError>
Returns the complete error chain.
The error chain contains all errors in the chain, starting with the most recent error (added at this level) and proceeding through to the original root cause. Each error in the chain is owned by this ChainableResult through unique_ptr.
- Returns
- A const reference to the vector of error pointers representing the full error chain
Definition at line 194 of file chainable_result.hpp.
template<typename T , typename E = FormattableError>
Returns a const reference to the immediate error.
This returns only the immediate error at this level, not the full error chain. To access the complete error history, use the chain() method instead.
- Returns
- A const reference to the error value
Definition at line 179 of file chainable_result.hpp.
template<typename T , typename E = FormattableError>
Returns an rvalue reference to the contained success value.
This method provides move access to the success value when called on an rvalue ChainableResult. It enables efficient extraction of the value when the ChainableResult itself is a temporary or has been moved from. It will throw std::bad_expected_access if called when the result contains an error rather than a success value.
- Returns
- An rvalue reference to the success value
Definition at line 165 of file chainable_result.hpp.