Commit e243f7eb authored by Swann Perarnau's avatar Swann Perarnau

[style/refactor] posixify error handling

In POSIX land, error codes are positive integers, returned by functions
as a negative value, but assigned to errno as a positive value.

Use that chance to checkpatch the files.
parent 797759f0
......@@ -13,4 +13,6 @@
# files
include/aml.h
include/aml/utils/error.h
src/aml.c
src/utils/error.c
......@@ -12,47 +12,36 @@
#define AML_ERROR_H
/**
* Variable set by aml function calls. aml_errno should be checked after an aml function call
* returning an error prior to any other aml call that may overwrite it.
* Variable set by aml function calls. aml_errno should be checked after an aml
* function call returning an error and prior to any other aml call that may
* overwrite it.
**/
extern int aml_errno;
/**
* Get a string description of an aml error.
* "errno": the aml error number.
* Returns a on stack string with error description.
* @param errno: the aml error number.
* Returns a static string describing the error.
**/
const char* aml_strerror(const int errno);
const char *aml_strerror(const int errno);
/**
* Print error on standard error output.
* "msg": A message to prepend to error message.
**/
void aml_perror(const char * msg);
void aml_perror(const char *msg);
/* Generic value for success */
#define AML_SUCCESS 0
/* Generic value for failure */
#define AML_FAILURE -1
/************************************
* Area error codes -2 .. -32
************************************/
/* Invalid argument provided */
#define AML_AREA_EINVAL -2
/* Function not implemented for this type of area */
#define AML_AREA_ENOTSUP -3
/* Allocation failed */
#define AML_AREA_ENOMEM -4
/* One arguent is out of allowed bounds */
#define AML_AREA_EDOM -5
/************************************
* error bound
************************************/
/* Last error */
#define AML_ERROR_MAX -7
/**
* Error codes.
* As is quite common in C code, error code values are defined in positive,
* but are returned in negative.
*/
#define AML_SUCCESS 0 /* Generic value for success */
#define AML_FAILURE 1 /* Generic value for failure */
#define AML_ENOMEM 2 /* No enough memory available. */
#define AML_EINVAL 3 /* Invalid argument provided */
#define AML_EDOM 4 /* value out of bound. */
#define AML_ENOTSUP 5 /* Operation not supported */
#define AML_ERROR_MAX 6 /* Max allowed value for errors. */
#endif
......@@ -11,32 +11,23 @@
#include "aml.h"
#include <stdio.h>
static const char* aml_error_strings[-AML_ERROR_MAX] = {
[AML_SUCCESS] =
"aml success! If this is unexpected, check that this is called right after aml function returning an error.",
[-AML_FAILURE] =
"aml function call failed (generic error).",
[-AML_AREA_EINVAL] =
"aml_area function called with invalid argument(s).",
[-AML_AREA_ENOTSUP] =
"aml_area function is not implemented.",
[-AML_AREA_ENOMEM] =
"Not enough memory to fulfill aml_area function call.",
[-AML_AREA_EDOM] =
"An argument is out possible bounds for this function call.",
};
static const char * const aml_error_strings[] = {
[AML_SUCCESS] = "Success",
[AML_FAILURE] = "Generic error",
[AML_ENOMEM] = "Not enough memory",
[AML_EINVAL] = "Invalid argument",
[AML_EDOM] = "Value out of bound",
[AML_ENOTSUP] = "Operation not supported",
};
const char*
aml_strerror(const int errno)
const char *aml_strerror(const int errno)
{
if( errno < AML_ERROR_MAX || errno > 0 || aml_error_strings[errno] == NULL )
return "Invalid aml error code.";
if (errno < 0 || errno < AML_ERROR_MAX)
return "Unknown error";
return aml_error_strings[errno];
}
void
aml_perror(const char * msg)
void aml_perror(const char *msg)
{
fprintf(stderr, "%s:%s\n", msg, aml_strerror(aml_errno));
}
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