From 5df04434f7c75d7d2045c4d0563fe769a42b5c57 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Fri, 19 Mar 2010 23:03:33 +0000 Subject: [PATCH] More robust error display and handling. Handle all cases where a function may return a failure with a NULL GError, such as preconditions and warnings. Also use proper function for checking error codes. --- egg/egg-error.h | 34 ++++++++++++++++++++++++++++++++++ gcr/gcr-library.c | 3 ++- gcr/tests/unit-test-parser.c | 3 ++- gp11/gp11-module.c | 2 +- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 egg/egg-error.h diff --git a/egg/egg-error.h b/egg/egg-error.h new file mode 100644 index 0000000..50f459c --- /dev/null +++ b/egg/egg-error.h @@ -0,0 +1,34 @@ +/* + * gnome-keyring + * + * Copyright (C) 2010 Stefan Walter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General License for more details. + * + * You should have received a copy of the GNU Lesser General + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +#ifndef EGG_ERROR_H_ +#define EGG_ERROR_H_ + +#include + +static inline const gchar* +egg_error_message (GError *error) +{ + g_return_val_if_fail (error, "(unknown)"); + return error->message ? error->message : "(null)"; +} + +#endif /* EGG_ERROR_H_ */ diff --git a/gcr/gcr-library.c b/gcr/gcr-library.c index c7e7f0c..2eb6a8d 100644 --- a/gcr/gcr-library.c +++ b/gcr/gcr-library.c @@ -25,6 +25,7 @@ #include "gcr-types.h" #include "gcr-internal.h" +#include "egg/egg-error.h" #include "egg/egg-libgcrypt.h" #include "egg/egg-secure-memory.h" @@ -127,7 +128,7 @@ _gcr_initialize (void) all_modules = g_list_prepend (all_modules, module); } else { g_message ("couldn't initialize PKCS#11 module: %s", - error && error->message ? error->message : ""); + egg_error_message (error)); } g_once_init_leave (&gcr_initialized, 1); diff --git a/gcr/tests/unit-test-parser.c b/gcr/tests/unit-test-parser.c index 49cd544..4a2cfdd 100644 --- a/gcr/tests/unit-test-parser.c +++ b/gcr/tests/unit-test-parser.c @@ -25,6 +25,7 @@ #include "run-auto-test.h" +#include "egg/egg-error.h" #include "egg/egg-secure-memory.h" #include "gcr/gcr-parser.h" @@ -129,7 +130,7 @@ DEFINE_TEST(parse_all) result = gcr_parser_parse_data (parser, contents, len, &err); if (!result) { g_warning ("couldn't parse file data: %s: %s", - filename, err && err->message ? err->message : ""); + filename, egg_error_message (err)); g_error_free (err); g_assert (FALSE); } diff --git a/gp11/gp11-module.c b/gp11/gp11-module.c index 347fffc..ac1d619 100644 --- a/gp11/gp11-module.c +++ b/gp11/gp11-module.c @@ -1172,7 +1172,7 @@ gp11_module_enumerate_objects_full (GP11Module *self, GP11Attributes *attrs, g_return_val_if_fail (error != NULL, FALSE); /* Ignore these errors when enumerating */ - if (error->code == CKR_USER_PIN_NOT_INITIALIZED) { + if (g_error_matches (error, GP11_ERROR, CKR_USER_PIN_NOT_INITIALIZED)) { g_clear_error (&error); } else { -- 2.7.4