ErrorApi#
Fully qualified name: 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 Functions
- inline Result setError(
- Result code,
- cpp::string_view message = {},
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.
- Parameters:
code – The code for the created error, which callers can potentially act on.
message – The associated message containing additional details about the error. If empty, the default default message for
codeis used.
- Return values:
kResultSuccess – when the error is set without issue.
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.
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.
- inline Error *errorCreateS(
- Result code,
- cpp::string_view message = {},
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.- Parameters:
code – The code for the created error, which callers can potentially act on.
message – The associated message containing additional details about the error.
- Returns:
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.
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 (*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.
-
Result (*getErrorInfoS)(Error const *error, Result *code, carb::cpp::string_view *message)#
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, and they’re filled with an empty view otherwise.
- 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] A pointer to a string_view to put the message of the code. If this is
nullptr, it will not be set.- 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 (*getCodeDescriptionS)(Result code, carb::cpp::string_view *name, carb::cpp::string_view *message)#
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 string_view to put the name of the code. If this is
nullptr, it will not be set.- Param message:
[out] A pointer to a string_view to put the message of the code. If this is
nullptr, 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() or CARB_PLUGIN_INTERFACE_EX.
- Returns:
The carb::InterfaceDesc struct with information about this interface.
- static inline constexpr carb::InterfaceDesc getLatestInterfaceDesc(
Returns information about the latest version of this interface.
Auto-generated by CARB_PLUGIN_INTERFACE() or CARB_PLUGIN_INTERFACE_EX.
- Returns:
The carb::InterfaceDesc struct with information about the latest version of 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.
-
static inline void clearError() noexcept#
Clears any error for the current thread.
This function is syntactic sugar around
setErrorTo(nullptr).See also
-
static inline Result getError() noexcept#
Get a view of this thread’s current error.
This function is syntactic sugar around viewCurrentError(). If further information is desired, including the current thread’s error message, use getErrorInfo().
- Returns:
If the calling thread has a current error, then the return value will be the result code of the current error. If the calling thread has no current error, the result will be omni::core::kResultSuccess.
-
static inline void setFromErrno()#
Sets the current thread’s error code value based on the value of
errno.The following table is a mapping of
errnovalues to omni::core::Result codes:Mapping of errno value to Result codes# errno values
0ENOSYSEACCESENOMEMEINVALEAGAINEWOULDBLOCKEINTREEXISTEPERMENOENTEverything else
Note
The value of
errnoremains consistent across the call to this function.
-
static void setFromWinApiErrorCode()#
(Windows only) Sets the current thread’s error code value based on the value of
GetLastError().The following table is a mapping of Windows error values to omni::core::Result codes:
Mapping of Windows error value to Result code# errno values
ERROR_SUCCESSERROR_PATH_NOT_FOUNDERROR_FILE_NOT_FOUNDERROR_ACCESS_DENIEDERROR_ALREADY_EXISTSERROR_FILE_EXISTSERROR_OUTOFMEMORYERROR_NO_MORE_FILESERROR_NO_MORE_ITEMSERROR_NOT_IMPLEMENTEDERROR_WAIT_TIMEOUTERROR_ERROR_TIMEOUTEverything else
Note
The value of
errnoremains consistent across the call to this function.