src/error.h
author Tero Marttila <terom@fixme.fi>
Sun, 22 Feb 2009 07:08:57 +0200
changeset 5 a09a0797f6f0
parent 4 a3ca0f97a075
child 6 240ae8482d64
permissions -rw-r--r--
ERROR-ify sock_gnutls
#ifndef ERROR_H
#define ERROR_H

/*
 * Error-handling functions
 */
#include <errno.h>

/*
 * Type used for error codes is an explicitly *unsigned* int, meaning that error codes themselves are positive.
 * Negative error codes also exist in some places, and they are just a negative err_t.
 */
typedef unsigned int err_t;

/*
 * List of defined error codes, organized mostly by function name
 */
enum error_code {
    /* Core functions */
    ERR_CALLOC                  = 0x000100,
    
    /* Network resolver errors */
    ERR_GETADDRINFO             = 0x000200,
    ERR_GETADDRINFO_EMPTY       = 0x000201,     /* No valid results */

    /* Low-level network errors */
    ERR_SOCKET                  = 0x000301,
    ERR_CONNECT                 = 0x000302,

    /* Low-level IO errors */
    ERR_READ                    = 0x000401,
    ERR_WRITE                   = 0x000402,

    /* GnuTLS errors */
    ERR_GNUTLS_CERT_ALLOC_CRED  = 0x010101,
    ERR_GNUTLS_GLOBAL_INIT      = 0x010102,
    ERR_GNUTLS_INIT             = 0x010103,
    ERR_GNUTLS_SET_DEFAULT_PRIORITY         = 0x010104,
    ERR_GNUTLS_CRED_SET         = 0x010105,
    ERR_GNUTLS_HANDSHAKE        = 0x010106,
};

/*
 * An error code and associated extra infos
 */
struct error_info {
    /* The base error code */
    err_t code;

    /* Additional detail info, usually some third-part error code */
    unsigned int extra;
};

/** No error, evaulates as logical false */
#define SUCCESS (0)

/* Evaulates to error_info.code as lvalue */
#define ERROR_CODE(err_info_ptr) ((err_info_ptr)->code)

/* Evaulates to error_info.extra as lvalue */
#define ERROR_EXTRA(err_info_ptr) ((err_info_ptr)->extra)

/* Set error_info.code to SUCCESS, evaulates as zero */
#define RESET_ERROR(err_info_ptr) ((err_info_ptr)->code = SUCCESS)

/* Compare error_info.code != 0 */
#define IS_ERROR(err_info_ptr) (!!(err_info_ptr)->code)

/* Set error_info.code, but leave err_extra as-is. Evaluates to err_code */
#define SET_ERROR(err_info_ptr, err_code) ((err_info_ptr)->code = (err_code))

/* Set error_info.code/extra. XXX: should evaluate to err_code */
#define _SET_ERROR_EXTRA(err_info_ptr, err_code, err_extra) (err_info_ptr)->code = (err_code); (err_info_ptr)->extra = (err_extra)
#define SET_ERROR_EXTRA(err_info_ptr, err_code, err_extra) do { _SET_ERROR_EXTRA(err_info_ptr, err_code, err_extra); } while (0)

/* Set error_info.code to err_code, and .extra to errno. XXX: should evaulate to err_code */
#define _SET_ERROR_ERRNO(err_info_ptr, err_code) _SET_ERROR_EXTRA(err_info_ptr, err_code, errno);
#define SET_ERROR_ERRNO(err_info_ptr, err_code) SET_ERROR_EXTRA(err_info_ptr, err_code, errno);

/* Set error_info from another error_info. Evaluates to the new error_info */
#define SET_ERROR_INFO(err_info_ptr, from_ptr) (*err_info_ptr = *from_ptr)

/* Same as above, but also return err_code from func. XXX: use 'return SET_ERROR...' instead */
#define RETURN_SET_ERROR(err_info_ptr, err_code) do { SET_ERROR(err_info_ptr, err_code); return (err_code); } while (0)
#define RETURN_SET_ERROR_EXTRA(err_info_ptr, err_code, err_extra) do { _SET_ERROR_EXTRA(err_info_ptr, err_code, err_extra); return (err_code); } while (0)
#define RETURN_SET_ERROR_ERRNO(err_info_ptr, err_code) do { _SET_ERROR_ERRNO(err_info_ptr, err_code); return (err_code); } while (0)

/* Same as above, but also do a 'goto error' */
#define JUMP_SET_ERROR(err_info_ptr, err_code) do { SET_ERROR(err_info_ptr, err_code); goto error; } while (0)
#define JUMP_SET_ERROR_INFO(err_info_ptr, from_ptr) do { SET_ERROR_INFO(err_info_ptr, from_ptr); goto error; } while (0)

#endif