1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-25 01:34:11 +03:00

util: error: Add API for prefixing last set error with a string

In some cases we report a low level error message which does not have
enough information to see what the problem is. To allow improving on
this add an API which will prefix the error message with another error
message string which can be used to describe where the error comes from.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
ACKed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2019-06-11 11:23:48 +02:00
parent 3203681e5e
commit 5fa5cc37fe
4 changed files with 43 additions and 0 deletions

1
cfg.mk
View File

@ -614,6 +614,7 @@ msg_gen_function += virReportError
msg_gen_function += virReportErrorHelper
msg_gen_function += virReportSystemError
msg_gen_function += xenapiSessionErrorHandler
msg_gen_function += virLastErrorPrefixMessage
# Uncomment the following and run "make syntax-check" to see diagnostics
# that are not yet marked for translation, but that need to be rewritten

View File

@ -1832,6 +1832,7 @@ virErrorPreserveLast;
virErrorRestore;
virErrorSetErrnoFromLastError;
virLastErrorIsSystemErrno;
virLastErrorPrefixMessage;
virRaiseErrorFull;
virRaiseErrorObject;
virReportErrorHelper;

View File

@ -1461,3 +1461,41 @@ bool virLastErrorIsSystemErrno(int errnum)
return false;
return true;
}
/**
* virLastErrorPrefixMessage:
* @fmt: printf-style formatting string
* @...: Arguments for @fmt
*
* Prefixes last error reported with message formatted from @fmt. This is useful
* if the low level error message does not convey enough information to describe
* the problem.
*/
void
virLastErrorPrefixMessage(const char *fmt, ...)
{
int save_errno = errno;
virErrorPtr err = virGetLastError();
VIR_AUTOFREE(char *) fmtmsg = NULL;
VIR_AUTOFREE(char *) newmsg = NULL;
va_list args;
if (!err)
return;
va_start(args, fmt);
if (virVasprintfQuiet(&fmtmsg, fmt, args) < 0)
goto cleanup;
if (virAsprintfQuiet(&newmsg, "%s: %s", fmtmsg, err->message) < 0)
goto cleanup;
VIR_FREE(err->message);
VIR_STEAL_PTR(err->message, newmsg);
cleanup:
va_end(args);
errno = save_errno;
}

View File

@ -205,4 +205,7 @@ bool virLastErrorIsSystemErrno(int errnum);
void virErrorPreserveLast(virErrorPtr *saveerr);
void virErrorRestore(virErrorPtr *savederr);
void virLastErrorPrefixMessage(const char *fmt, ...)
ATTRIBUTE_FMT_PRINTF(1, 2);
VIR_DEFINE_AUTOPTR_FUNC(virError, virFreeError);