module
CnxErrorCnxError
provides an extensible, configurable type for communicating recoverable errors via error codes and error message strings
Example
CnxError do_thing(i64 input, i64* restrict out) { // do things ... if(errno) { return cnx_error_new(errno, CNX_POSIX_ERROR_CATEGORY); } else { *out = 42; return cnx_error_new(0, CNX_POSIX_ERROR_CATEGORY); } } void func(void) { i64 i = 0; let maybe_err = do_thing(10, &i); if(cnx_error_code(&maybe_err) != 0) { // handle error ... } // do other things... }
In most cases, CnxError
would be used in tandem with CnxResult(T)
to enable concise error handling without having to use "out" parameters.
Classes
Typedefs
Functions
-
Trait(CnxErrorCategory,
const_
cstring(*)(const CnxErrorCategory*restrict self, i64 code) message;i64(*)(const CnxErrorCategory*restrict self) get_last_error;) CnxErrorCategory
is aTrait
that provides the mechanism to convert an arbitrary error code into a corresponding message associated with a specific class of errors, and to get the last error that occurred in a particular category (if supported by the category. For example, global categories likeCNX_WIN32_ERROR_CATEGORY
andCNX_POSIX_ERROR_CATEGORY
support this, but categories local to specific modules probably won't).- CnxError cnx_error_new(i64 error_code, CnxErrorCategory error_category)
- Creates a
CnxError
with the given error code and category. - i64 cnx_error_code(const CnxError*restrict self)
- Returns the error code associated with the given error.
-
const_
cstring cnx_error_message(const CnxError*restrict self) - Returns the error message associated with the given error, as a
cstring
-
const_
cstring cnx_error_category_get_message(CnxErrorCategory self, i64 error_code) - Returns the error message associated with the given error code, as a
cstring
- i64 cnx_error_category_get_last_error(CnxErrorCategory self)
- Returns the error code for the last reported error.
-
const_
cstring cnx_posix_category_get_message(const CnxErrorCategory*restrict self, i64 error_code) - Returns the POSIX error message associated with the given error code, as a
cstring
- i64 cnx_posix_category_get_last_error(const CnxErrorCategory*restrict self)
- Returns the error code for the last reported POSIX error.
-
static ImplTraitFor(CnxFormat,
CnxError,
cnx_
error_ is_ specifier_ valid, cnx_ error_ format, cnx_ error_ format_ with_ allocator) - Implement
CnxFormat
forCnxError
Defines
- #define CNX_POSIX_ERROR_CATEGORY
- The
CnxErrorCategory
to map POSIX error codes. - #define CNX_DEFAULT_ERROR_CATEGORY
- the default
CnxErrorCategory
. By default, this isCNX_POSIX_ERROR_CATEGORY
Typedef documentation
typedef struct CnxError CnxError
#include <include/Cnx/Error.h>
CnxError
provides an extensible, configurable type for communicating recoverable errors via error codes and error message strings
Example
CnxError do_thing(i64 input, i64* restrict out) { // do things ... if(errno) { return cnx_error_new(errno, CNX_POSIX_ERROR_CATEGORY); } else { *out = 42; return cnx_error_new(0, CNX_POSIX_ERROR_CATEGORY); } } void func(void) { i64 i = 0; let maybe_err = do_thing(10, &i); if(cnx_error_code(&maybe_err) != 0) { // handle error ... } // do other things... }
In most cases, CnxError
would be used in tandem with CnxResult(T)
to enable concise error handling without having to use "out" parameters.
Function documentation
Trait(CnxErrorCategory,
const_ cstring(*)(const CnxErrorCategory*restrict self, i64 code) message;i64(*)(const CnxErrorCategory*restrict self) get_last_error;)
#include <include/Cnx/Error.h>
CnxErrorCategory
is a Trait
that provides the mechanism to convert an arbitrary error code into a corresponding message associated with a specific class of errors, and to get the last error that occurred in a particular category (if supported by the category. For example, global categories like CNX_WIN32_ERROR_CATEGORY
and CNX_POSIX_ERROR_CATEGORY
support this, but categories local to specific modules probably won't).
CnxErrorCategory
is what allows a CnxError
to communicate error messages unique to the module the error originated from.
CnxError cnx_error_new(i64 error_code,
CnxErrorCategory error_category)
#include <include/Cnx/Error.h>
Creates a CnxError
with the given error code and category.
Parameters | |
---|---|
error_code | - The error code associated with the error |
error_category | - The CnxErrorCategory that will generate the message associated with the error code |
Returns | a CnxError |
i64 cnx_error_code(const CnxError*restrict self)
#include <include/Cnx/Error.h>
Returns the error code associated with the given error.
Parameters | |
---|---|
self | - The error |
Returns | the associated error code |
const_ cstring cnx_error_message(const CnxError*restrict self)
#include <include/Cnx/Error.h>
Returns the error message associated with the given error, as a cstring
Parameters | |
---|---|
self | - The error |
Returns | the associated error message |
const_ cstring cnx_error_category_get_message(CnxErrorCategory self,
i64 error_code)
#include <include/Cnx/Error.h>
Returns the error message associated with the given error code, as a cstring
Parameters | |
---|---|
self | - The error category to get the message from |
error_code | - The error code |
Returns | the message associated with the error code |
i64 cnx_error_category_get_last_error(CnxErrorCategory self)
#include <include/Cnx/Error.h>
Returns the error code for the last reported error.
Parameters | |
---|---|
self | - The error category to get the last error from |
Returns | the i64 error code representing the last reported error |
const_ cstring cnx_posix_category_get_message(const CnxErrorCategory*restrict self,
i64 error_code)
#include <include/Cnx/Error.h>
Returns the POSIX error message associated with the given error code, as a cstring
Parameters | |
---|---|
self | |
error_code | - The error code to get the message for |
Returns | the message associated with the error code |
i64 cnx_posix_category_get_last_error(const CnxErrorCategory*restrict self)
#include <include/Cnx/Error.h>
Returns the error code for the last reported POSIX error.
Returns | the error code for the last POSIX error |
---|
static ImplTraitFor(CnxFormat,
CnxError,
cnx_ error_ is_ specifier_ valid,
cnx_ error_ format,
cnx_ error_ format_ with_ allocator)
#include <include/Cnx/Error.h>
Implement CnxFormat
for CnxError
Returns | The CnxFormat trait implementation for CnxError |
---|
Define documentation
#define CNX_POSIX_ERROR_CATEGORY
#include <include/Cnx/Error.h>
The CnxErrorCategory
to map POSIX error codes.
CNX_POSIX_ERROR_CATEGORY
is the error category to map POSIX error codes to their associated error messages. It will produce equivalent results to strerror
in standard C
#define CNX_DEFAULT_ERROR_CATEGORY
#include <include/Cnx/Error.h>
the default CnxErrorCategory
. By default, this is CNX_POSIX_ERROR_CATEGORY
This can be overridden by defining CNX_DEFAULT_ERROR_CATEGORY
to a compound literal of your chosen CnxErrorCategory
prior to including <C2nxt/Error.h>