/** * virDispatchError: * @conn: pointer to the hypervisor connection * * Internal helper to do final stage of error * reporting in public APIs. * * - Copy the global error to per-connection error if needed * - Set a generic error message if none is already set * - Invoke the error callback functions */ void virDispatchError(virConnectPtr conn) { virErrorPtr err = virLastErrorObject(); virErrorFunc handler = virErrorHandler; void *userData = virUserData; /* Can only happen on OOM. */ if (!err) return; /* Set a generic error message if none is already set */ if (err->code == VIR_ERR_OK) virErrorGenericFailure(err); /* Copy the global error to per-connection error if needed */ if (conn) { virMutexLock(&conn->lock); virCopyError(err, &conn->err); if (conn->handler != NULL) { handler = conn->handler; userData = conn->userData; } virMutexUnlock(&conn->lock); } /* Invoke the error callback functions */ if (handler != NULL) { (handler)(userData, err); } else { virDefaultErrorFunc(err); } }
/** * virResetLastError: * * Reset the last error caught at the library level. * * The error object is kept in thread local storage, so separate * threads can safely access this concurrently, only resetting * their own error object. */ void virResetLastError(void) { virErrorPtr err = virLastErrorObject(); if (err) virResetError(err); }
/** * virGetLastError: * * Provide a pointer to the last error caught at the library level * * The error object is kept in thread local storage, so separate * threads can safely access this concurrently. * * Returns a pointer to the last error or NULL if none occurred. */ virErrorPtr virGetLastError(void) { virErrorPtr err = virLastErrorObject(); if (!err || err->code == VIR_ERR_OK) return NULL; return err; }
/** * 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; }
/** * 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; }
/** * virGetLastErrorMessage: * * Get the most recent error message * * Returns the most recent error message string in this * thread, or a generic message if none is set */ const char * virGetLastErrorMessage(void) { virErrorPtr err = virLastErrorObject(); if (!err || err->code == VIR_ERR_OK) return _("no error"); if (err->message == NULL) return _("unknown error"); return err->message; }
/** * virCopyLastError: * @to: target to receive the copy * * Copy the content of the last error caught at the library level * * The error object is kept in thread local storage, so separate * threads can safely access this concurrently. * * One will need to free the result with virResetError() * * Returns 0 if no error was found and the error code otherwise and -1 in case * of parameter error. */ int virCopyLastError(virErrorPtr to) { virErrorPtr err = virLastErrorObject(); /* We can't guarantee caller has initialized it to zero */ memset(to, 0, sizeof(*to)); if (err) virCopyError(err, to); else virResetError(to); return to->code; }
/** * virSetError: * @newerr: previously saved error object * * Set the current error from a previously saved error object * * Can be used to re-set an old error, which may have been squashed by * other functions (like cleanup routines). * * Returns 0 on success, -1 on failure. Leaves errno unchanged. */ int virSetError(virErrorPtr newerr) { virErrorPtr err; int saved_errno = errno; int ret = -1; err = virLastErrorObject(); if (!err) goto cleanup; virResetError(err); ret = virCopyError(newerr, err); cleanup: errno = saved_errno; return ret; }
/** * virCopyLastError: * @to: target to receive the copy * * Copy the content of the last error caught at the library level * * The error object is kept in thread local storage, so separate * threads can safely access this concurrently. * * One will need to free the result with virResetError() * * Returns error code or -1 in case of parameter error. */ int virCopyLastError(virErrorPtr to) { virErrorPtr err = virLastErrorObject(); if (!to) return -1; /* We can't guarantee caller has initialized it to zero */ memset(to, 0, sizeof(*to)); if (err) { virCopyError(err, to); } else { virResetError(to); to->code = VIR_ERR_NO_MEMORY; to->domain = VIR_FROM_NONE; to->level = VIR_ERR_ERROR; } return to->code; }
const char *str3, int int1, int int2, const char *fmt, ...) { int save_errno = errno; virErrorPtr to; char *str; int priority; /* * All errors are recorded in thread local storage * For compatibility, public API calls will copy them * to the per-connection error object when necessary */ to = virLastErrorObject(); if (!to) { errno = save_errno; return; /* Hit OOM allocating thread error object, sod all we can do now */ } virResetError(to); if (code == VIR_ERR_OK) { errno = save_errno; return; } /* * formats the message; drop message on OOM situations */