summaryrefslogtreecommitdiff
path: root/glnx-errors.h
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2017-03-21 11:19:16 -0400
committerColin Walters <walters@verbum.org>2017-03-22 11:08:13 -0400
commit602fdd93cb7a339c6b5749eee73df926429a5ab8 (patch)
tree8e74035b3c3ee29bc132ed7b8065c90142d2091e /glnx-errors.h
parent074236b88d0c742965fcca25a235fc79d3f87e48 (diff)
downloadlibglnx-602fdd93cb7a339c6b5749eee73df926429a5ab8.tar.gz
errors: Add glnx_throw() and tests
Following up to the previous commit, also shorten our use of `g_set_error (..., G_IO_ERROR_FAILED, ...)`. There's a lot of this in libostree at least. See also https://bugzilla.gnome.org/show_bug.cgi?id=774061
Diffstat (limited to 'glnx-errors.h')
-rw-r--r--glnx-errors.h25
1 files changed, 25 insertions, 0 deletions
diff --git a/glnx-errors.h b/glnx-errors.h
index 7bf53d3..8f9cad8 100644
--- a/glnx-errors.h
+++ b/glnx-errors.h
@@ -25,6 +25,31 @@
G_BEGIN_DECLS
+/* Set @error with G_IO_ERROR/G_IO_ERROR_FAILED.
+ *
+ * This function returns %FALSE so it can be used conveniently in a single
+ * statement:
+ *
+ * ``
+ * if (strcmp (foo, "somevalue") != 0)
+ * return glnx_throw (error, "key must be somevalue, not '%s'", foo);
+ * ```
+ */
+static inline gboolean G_GNUC_PRINTF (2,3)
+glnx_throw (GError **error, const char *fmt, ...)
+{
+ if (error == NULL)
+ return FALSE;
+
+ va_list args;
+ va_start (args, fmt);
+ GError *new = g_error_new_valist (G_IO_ERROR, G_IO_ERROR_FAILED, fmt, args);
+ va_end (args);
+ g_propagate_error (error, g_steal_pointer (&new));
+ return FALSE;
+}
+
+
/* Set @error using the value of `g_strerror (errno)`.
*
* This function returns %FALSE so it can be used conveniently in a single