Overview
The error handling system in Draconis++ provides structured error types and Rust-style error propagation macros. It’s built on top ofstd::expected to provide type-safe error handling.
Namespace: draconis::utils::error
Header: <Drac++/Utils/Error.hpp>
DracErrorCode enum
Error codes for OS-level and general operations.Values
| Code | Description |
|---|---|
ApiUnavailable | A required OS service/API is unavailable or failed unexpectedly at runtime |
ConfigurationError | Configuration or environment issue |
CorruptedData | Data present but corrupt or inconsistent |
InternalError | An error occurred within the application’s OS abstraction code logic |
InvalidArgument | An invalid argument was passed to a function or method |
IoError | General I/O error (filesystem, pipes, etc.) |
NetworkError | A network-related error occurred (e.g., DNS resolution, connection failure) |
NotFound | A required resource (file, registry key, device, API endpoint) was not found |
NotSupported | The requested operation is not supported on this platform, version, or configuration |
Other | A generic or unclassified error originating from the OS or an external library |
OutOfMemory | The system ran out of memory or resources to complete the operation |
ParseError | Failed to parse data obtained from the OS (e.g., file content, API output) |
PermissionDenied | Insufficient permissions to perform the operation |
PermissionRequired | Operation requires elevated privileges |
PlatformSpecific | An unmapped error specific to the underlying OS platform occurred (check message) |
ResourceExhausted | System resource limit reached (not memory) |
Timeout | An operation timed out (e.g., waiting for IPC reply) |
UnavailableFeature | Feature not present on this hardware/OS |
DracError struct
Holds structured information about an OS-level error.Fields
A descriptive error message, potentially including platform details
The source location where the error occurred (file, line, function)
The general category of the error
Constructor
The error code category
The error message
The source location (automatically captured if not provided)
Error creation macros
These macros simplify creating and returning errors.ERR
Creates and returns an error with a code and message.The error code
The error message
ERR_FMT
Creates and returns a formatted error message.The error code
Format string (std::format style)
Format arguments
ERR_FROM
Returns an error constructed from an existing error object.The error object to return
Error propagation macros
These macros provide Rust-style error propagation using the? operator pattern.
TRY
Propagates errors fromResult<T> (non-void types).
An expression returning a
Result<T, E> where T is not voidPlatform differences:
- GCC/Clang: Uses GNU statement expressions
- MSVC: Uses lambda-based implementation
TRY_VOID
Propagates errors fromResult<void> (void types).
An expression returning a
Result<void, E>TRY_RESULT (MSVC only)
Alternative error propagation for MSVC without exceptions.Variable to assign the result to
Expression returning a Result
Complete example
Best practices
-
Use TRY macros for clean error propagation
-
Choose appropriate error codes
-
Provide context in error messages
-
Use TRY_VOID for void Results
See also
- Type system -
ResultandErrtype aliases - CacheManager - Uses
Resultfor error handling