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 f7fb68755c)
This commit is contained in:
Thomas Haller 2015-06-15 15:50:59 +02:00
parent 0def11cf47
commit 8fbd2d84e8
3 changed files with 53 additions and 27 deletions

View file

@ -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"

View file

@ -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 <errno.h> 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);

View file

@ -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;