src/error.h
changeset 7 844f014409ff
parent 6 240ae8482d64
child 8 be88e543c8ff
equal deleted inserted replaced
6:240ae8482d64 7:844f014409ff
    11  * Negative error codes also exist in some places, and they are just a negative err_t.
    11  * Negative error codes also exist in some places, and they are just a negative err_t.
    12  */
    12  */
    13 typedef unsigned int err_t;
    13 typedef unsigned int err_t;
    14 
    14 
    15 /*
    15 /*
       
    16  * Bitmask of error_info.extra meanings
       
    17  */
       
    18 enum error_extra_types {
       
    19     // bit offset of ERR_EXTRA_* mask in error_code
       
    20     _ERR_EXTRA_OFFSET   = 3 * 8,
       
    21     
       
    22     // mask of bits used for the error_extra_types value
       
    23     _ERR_EXTRA_MASK     = 0xff << _ERR_EXTRA_OFFSET,
       
    24 
       
    25     ERR_EXTRA_NONE      = 0x00 << _ERR_EXTRA_OFFSET,
       
    26     ERR_EXTRA_ERRNO     = 0x01 << _ERR_EXTRA_OFFSET,
       
    27     ERR_EXTRA_GAI       = 0x02 << _ERR_EXTRA_OFFSET,
       
    28     ERR_EXTRA_GNUTLS    = 0x03 << _ERR_EXTRA_OFFSET,
       
    29     
       
    30 };
       
    31 
       
    32 
       
    33 #define _ERROR_CODE(name, code, extra) name = (code | ERR_EXTRA_ ## extra)
       
    34 /*
    16  * List of defined error codes, organized mostly by function name
    35  * List of defined error codes, organized mostly by function name
    17  */
    36  */
    18 enum error_code {
    37 enum error_code {
    19     /* Core functions */
    38     /* Core functions */
    20     ERR_CALLOC                  = 0x000100,
    39     _ERROR_CODE( ERR_CALLOC,                        0x000100,   NONE    ),
    21     
    40     
    22     /* Network resolver errors */
    41     /* Network resolver errors */
    23     ERR_GETADDRINFO             = 0x000200,
    42     _ERROR_CODE( ERR_GETADDRINFO,                   0x000200,   GAI     ),
    24     ERR_GETADDRINFO_EMPTY       = 0x000201,     /* No valid results */
    43     _ERROR_CODE( ERR_GETADDRINFO_EMPTY,             0x000201,   GAI     ),
    25 
    44 
    26     /* Low-level network errors */
    45     /* Low-level network errors */
    27     ERR_SOCKET                  = 0x000301,
    46     _ERROR_CODE( ERR_SOCKET,                        0x000301,   ERRNO   ),
    28     ERR_CONNECT                 = 0x000302,
    47     _ERROR_CODE( ERR_CONNECT,                       0x000302,   ERRNO   ),
    29 
    48 
    30     /* Low-level IO errors */
    49     /* Low-level IO errors */
    31     ERR_READ                    = 0x000401,
    50     _ERROR_CODE( ERR_READ,                          0x000401,   ERRNO   ),
    32     ERR_WRITE                   = 0x000402,
    51     _ERROR_CODE( ERR_WRITE,                         0x000402,   ERRNO   ),
    33 
    52 
    34     /* GnuTLS errors */
    53     /* GnuTLS errors */
    35     ERR_GNUTLS_CERT_ALLOC_CRED  = 0x010101,
    54     _ERROR_CODE( ERR_GNUTLS_CERT_ALLOC_CRED,        0x010101,   GNUTLS  ),
    36     ERR_GNUTLS_GLOBAL_INIT      = 0x010102,
    55     _ERROR_CODE( ERR_GNUTLS_GLOBAL_INIT,            0x010102,   GNUTLS  ),
    37     ERR_GNUTLS_INIT             = 0x010103,
    56     _ERROR_CODE( ERR_GNUTLS_INIT,                   0x010103,   GNUTLS  ),
    38     ERR_GNUTLS_SET_DEFAULT_PRIORITY         = 0x010104,
    57     _ERROR_CODE( ERR_GNUTLS_SET_DEFAULT_PRIORITY,   0x010104,   GNUTLS  ),
    39     ERR_GNUTLS_CRED_SET         = 0x010105,
    58     _ERROR_CODE( ERR_GNUTLS_CRED_SET,               0x010105,   GNUTLS  ),
    40     ERR_GNUTLS_HANDSHAKE        = 0x010106,
    59     _ERROR_CODE( ERR_GNUTLS_HANDSHAKE,              0x010106,   GNUTLS  ),
       
    60     
       
    61     // mask of bits used for the error_code value
       
    62     _ERROR_CODE_MASK    = 0xffffff,
    41 };
    63 };
    42 
    64 
    43 /*
    65 /*
    44  * An error code and associated extra infos
    66  * An error code and associated extra infos
    45  */
    67  */
    50     /* Additional detail info, usually some third-part error code */
    72     /* Additional detail info, usually some third-part error code */
    51     unsigned int extra;
    73     unsigned int extra;
    52 };
    74 };
    53 
    75 
    54 /*
    76 /*
    55  * Translate an err_t into a string.
    77  * Translate an err_t into a function name.
    56  */
    78  */
    57 const char *error_name (err_t code);
    79 const char *error_name (err_t code);
       
    80 
       
    81 /*
       
    82  * Maximum length of error messages returned by error_msg (including NUL byte)
       
    83  */
       
    84 #define ERROR_MSG_MAXLEN 1024
       
    85 
       
    86 /*
       
    87  * Translate an error_info into a message.
       
    88  *
       
    89  * This is returned as a pointer into a statically allocated buffer. It is not re-entrant.
       
    90  */
       
    91 const char *error_msg (struct error_info *err);
    58 
    92 
    59 /** No error, evaulates as logical false */
    93 /** No error, evaulates as logical false */
    60 #define SUCCESS (0)
    94 #define SUCCESS (0)
    61 
    95 
    62 /* Evaulates to error_info.code as lvalue */
    96 /* Evaulates to error_info.code as lvalue */