|
|
8fced6 |
From 439c11850165fd838e367aa6d4fff4af951a5bd9 Mon Sep 17 00:00:00 2001
|
|
|
8fced6 |
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
|
|
|
8fced6 |
Date: Wed, 16 Dec 2020 16:06:08 -0500
|
|
|
8fced6 |
Subject: [PATCH 07/14] error: Improve error.h's big comment
|
|
|
8fced6 |
MIME-Version: 1.0
|
|
|
8fced6 |
Content-Type: text/plain; charset=UTF-8
|
|
|
8fced6 |
Content-Transfer-Encoding: 8bit
|
|
|
8fced6 |
|
|
|
8fced6 |
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
|
|
|
8fced6 |
Message-id: <20201216160615.324213-4-marcandre.lureau@redhat.com>
|
|
|
8fced6 |
Patchwork-id: 100474
|
|
|
8fced6 |
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 03/10] error: Improve error.h's big comment
|
|
|
8fced6 |
Bugzilla: 1859494
|
|
|
8fced6 |
RH-Acked-by: Danilo de Paula <ddepaula@redhat.com>
|
|
|
8fced6 |
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
|
|
|
8fced6 |
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
8fced6 |
|
|
|
8fced6 |
From: Markus Armbruster <armbru@redhat.com>
|
|
|
8fced6 |
|
|
|
8fced6 |
Add headlines to the big comment.
|
|
|
8fced6 |
|
|
|
8fced6 |
Explain examples for NULL, &error_abort and &error_fatal argument
|
|
|
8fced6 |
better.
|
|
|
8fced6 |
|
|
|
8fced6 |
Tweak rationale for error_propagate_prepend().
|
|
|
8fced6 |
|
|
|
8fced6 |
Signed-off-by: Markus Armbruster <armbru@redhat.com>
|
|
|
8fced6 |
Message-Id: <20200707160613.848843-3-armbru@redhat.com>
|
|
|
8fced6 |
Reviewed-by: Eric Blake <eblake@redhat.com>
|
|
|
8fced6 |
Reviewed-by: Greg Kurz <groug@kaod.org>
|
|
|
8fced6 |
|
|
|
8fced6 |
(cherry picked from commit 9aac7d486cc792191c25c30851f501624b0c2751)
|
|
|
8fced6 |
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
|
|
|
8fced6 |
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
|
|
|
8fced6 |
---
|
|
|
8fced6 |
include/qapi/error.h | 51 +++++++++++++++++++++++++++++++-------------
|
|
|
8fced6 |
1 file changed, 36 insertions(+), 15 deletions(-)
|
|
|
8fced6 |
|
|
|
8fced6 |
diff --git a/include/qapi/error.h b/include/qapi/error.h
|
|
|
8fced6 |
index 83c38f9a188..3351fe76368 100644
|
|
|
8fced6 |
--- a/include/qapi/error.h
|
|
|
8fced6 |
+++ b/include/qapi/error.h
|
|
|
8fced6 |
@@ -15,6 +15,8 @@
|
|
|
8fced6 |
/*
|
|
|
8fced6 |
* Error reporting system loosely patterned after Glib's GError.
|
|
|
8fced6 |
*
|
|
|
8fced6 |
+ * = Creating errors =
|
|
|
8fced6 |
+ *
|
|
|
8fced6 |
* Create an error:
|
|
|
8fced6 |
* error_setg(&err, "situation normal, all fouled up");
|
|
|
8fced6 |
*
|
|
|
8fced6 |
@@ -27,6 +29,8 @@
|
|
|
8fced6 |
* error_setg(&err, "invalid quark\n" // WRONG!
|
|
|
8fced6 |
* "Valid quarks are up, down, strange, charm, top, bottom.");
|
|
|
8fced6 |
*
|
|
|
8fced6 |
+ * = Reporting and destroying errors =
|
|
|
8fced6 |
+ *
|
|
|
8fced6 |
* Report an error to the current monitor if we have one, else stderr:
|
|
|
8fced6 |
* error_report_err(err);
|
|
|
8fced6 |
* This frees the error object.
|
|
|
8fced6 |
@@ -40,6 +44,30 @@
|
|
|
8fced6 |
* error_free(err);
|
|
|
8fced6 |
* Note that this loses hints added with error_append_hint().
|
|
|
8fced6 |
*
|
|
|
8fced6 |
+ * Call a function ignoring errors:
|
|
|
8fced6 |
+ * foo(arg, NULL);
|
|
|
8fced6 |
+ * This is more concise than
|
|
|
8fced6 |
+ * Error *err = NULL;
|
|
|
8fced6 |
+ * foo(arg, &err;;
|
|
|
8fced6 |
+ * error_free(err); // don't do this
|
|
|
8fced6 |
+ *
|
|
|
8fced6 |
+ * Call a function aborting on errors:
|
|
|
8fced6 |
+ * foo(arg, &error_abort);
|
|
|
8fced6 |
+ * This is more concise and fails more nicely than
|
|
|
8fced6 |
+ * Error *err = NULL;
|
|
|
8fced6 |
+ * foo(arg, &err;;
|
|
|
8fced6 |
+ * assert(!err); // don't do this
|
|
|
8fced6 |
+ *
|
|
|
8fced6 |
+ * Call a function treating errors as fatal:
|
|
|
8fced6 |
+ * foo(arg, &error_fatal);
|
|
|
8fced6 |
+ * This is more concise than
|
|
|
8fced6 |
+ * Error *err = NULL;
|
|
|
8fced6 |
+ * foo(arg, &err;;
|
|
|
8fced6 |
+ * if (err) { // don't do this
|
|
|
8fced6 |
+ * error_report_err(err);
|
|
|
8fced6 |
+ * exit(1);
|
|
|
8fced6 |
+ * }
|
|
|
8fced6 |
+ *
|
|
|
8fced6 |
* Handle an error without reporting it (just for completeness):
|
|
|
8fced6 |
* error_free(err);
|
|
|
8fced6 |
*
|
|
|
8fced6 |
@@ -47,6 +75,11 @@
|
|
|
8fced6 |
* reporting it (primarily useful in testsuites):
|
|
|
8fced6 |
* error_free_or_abort(&err;;
|
|
|
8fced6 |
*
|
|
|
8fced6 |
+ * = Passing errors around =
|
|
|
8fced6 |
+ *
|
|
|
8fced6 |
+ * Errors get passed to the caller through the conventional @errp
|
|
|
8fced6 |
+ * parameter.
|
|
|
8fced6 |
+ *
|
|
|
8fced6 |
* Pass an existing error to the caller:
|
|
|
8fced6 |
* error_propagate(errp, err);
|
|
|
8fced6 |
* where Error **errp is a parameter, by convention the last one.
|
|
|
8fced6 |
@@ -54,11 +87,10 @@
|
|
|
8fced6 |
* Pass an existing error to the caller with the message modified:
|
|
|
8fced6 |
* error_propagate_prepend(errp, err,
|
|
|
8fced6 |
* "Could not frobnicate '%s': ", name);
|
|
|
8fced6 |
- *
|
|
|
8fced6 |
- * Avoid
|
|
|
8fced6 |
- * error_propagate(errp, err);
|
|
|
8fced6 |
+ * This is more concise than
|
|
|
8fced6 |
+ * error_propagate(errp, err); // don't do this
|
|
|
8fced6 |
* error_prepend(errp, "Could not frobnicate '%s': ", name);
|
|
|
8fced6 |
- * because this fails to prepend when @errp is &error_fatal.
|
|
|
8fced6 |
+ * and works even when @errp is &error_fatal.
|
|
|
8fced6 |
*
|
|
|
8fced6 |
* Create a new error and pass it to the caller:
|
|
|
8fced6 |
* error_setg(errp, "situation normal, all fouled up");
|
|
|
8fced6 |
@@ -70,15 +102,6 @@
|
|
|
8fced6 |
* handle the error...
|
|
|
8fced6 |
* }
|
|
|
8fced6 |
*
|
|
|
8fced6 |
- * Call a function ignoring errors:
|
|
|
8fced6 |
- * foo(arg, NULL);
|
|
|
8fced6 |
- *
|
|
|
8fced6 |
- * Call a function aborting on errors:
|
|
|
8fced6 |
- * foo(arg, &error_abort);
|
|
|
8fced6 |
- *
|
|
|
8fced6 |
- * Call a function treating errors as fatal:
|
|
|
8fced6 |
- * foo(arg, &error_fatal);
|
|
|
8fced6 |
- *
|
|
|
8fced6 |
* Receive an error and pass it on to the caller:
|
|
|
8fced6 |
* Error *err = NULL;
|
|
|
8fced6 |
* foo(arg, &err;;
|
|
|
8fced6 |
@@ -86,8 +109,6 @@
|
|
|
8fced6 |
* handle the error...
|
|
|
8fced6 |
* error_propagate(errp, err);
|
|
|
8fced6 |
* }
|
|
|
8fced6 |
- * where Error **errp is a parameter, by convention the last one.
|
|
|
8fced6 |
- *
|
|
|
8fced6 |
* Do *not* "optimize" this to
|
|
|
8fced6 |
* foo(arg, errp);
|
|
|
8fced6 |
* if (*errp) { // WRONG!
|
|
|
8fced6 |
--
|
|
|
8fced6 |
2.27.0
|
|
|
8fced6 |
|