From 8fbd2d84e89d523bba03f186e37798b8e709780f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 15 Jun 2015 15:50:59 +0200 Subject: [PATCH] platform: rework NMPlatformError codes - rename "NONE" to "SUCCESS", what it really is. - change the to-string result not to contain spaces and being closer the name of the enum value. - add new error reasons "UNSPECIFIED" and "BUG". - remove the code comments around the enum definition. They add no further description about why this error happens and only paraphrase the name of the enum. - reserve negative integers for 'errno'. This is neat because if we get a system error we can pass on the underlying errno as cause. (cherry picked from commit f7fb68755c30d984aa844ecadbc2eb8715e2ecc8) --- src/platform/nm-platform.c | 56 ++++++++++++++++++++++---------- src/platform/nm-platform.h | 22 ++++++++----- src/platform/tests/test-common.h | 2 +- 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 2335115b9d..9a37f712bf 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -173,11 +173,46 @@ void nm_platform_set_error (NMPlatform *self, NMPlatformError error) NMPlatformError nm_platform_get_error (NMPlatform *self) { - _CHECK_SELF (self, klass, NM_PLATFORM_ERROR_NONE); + _CHECK_SELF (self, klass, NM_PLATFORM_ERROR_BUG); return self->error; } +/** + * nm_platform_error_to_string: + * @error_code: the error code to stringify. + * + * Returns: A string representation of the error. + * For negative numbers, this function interprets + * the code as -errno. + */ +const char * +nm_platform_error_to_string (NMPlatformError error) +{ + switch (error) { + case NM_PLATFORM_ERROR_SUCCESS: + return "success"; + case NM_PLATFORM_ERROR_BUG: + return "bug"; + case NM_PLATFORM_ERROR_UNSPECIFIED: + return "unspecified"; + case NM_PLATFORM_ERROR_NOT_FOUND: + return "not-found"; + case NM_PLATFORM_ERROR_EXISTS: + return "exists"; + case NM_PLATFORM_ERROR_WRONG_TYPE: + return "wrong-type"; + case NM_PLATFORM_ERROR_NOT_SLAVE: + return "not-slave"; + case NM_PLATFORM_ERROR_NO_FIRMWARE: + return "no-firmware"; + default: + if (error < 0) + return g_strerror (- ((int) error)); + return "unknown"; + } +} + /** * nm_platform_get_error_message: * @self: platform instance @@ -189,29 +224,14 @@ nm_platform_get_error_msg (NMPlatform *self) { _CHECK_SELF (self, klass, NULL); - switch (self->error) { - case NM_PLATFORM_ERROR_NONE: - return "unknown error"; - case NM_PLATFORM_ERROR_NOT_FOUND: - return "object not found"; - case NM_PLATFORM_ERROR_EXISTS: - return "object already exists"; - case NM_PLATFORM_ERROR_WRONG_TYPE: - return "object is wrong type"; - case NM_PLATFORM_ERROR_NOT_SLAVE: - return "link not a slave"; - case NM_PLATFORM_ERROR_NO_FIRMWARE: - return "firmware not found"; - default: - return "invalid error number"; - } + return nm_platform_error_to_string (self->error); } static void reset_error (NMPlatform *self) { g_assert (self); - self->error = NM_PLATFORM_ERROR_NONE; + self->error = NM_PLATFORM_ERROR_SUCCESS; } #define IFA_F_MANAGETEMPADDR_STR "mngtmpaddr" diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index 8a3045d60d..d0283744f1 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -55,18 +55,23 @@ typedef struct _NMPlatform NMPlatform; #endif typedef enum { - /* no error specified, sometimes this means the arguments were wrong */ - NM_PLATFORM_ERROR_NONE, - /* object was not found */ + + /* dummy value, to enforce that the enum type is signed and has a size + * to hold an integer. We want to encode errno from as negative + * values. */ + _NM_PLATFORM_ERROR_MININT = G_MININT, + + NM_PLATFORM_ERROR_SUCCESS = 0, + + NM_PLATFORM_ERROR_BUG, + + NM_PLATFORM_ERROR_UNSPECIFIED, + NM_PLATFORM_ERROR_NOT_FOUND, - /* object already exists */ NM_PLATFORM_ERROR_EXISTS, - /* object is wrong type */ NM_PLATFORM_ERROR_WRONG_TYPE, - /* object is not a slave */ NM_PLATFORM_ERROR_NOT_SLAVE, - /* firmware is not found */ - NM_PLATFORM_ERROR_NO_FIRMWARE + NM_PLATFORM_ERROR_NO_FIRMWARE, } NMPlatformError; typedef enum { @@ -583,6 +588,7 @@ nm_platform_route_scope_inv (guint8 scope) const char *nm_link_type_to_string (NMLinkType link_type); +const char *nm_platform_error_to_string (NMPlatformError error); void nm_platform_set_error (NMPlatform *self, NMPlatformError error); NMPlatformError nm_platform_get_error (NMPlatform *self); const char *nm_platform_get_error_msg (NMPlatform *self); diff --git a/src/platform/tests/test-common.h b/src/platform/tests/test-common.h index 563d9fb4f6..5f72a1480d 100644 --- a/src/platform/tests/test-common.h +++ b/src/platform/tests/test-common.h @@ -16,7 +16,7 @@ #define debug(...) nm_log_dbg (LOGD_PLATFORM, __VA_ARGS__) #define error(err) g_assert (nm_platform_get_error (NM_PLATFORM_GET) == err) -#define no_error() error (NM_PLATFORM_ERROR_NONE) +#define no_error() error (NM_PLATFORM_ERROR_SUCCESS) typedef struct { int handler_id;