libvirt/src/util/virerror.h

213 lines
8.1 KiB
C
Raw Normal View History

/*
* virerror.h: error handling and reporting code for libvirt
*
maint: improve VIR_ERR_NO_SUPPORT usage We weren't very consistent in our use of VIR_ERR_NO_SUPPORT; many users just passed __FUNCTION__ on, while others passed "%s" to silence over-eager compilers that warn about __FUNCTION__ not containing any %. It's nicer to route all these uses through a single macro, so that if we ever need to change the reporting, we can do it in one place. I verified that 'virsh -c test:///default qemu-monitor-command test foo' gives the same error message before and after this patch: error: this function is not supported by the connection driver: virDomainQemuMonitorCommand Note that in libvirt.c, we were inconsistent on whether virDomain* API used virLibConnError() (with VIR_FROM_NONE) or virLibDomainError() (with VIR_FROM_DOMAIN); this patch unifies these errors to all use VIR_FROM_NONE, on the grounds that it is unlikely that a caller learning that a call is unimplemented can do anything in particular with extra knowledge of which error domain it belongs to. One particular change to note is virDomainOpenGraphics which was trying to fail with VIR_ERR_NO_SUPPORT after a failed VIR_DRV_SUPPORTS_FEATURE check; all other places that fail a feature check report VIR_ERR_ARGUMENT_UNSUPPORTED. * src/util/virerror.h (virReportUnsupportedError): New macro. * src/libvirt-qemu.c: Use new macro. * src/libvirt-lxc.c: Likewise. * src/lxc/lxc_driver.c: Likewise. * src/security/security_manager.c: Likewise. * src/util/virinitctl.c: Likewise. * src/libvirt.c: Likewise. (virDomainOpenGraphics): Use correct error for unsupported feature. Signed-off-by: Eric Blake <eblake@redhat.com>
2013-12-20 01:38:59 +00:00
* Copyright (C) 2006-2014 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "internal.h"
#include "virautoclean.h"
#define VIR_ERROR_MAX_LENGTH 1024
extern virErrorFunc virErrorHandler;
extern void *virUserData;
2009-01-20 12:01:45 +00:00
int virErrorInitialize(void);
void virRaiseErrorFull(const char *filename,
2009-05-27 12:10:47 +00:00
const char *funcname,
size_t linenr,
int domain,
int code,
virErrorLevel level,
const char *str1,
const char *str2,
const char *str3,
int int1,
int int2,
const char *fmt, ...)
G_GNUC_PRINTF(12, 13);
2009-05-27 12:10:47 +00:00
void virRaiseErrorObject(const char *filename,
const char *funcname,
size_t linenr,
virErrorPtr err);
void virReportErrorHelper(int domcode, int errcode,
const char *filename,
const char *funcname,
size_t linenr,
const char *fmt, ...)
G_GNUC_PRINTF(6, 7);
void virReportSystemErrorFull(int domcode,
int theerrno,
const char *filename,
const char *funcname,
size_t linenr,
const char *fmt, ...)
G_GNUC_PRINTF(6, 7);
#define virReportSystemError(theerrno, fmt,...) \
virReportSystemErrorFull(VIR_FROM_THIS, \
(theerrno), \
__FILE__, __FUNCTION__, __LINE__, \
(fmt), __VA_ARGS__)
#define virReportInvalidNullArg(argname) \
virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
VIR_FROM_THIS, \
VIR_ERR_INVALID_ARG, \
VIR_ERR_ERROR, \
__FUNCTION__, \
#argname, \
NULL, \
0, 0, \
_("%s in %s must be NULL"), \
#argname, __FUNCTION__)
#define virReportInvalidNonNullArg(argname) \
virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
VIR_FROM_THIS, \
VIR_ERR_INVALID_ARG, \
VIR_ERR_ERROR, \
__FUNCTION__, \
#argname, \
NULL, \
0, 0, \
_("%s in %s must not be NULL"), \
#argname, __FUNCTION__)
#define virReportInvalidEmptyStringArg(argname) \
virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
VIR_FROM_THIS, \
VIR_ERR_INVALID_ARG, \
VIR_ERR_ERROR, \
__FUNCTION__, \
#argname, \
NULL, \
0, 0, \
_("string %s in %s must not be empty"), \
#argname, __FUNCTION__)
#define virReportInvalidPositiveArg(argname) \
virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
VIR_FROM_THIS, \
VIR_ERR_INVALID_ARG, \
VIR_ERR_ERROR, \
__FUNCTION__, \
#argname, \
NULL, \
0, 0, \
_("%s in %s must be greater than zero"), \
#argname, __FUNCTION__)
#define virReportInvalidNonZeroArg(argname) \
virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
VIR_FROM_THIS, \
VIR_ERR_INVALID_ARG, \
VIR_ERR_ERROR, \
__FUNCTION__, \
#argname, \
NULL, \
0, 0, \
_("%s in %s must not be zero"), \
#argname, __FUNCTION__)
#define virReportInvalidZeroArg(argname) \
virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
VIR_FROM_THIS, \
VIR_ERR_INVALID_ARG, \
VIR_ERR_ERROR, \
__FUNCTION__, \
#argname, \
NULL, \
0, 0, \
_("%s in %s must be zero"), \
#argname, __FUNCTION__)
#define virReportInvalidNonNegativeArg(argname) \
virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
VIR_FROM_THIS, \
VIR_ERR_INVALID_ARG, \
VIR_ERR_ERROR, \
__FUNCTION__, \
#argname, \
NULL, \
0, 0, \
_("%s in %s must be zero or greater"), \
#argname, __FUNCTION__)
#define virReportInvalidArg(argname, fmt, ...) \
virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
VIR_FROM_THIS, \
VIR_ERR_INVALID_ARG, \
VIR_ERR_ERROR, \
__FUNCTION__, \
#argname, \
NULL, \
0, 0, \
(fmt), __VA_ARGS__)
#define virReportUnsupportedError() \
virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_NO_SUPPORT, \
maint: improve VIR_ERR_NO_SUPPORT usage We weren't very consistent in our use of VIR_ERR_NO_SUPPORT; many users just passed __FUNCTION__ on, while others passed "%s" to silence over-eager compilers that warn about __FUNCTION__ not containing any %. It's nicer to route all these uses through a single macro, so that if we ever need to change the reporting, we can do it in one place. I verified that 'virsh -c test:///default qemu-monitor-command test foo' gives the same error message before and after this patch: error: this function is not supported by the connection driver: virDomainQemuMonitorCommand Note that in libvirt.c, we were inconsistent on whether virDomain* API used virLibConnError() (with VIR_FROM_NONE) or virLibDomainError() (with VIR_FROM_DOMAIN); this patch unifies these errors to all use VIR_FROM_NONE, on the grounds that it is unlikely that a caller learning that a call is unimplemented can do anything in particular with extra knowledge of which error domain it belongs to. One particular change to note is virDomainOpenGraphics which was trying to fail with VIR_ERR_NO_SUPPORT after a failed VIR_DRV_SUPPORTS_FEATURE check; all other places that fail a feature check report VIR_ERR_ARGUMENT_UNSUPPORTED. * src/util/virerror.h (virReportUnsupportedError): New macro. * src/libvirt-qemu.c: Use new macro. * src/libvirt-lxc.c: Likewise. * src/lxc/lxc_driver.c: Likewise. * src/security/security_manager.c: Likewise. * src/util/virinitctl.c: Likewise. * src/libvirt.c: Likewise. (virDomainOpenGraphics): Use correct error for unsupported feature. Signed-off-by: Eric Blake <eblake@redhat.com>
2013-12-20 01:38:59 +00:00
__FILE__, __FUNCTION__, __LINE__, __FUNCTION__)
#define virReportRestrictedError(...) \
virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_OPERATION_DENIED, \
2013-12-20 14:02:49 +00:00
__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
/* The sizeof(...) comparison here is a hack to catch typos
* in the name of the enum by triggering a compile error, as well
* as detecting if you passed a typename that refers to a function
* or struct type, instead of an enum. It should get optimized away
* since sizeof() is known at compile time */
#define virReportEnumRangeError(typname, value) \
virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INTERNAL_ERROR, \
__FILE__, __FUNCTION__, __LINE__, \
"Unexpected enum value %d for %s", \
value, sizeof((typname)1) != 0 ? #typname : #typname);
maint: improve VIR_ERR_NO_SUPPORT usage We weren't very consistent in our use of VIR_ERR_NO_SUPPORT; many users just passed __FUNCTION__ on, while others passed "%s" to silence over-eager compilers that warn about __FUNCTION__ not containing any %. It's nicer to route all these uses through a single macro, so that if we ever need to change the reporting, we can do it in one place. I verified that 'virsh -c test:///default qemu-monitor-command test foo' gives the same error message before and after this patch: error: this function is not supported by the connection driver: virDomainQemuMonitorCommand Note that in libvirt.c, we were inconsistent on whether virDomain* API used virLibConnError() (with VIR_FROM_NONE) or virLibDomainError() (with VIR_FROM_DOMAIN); this patch unifies these errors to all use VIR_FROM_NONE, on the grounds that it is unlikely that a caller learning that a call is unimplemented can do anything in particular with extra knowledge of which error domain it belongs to. One particular change to note is virDomainOpenGraphics which was trying to fail with VIR_ERR_NO_SUPPORT after a failed VIR_DRV_SUPPORTS_FEATURE check; all other places that fail a feature check report VIR_ERR_ARGUMENT_UNSUPPORTED. * src/util/virerror.h (virReportUnsupportedError): New macro. * src/libvirt-qemu.c: Use new macro. * src/libvirt-lxc.c: Likewise. * src/lxc/lxc_driver.c: Likewise. * src/security/security_manager.c: Likewise. * src/util/virinitctl.c: Likewise. * src/libvirt.c: Likewise. (virDomainOpenGraphics): Use correct error for unsupported feature. Signed-off-by: Eric Blake <eblake@redhat.com>
2013-12-20 01:38:59 +00:00
void virReportOOMErrorFull(int domcode,
const char *filename,
const char *funcname,
size_t linenr);
#define virReportOOMError() \
virReportOOMErrorFull(VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
#define virReportError(code, ...) \
virReportErrorHelper(VIR_FROM_THIS, code, __FILE__, \
__FUNCTION__, __LINE__, __VA_ARGS__)
#define virReportErrorObject(obj) \
virRaiseErrorObject(__FILE__, __FUNCTION__, __LINE__, obj)
int virSetError(virErrorPtr newerr);
virErrorPtr virErrorCopyNew(virErrorPtr err);
void virDispatchError(virConnectPtr conn);
/* DEPRECATED: use g_strerror() directly */
const char *virStrerror(int theerrno, char *errBuf, size_t errBufLen);
2009-01-20 12:01:45 +00:00
typedef int (*virErrorLogPriorityFunc)(virErrorPtr, int);
void virSetErrorLogPriorityFunc(virErrorLogPriorityFunc func);
void virErrorSetErrnoFromLastError(void);
bool virLastErrorIsSystemErrno(int errnum);
void virErrorPreserveLast(virErrorPtr *saveerr);
void virErrorRestore(virErrorPtr *savederr);
void virLastErrorPrefixMessage(const char *fmt, ...)
G_GNUC_PRINTF(1, 2);
VIR_DEFINE_AUTOPTR_FUNC(virError, virFreeError);