summaryrefslogtreecommitdiff
path: root/glnx-errors.h
diff options
context:
space:
mode:
authorJonathan Lebon <jlebon@redhat.com>2017-05-11 10:48:30 -0400
committerColin Walters <walters@verbum.org>2017-05-11 12:34:02 -0400
commit32231fdb5273dd2a812c61549d6c0e681c0f5d59 (patch)
tree03eefc7ec8603681f34a3bd7f90501e30fd3b71c /glnx-errors.h
parent3a4d0f4684f4653338c4756c8a1abfc6b5738467 (diff)
downloadlibglnx-32231fdb5273dd2a812c61549d6c0e681c0f5d59.tar.gz
glnx-errors.h: add a glnx_throw_prefix() variant
For completeness. It just looks much cleaner than doing the `, FALSE` trick. It also takes care of appending the ': ' for you like its errno version.
Diffstat (limited to 'glnx-errors.h')
-rw-r--r--glnx-errors.h37
1 files changed, 34 insertions, 3 deletions
diff --git a/glnx-errors.h b/glnx-errors.h
index 2f6b962..e26a513 100644
--- a/glnx-errors.h
+++ b/glnx-errors.h
@@ -30,7 +30,7 @@ G_BEGIN_DECLS
* 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);
* ```
@@ -49,16 +49,47 @@ glnx_throw (GError **error, const char *fmt, ...)
return FALSE;
}
-/* Like glnx_throw(), but yields a NULL pointer. */
+/* Like `glnx_throw ()`, but returns %NULL. */
#define glnx_null_throw(error, args...) \
({glnx_throw (error, args); NULL;})
+/* Implementation detail of glnx_throw_prefix() */
+void glnx_real_set_prefix_error_va (GError *error,
+ const char *format,
+ va_list args) G_GNUC_PRINTF (2,0);
+
+/* Prepend to @error's message by `$prefix: ` where `$prefix` is computed via
+ * printf @fmt. Returns %FALSE so it can be used conveniently in a single
+ * statement:
+ *
+ * ```
+ * if (!function_that_fails (s, error))
+ * return glnx_throw_prefix (error, "while handling '%s'", s);
+ * ```
+ * */
+static inline gboolean G_GNUC_PRINTF (2,3)
+glnx_prefix_error (GError **error, const char *fmt, ...)
+{
+ if (error == NULL)
+ return FALSE;
+
+ va_list args;
+ va_start (args, fmt);
+ glnx_real_set_prefix_error_va (*error, fmt, args);
+ va_end (args);
+ return FALSE;
+}
+
+/* Like `glnx_prefix_error ()`, but returns %NULL. */
+#define glnx_prefix_error_null(error, args...) \
+ ({glnx_prefix_error (error, args); NULL;})
+
/* Set @error using the value of `g_strerror (errno)`.
*
* This function returns %FALSE so it can be used conveniently in a single
* statement:
*
- * ``
+ * ```
* if (unlinkat (fd, somepathname) < 0)
* return glnx_throw_errno (error);
* ```