ErrorApi#
Fully qualified name: carb::ErrorApi
Defined in carb/Error.h
-
struct ErrorApi : public carb::IError#
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
-
virtual ErrorPtr viewCurrentError() const noexcept = 0#
Access the current thread’s Error, if any.
Note: the error remains set for the current thread. In order to clear the error after viewing it, call ErrorApi::clearError or setError with
nullptr.
-
virtual void setError(ErrorPtr error) const noexcept = 0#
Set this thread’s current error to the exact given
erroror clear the current error iferroris empty.- Parameters:
error – The error to set. If this is empty, this clears the current error.
- virtual omni::expected<void, ErrorCode> setError(
- ErrorCode 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.
- Possible returned ErrorCodes
kInvalidArgument - A default-constructed
codewas given (or ErrorCode::kOutOfMemory), but with a non-emptymessage. Thecodeis still set as the current error butmessageis ignored.kResultOutOfMemory - 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 - 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.
- 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. A non-empty message is not allowed for kOutOfMemory or a default-constructedcode.
- Returns:
voidon success; if an error occurs while setting the current error, anomni::unexpectedis returned with the ErrorCode.
-
virtual void setErrorOom() const noexcept = 0#
Set this thread’s current error to a pre-allocated error indicating the system is out of memory. It will always succeed.
- virtual omni::expected<ErrorPtr, ErrorCode> errorCreate(
- ErrorCode 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 returnomni::unexpectedwith the error.- Possible return ErrorCode values
ErrorCode::kInvalidArgument - a default-constructed
codewas provided, or ErrorCode::kOutOfMemory was given with a non-empty message.
- Parameters:
code – The code for the created error, which callers can potentially act on. This does not have to be a pre-defined ErrorCode value.
message – The associated message containing additional details about the error.
- Returns:
The created error on success. On failure,
omni::unexpectedwith an ErrorCode is returned giving the reason for the failure.
- virtual cpp::optional<std::pair<cpp::zstring_view, cpp::zstring_view>> getCodeDescription(
- ErrorCode code,
Retrieve the name and default message for the given code.
The name is 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".- Possible ErrorCode return values
kResultNotFoundif code is not in the known list of error codes.
Note
The current error is not changed.
- Parameters:
code – The ErrorCode to look up.
- Returns:
A optional containing a
std::pair<name, message>on success, wherenameandmessageare of type carb::cpp::zstring_view. If the code is not a known default ErrorCode,carb::cpp::nulloptis returned instead.
- virtual ErrorCode currentError(
- cpp::zstring_view *outMessage = nullptr,
A simple helper function to retrieve the ErrorCode stored by the current thread.
- Parameters:
outMessage – If not
nullptr, will receive the error message stored by the current thread. If no error message is stored by the current thread, this will receive an empty zstring_view. This view is valid until the error stored by the current thread changes.- Returns:
The ErrorCode stored by the current thread. If no ErrorCode is stored by the current thread, This will be a default-constructed ErrorCode.
Public Static Functions
-
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 stored for the current thread.
- Post:
IError::viewCurrentError() returns an empty ErrorPtr.
- static inline ErrorCode getError(
- cpp::zstring_view *outMessage = nullptr,
A simple helper function to retrieve the ErrorCode stored by the current thread.
- Parameters:
outMessage – If not
nullptr, will receive the error message stored by the current thread. If no error message is stored by the current thread, this will receive an empty zstring_view. This view is valid until the error stored by the current thread changes.- Returns:
The ErrorCode stored by the current thread. If no ErrorCode is stored by the current thread, This will be a default-constructed ErrorCode.
- static inline ErrorCode convertFromErrno( )#
Converts the current thread’s
errnovalue to an ErrorCode.The following table is a mapping of
errnovalues to ErrorCode codes:Mapping of errno value to Result codes# errno values
0carb::ErrorCode{}ENOSYScarb::ErrorCode::kNotImplementedEACCEScarb::ErrorCode::kAccessDeniedENOMEMcarb::ErrorCode::kOutOfMemoryEINVALcarb::ErrorCode::kInvalidArgumentEAGAINcarb::ErrorCode::kTryAgainEWOULDBLOCKcarb::ErrorCode::kTryAgainEINTRcarb::ErrorCode::kInterruptedEEXISTcarb::ErrorCode::kAlreadyExistsEPERMcarb::ErrorCode::kInvalidOperationENOENTcarb::ErrorCode::kNotFoundEverything else
carb::ErrorCode::kFailNote
The value of
errnoremains consistent across the call to this function.- Parameters:
err – An
errnovalue. If not specified, the current value fromerrnoitself will be used.message – [out] An optional
std::string*that will receive the error description for the currenterrnovalue as viastrerror. If the errno value is0the string will be cleared.
-
static inline void setFromErrno()#
Sets the current thread’s error code value based on the value of
errno.Note
The value of
errnoremains consistent across the call to this function.
- static ErrorCode convertFromWinApiErrorCode(
- DWORD err = ::GetLastError(),
- std::string *message = nullptr,
(Windows only) Converts the error code from
GetLastError()to a omni::core::Result.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_SUCCESScarb::ErrorCode{}ERROR_PATH_NOT_FOUNDcarb::ErrorCode::kNotFoundERROR_FILE_NOT_FOUNDcarb::ErrorCode::kNotFoundERROR_ACCESS_DENIEDcarb::ErrorCode::kAccessDeniedERROR_ALREADY_EXISTScarb::ErrorCode::kAlreadyExistsERROR_FILE_EXISTScarb::ErrorCode::kAlreadyExistsERROR_OUTOFMEMORYcarb::ErrorCode::kOutOfMemoryERROR_NO_MORE_FILEScarb::ErrorCode::kNoMoreItemsERROR_NO_MORE_ITEMScarb::ErrorCode::kNoMoreItemsERROR_NOT_IMPLEMENTEDcarb::ErrorCode::kNotImplementedERROR_WAIT_TIMEOUTcarb::ErrorCode::kTryAgainERROR_ERROR_TIMEOUTcarb::ErrorCode::kTryAgainEverything else
carb::ErrorCode::kFailNote
The value of
GetLastErrorremains consistent across the call to this function.- Parameters:
err – The value from GetLastError() to convert. If not specified, the current value from GetLastError() will be used.
message – [out] An optional
std::string*that will receive the Windows formatted error description for the current error code. IfGetLastErrorreportsERROR_SUCCESSthis will be cleared.
- Returns:
The omni::core::Result mapped from the windows error code.
-
static void setFromWinApiErrorCode()#
(Windows only) Sets the current thread’s error code value based on the value of
GetLastError().See convertFromWinApiErrorCode for the error mapping.
Note
The value of
GetLastErrorremains consistent across the call to this function.
- 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.
-
virtual ErrorPtr viewCurrentError() const noexcept = 0#