Commit 82be389f authored by Christian Kellner's avatar Christian Kellner

common: helper to create on GError for errno

Like g_set_error but will create a G_IO_ERROR with the error code
derived via g_io_error_from_errno. Returns TRUE if errno is 0 and
will not do anything if error is NULL.
NB: will not create a error message from the errno, but the '%m'
extension should work (and is tested for).
parent 7cee7b7e
......@@ -125,3 +125,30 @@ bolt_error_propagate_stripped (GError **dest,
g_propagate_error (dest, g_steal_pointer (source));
return FALSE;
bolt_error_for_errno (GError **error,
gint err_no,
const char *format,
va_list ap;
int code;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
g_return_val_if_fail (format != NULL, FALSE);
if (err_no == 0)
return TRUE;
if (error == NULL)
return FALSE;
code = g_io_error_from_errno (err_no);
va_start (ap, format);
*error = g_error_new_valist (G_IO_ERROR, code, format, ap);
va_end (ap);
return FALSE;
......@@ -62,4 +62,8 @@ gboolean bolt_error_propagate (GError **dest,
gboolean bolt_error_propagate_stripped (GError **dest,
GError **source);
gboolean bolt_error_for_errno (GError **error,
gint err_no,
const char *format,
...) G_GNUC_PRINTF (3, 4);
......@@ -38,6 +38,7 @@
#include <gio/gio.h>
#include <glib/gprintf.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <string.h>
......@@ -158,6 +159,7 @@ test_enums (TestRng *tt, gconstpointer user_data)
static void
test_error (TestRng *tt, gconstpointer user_data)
g_autoptr(GError) error = NULL;
g_autoptr(GError) failed = NULL;
g_autoptr(GError) notfound = NULL;
g_autoptr(GError) exists = NULL;
......@@ -265,6 +267,30 @@ test_error (TestRng *tt, gconstpointer user_data)
g_assert_error (target, BOLT_ERROR, BOLT_ERROR_BADKEY);
g_assert_false (g_dbus_error_is_remote_error (target));
g_assert_cmpstr (target->message, ==, buserr->message);
/* bolt_error_for_errno */
ok = bolt_error_for_errno (NULL, 0, "no error!");
g_assert_true (ok);
ok = bolt_error_for_errno (&error, 0, "no error!");
g_assert_no_error (error);
g_assert_true (ok);
ok = bolt_error_for_errno (NULL, ENOENT, "no such thing");
g_assert_false (ok);
ok = bolt_error_for_errno (&error, ENOENT, "no such thing");
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
g_assert_true (g_str_has_prefix (error->message, "no such thing"));
g_assert_false (ok);
g_clear_pointer (&error, g_error_free);
ok = bolt_error_for_errno (&error, ENOENT, "%m");
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
g_debug ("ENOENT formatted via %%m is '%s'", error->message);
g_assert_true (strlen (error->message) > 0);
g_assert_false (ok);
g_clear_pointer (&error, g_error_free);
static void
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment