carb::ErrorApi
Defined in carb/Error.h
-
struct ErrorApi
The low-level API for interacting with the Carbonite error handling system. At the core, this system maintains a thread-specific error code and optional error message.
The state of the error objects are maintained through
libcarb.so/carb.dll, the API is accessed throughcarbGetErrorApiorErrorApi::instance. Different versions of the API interact with the same global state. A call toErrorApi::viewCurrentErrorwith version 1.0 will still be able to read aErrorApi::setErrorfrom a version 4.0 API (version 4.0 does not exist at this time).Public Members
-
Error const *(*viewCurrentError)(Result *code)
Get a view of this thread’s current error, if it is set.
Warning
The caller does not own the returned error. Any information pointed to by the returned value can be altered or cleared by a function calling any of the
setErrorcalls on this thread. Errors accessed are meant to be acted upon immediately. If you wish to preserve information, copy the pieces you wish to save or store the whole thing witherrorCloneortakeCurrentError.- Param code
[out] If not
nullptrand there is a current error, then*codewill be set to the error code of the current error. Use this to save an extra call togetErrorInfowhen you are going to act on the code value. If there is no current error, then this will be set tokResultSuccess.- Return
The current error, if it was set. If there is no error, this returns
nullptr.
-
Error *(*takeCurrentError)(Result *code)
Get this thread’s current error as an owned object, if it is set. After this call, this thread’s current error will be cleared.
It is the caller’s responsibility to clean up the returned value, unless it is
nullptr.This function is equivalent to cloning the current error, then resetting it:
ErrorApi const& api = ErrorApi::instance(); Result code{}; // <- optimization only -- see viewCurrentError Error* my_err = api.errorClone(api.viewCurrentError(&code)); api.setErrorTo(nullptr);
- Param code
[out] If not
nullptrand there is a current error, then*codewill be set to the error code of the current error. Use this to save an extra call togetErrorInfowhen you are going to act on the code value. If there is no current error, then this will be set tokResultSuccess.- Return
An owned version of the current error, if it was set. If there is no error, this returns
nullptr.
-
Result (*setError)(Result code, char const *message, std::size_t message_size)
Set this thread’s current error to a pre-formatted string.
In the case of any error of a call to this function, the current error will always be set with code. However, the associated message of the error will be the default for that code.
- Param code
The code for the created error, which callers can potentially act on.
- Param message
The associated message containing additional details about the error. If this is
nullptr, the default message for the code is used.- Param message_size
The number of bytes message is. If message is
nullptr, this must be0.- Retval kResultSuccess
when the error is set without issue.
- Retval kResultInvalidArgument
if the message is
nullptrand message_size is not 0 or if message is notnullptrand the message_size is 0. The current error will still be set to code, but the message will be the default for that code.- Retval kResultOutOfMemory
if the message is too large to fit in the string buffer, but the call to allocate memory failed. The current error is still set to code, but the error message is saved as the default for that code.
- Retval kResultTooMuchData
If the provided message is too large to fit in any error message buffer (the current maximum is 64 KiB). The current error will be set with a truncated version of the provided message.
-
Result (*setErrorTo)(Error *error)
Set this thread’s current error to the exact error or clear the current error if error is
nullptr. Responsibility for this instance is taken by the error system, so you should not callerrorReleaseon it.- Param error
The error to set. If this is
nullptr, this clears the current error.- Retval kResultSuccess
when the error is set without issue.
- Retval kResultInvalidOperation
if
error == viewCurrentError(). Restoring a previously-saved error should occur through aerrorClonecall, so doing this represents a bug in the code (note that you must have usedconst_castor equivalent to get this to compile). In this case, no action is performed, as the current error is already set to the error.
-
void (*setErrorOom)()
Set this thread’s current error to a pre-allocated error indicating the system is out of memory. It will always succeed.
-
Result (*errorRelease)(Error *error)
Release the error which was previously
errorCloned orerrorCreated.- Retval kResultSuccess
when the error was released.
- Retval kResultInvalidOperation
when
error == viewCurrentError(). The current error does not need to be released.
-
Error *(*errorClone)(Error const *source)
Create a copy of the source error.
- Param source
The error to clone from. If this is
nullptr,nullptrwill be returned without error.- Return
The cloned error. In case of error,
nullptris returned and the current error is set with a message containing additional details.
-
Error *(*errorCreate)(Result code, const char *message, std::size_t message_size)
Create an error message with the code and pre-formatted message string without setting the current error. The parameters operate similarly to
setError, but are more strict. WheresetErrorwould return a non-OK code but still set the current error, this function would returnnullptrand set the current error to the failure.- Param code
The code for the created error, which callers can potentially act on.
- Param message
The associated message containing additional details about the error. If this is
nullptr, the default message for the code is used.- Param message_size
The number of bytes message is.
- Return
The created error on success. On failure,
nullptris returned and the current error is set with the reason for the failure. The code values are the same assetError.
-
Result (*getErrorInfo)(Error const *error, Result *code, char const **message, std::size_t *message_size)
Extract the information associated with the error message. The output parameters are views of the properties, so they are only valid as long as error is valid.
- Param error
The source to extract data from. This can not be
nullptr.- Param code
[out] If not
nullptr,*codewill be set to the code of this error.- Param message
[out] If not
nullptr,*messagewill point to the error’s detail message.- Param message_size
[out] If not
nullptr,*message_sizewill be set to the size of the error’s detail message. Note that*messageis always null-terminated, but this is useful for optimization when copying.- Retval kResultSuccess
when the operation was successfully performed.
- Retval kResultInvalidArgument
if error is
nullptr. The current error is not set in this case (since the error can becurrent_error, we do not want to clear it for you.
-
Result (*getCodeDescription)(Result code, char const **name, std::size_t *name_size, char const **message, std::size_t *message_size)
Get the name and default message for the given code. The name a symbol-like name in snake case like
"invalid_argument". The message is the default message for the code as a sentence fragment like"invalid argument".Note that all of the output parameters are allowed to be
nullptr. In this case,kResultSuccessis still returned. This can be useful for checking if a given code exists at all.- Param code
The error code to look up.
- Param name
[out] A pointer to a place to put the name of the code. If this is
nullptr, it will not be set.- Param name_size
[out] A pointer to place to put the size of name (in UTF-8 code units). Since name is null-terminated, this is not strictly needed, but can save you a
strlencall. If this isnullptr, it will not be set.- Param message
[out] A pointer to the place to put the default message for this code. If this is
nullptr, it will not be set.- Param message_size
[out] A pointer to the place to put the size of message (in UTF-8 code units). Since message is null-terminated, this is not strictly needed, but can save you a
strlencall. If this isnullptr, it will not be set.- Retval kResultSuccess
when the operation was successfully performed.
- Retval kResultNotFound
when code is not in the known list of error codes. The current error is not set.
Public Static Functions
-
static inline constexpr carb::InterfaceDesc getInterfaceDesc()
Returns information about this interface. Auto-generated by CARB_PLUGIN_INTERFACE(). *
- Returns
The carb::InterfaceDesc struct with information about this interface.
-
static inline ErrorApi const &instance() noexcept
Get the singleton instance of this API.
The implementation of this is backed by
libcarb.so/carb.dll, so it is a singleton instance for all loaded modules. This function is backed bycarbGetErrorApiwith theversionargument provided with the value of the API version the calling module was built against.
-
Error const *(*viewCurrentError)(Result *code)