util: virerror: Introduce virGetLastError{Code,Domain} public APIs

Many places in the code call virGetLastError() just to check the
raised error code, or domain. However virGetLastError() can return
NULL, so the code has to check for that first. This patch therefore
introduces virGetLasError{Code,Domain} functions which always return a
valid error code or domain respectively, thus dropping the need to
perform any checks on the error object.

Signed-off-by: Ramy Elkest <ramyelkest@gmail.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
ramyelkest 2018-05-05 13:04:20 +01:00 committed by Erik Skultety
parent 52d88d11db
commit 50e96bb2a1
3 changed files with 43 additions and 0 deletions

View File

@ -344,6 +344,8 @@ void virResetLastError (void);
void virResetError (virErrorPtr err);
void virFreeError (virErrorPtr err);
int virGetLastErrorCode (void);
int virGetLastErrorDomain (void);
const char * virGetLastErrorMessage (void);
virErrorPtr virConnGetLastError (virConnectPtr conn);

View File

@ -792,4 +792,10 @@ LIBVIRT_4.4.0 {
virConnectBaselineHypervisorCPU;
} LIBVIRT_4.1.0;
LIBVIRT_4.5.0 {
global:
virGetLastErrorCode;
virGetLastErrorDomain;
} LIBVIRT_4.4.0;
# .... define new API here using predicted next version number ....

View File

@ -271,6 +271,41 @@ virGetLastError(void)
}
/**
* virGetLastErrorCode:
*
* Get the most recent error code (enum virErrorNumber).
*
* Returns the most recent error code, or VIR_ERR_OK if none is set.
*/
int
virGetLastErrorCode(void)
{
virErrorPtr err = virLastErrorObject();
if (!err)
return VIR_ERR_OK;
return err->code;
}
/**
* virGetLastErrorDomain:
*
* Get the most recent error domain (enum virErrorDomain).
*
* Returns a numerical value of the most recent error's origin, or VIR_FROM_NONE
* if none is set.
*/
int
virGetLastErrorDomain(void)
{
virErrorPtr err = virLastErrorObject();
if (!err)
return VIR_FROM_NONE;
return err->domain;
}
/**
* virGetLastErrorMessage:
*