author | Tero Marttila <terom@fixme.fi> |
Sun, 22 Feb 2009 07:21:28 +0200 | |
changeset 6 | 240ae8482d64 |
parent 5 | a09a0797f6f0 |
child 7 | 844f014409ff |
permissions | -rw-r--r-- |
3 | 1 |
#ifndef ERROR_H |
2 |
#define ERROR_H |
|
3 |
||
4 |
/* |
|
5 |
* Error-handling functions |
|
6 |
*/ |
|
7 |
#include <errno.h> |
|
8 |
||
9 |
/* |
|
10 |
* Type used for error codes is an explicitly *unsigned* int, meaning that error codes themselves are positive. |
|
11 |
* Negative error codes also exist in some places, and they are just a negative err_t. |
|
12 |
*/ |
|
13 |
typedef unsigned int err_t; |
|
14 |
||
15 |
/* |
|
16 |
* List of defined error codes, organized mostly by function name |
|
17 |
*/ |
|
18 |
enum error_code { |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
19 |
/* Core functions */ |
3 | 20 |
ERR_CALLOC = 0x000100, |
21 |
||
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
22 |
/* Network resolver errors */ |
3 | 23 |
ERR_GETADDRINFO = 0x000200, |
24 |
ERR_GETADDRINFO_EMPTY = 0x000201, /* No valid results */ |
|
25 |
||
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
26 |
/* Low-level network errors */ |
3 | 27 |
ERR_SOCKET = 0x000301, |
28 |
ERR_CONNECT = 0x000302, |
|
29 |
||
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
30 |
/* Low-level IO errors */ |
3 | 31 |
ERR_READ = 0x000401, |
32 |
ERR_WRITE = 0x000402, |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
33 |
|
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
34 |
/* GnuTLS errors */ |
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
35 |
ERR_GNUTLS_CERT_ALLOC_CRED = 0x010101, |
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
36 |
ERR_GNUTLS_GLOBAL_INIT = 0x010102, |
5 | 37 |
ERR_GNUTLS_INIT = 0x010103, |
38 |
ERR_GNUTLS_SET_DEFAULT_PRIORITY = 0x010104, |
|
39 |
ERR_GNUTLS_CRED_SET = 0x010105, |
|
40 |
ERR_GNUTLS_HANDSHAKE = 0x010106, |
|
3 | 41 |
}; |
42 |
||
43 |
/* |
|
44 |
* An error code and associated extra infos |
|
45 |
*/ |
|
46 |
struct error_info { |
|
47 |
/* The base error code */ |
|
48 |
err_t code; |
|
49 |
||
50 |
/* Additional detail info, usually some third-part error code */ |
|
51 |
unsigned int extra; |
|
52 |
}; |
|
53 |
||
6 | 54 |
/* |
55 |
* Translate an err_t into a string. |
|
56 |
*/ |
|
57 |
const char *error_name (err_t code); |
|
58 |
||
3 | 59 |
/** No error, evaulates as logical false */ |
60 |
#define SUCCESS (0) |
|
61 |
||
62 |
/* Evaulates to error_info.code as lvalue */ |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
63 |
#define ERROR_CODE(err_info_ptr) ((err_info_ptr)->code) |
3 | 64 |
|
65 |
/* Evaulates to error_info.extra as lvalue */ |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
66 |
#define ERROR_EXTRA(err_info_ptr) ((err_info_ptr)->extra) |
3 | 67 |
|
68 |
/* Set error_info.code to SUCCESS, evaulates as zero */ |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
69 |
#define RESET_ERROR(err_info_ptr) ((err_info_ptr)->code = SUCCESS) |
3 | 70 |
|
71 |
/* Compare error_info.code != 0 */ |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
72 |
#define IS_ERROR(err_info_ptr) (!!(err_info_ptr)->code) |
3 | 73 |
|
74 |
/* Set error_info.code, but leave err_extra as-is. Evaluates to err_code */ |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
75 |
#define SET_ERROR(err_info_ptr, err_code) ((err_info_ptr)->code = (err_code)) |
3 | 76 |
|
77 |
/* Set error_info.code/extra. XXX: should evaluate to err_code */ |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
78 |
#define _SET_ERROR_EXTRA(err_info_ptr, err_code, err_extra) (err_info_ptr)->code = (err_code); (err_info_ptr)->extra = (err_extra) |
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
79 |
#define SET_ERROR_EXTRA(err_info_ptr, err_code, err_extra) do { _SET_ERROR_EXTRA(err_info_ptr, err_code, err_extra); } while (0) |
3 | 80 |
|
5 | 81 |
/* Set error_info.code to err_code, and .extra to errno. XXX: should evaulate to err_code */ |
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
82 |
#define _SET_ERROR_ERRNO(err_info_ptr, err_code) _SET_ERROR_EXTRA(err_info_ptr, err_code, errno); |
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
83 |
#define SET_ERROR_ERRNO(err_info_ptr, err_code) SET_ERROR_EXTRA(err_info_ptr, err_code, errno); |
3 | 84 |
|
5 | 85 |
/* Set error_info from another error_info. Evaluates to the new error_info */ |
86 |
#define SET_ERROR_INFO(err_info_ptr, from_ptr) (*err_info_ptr = *from_ptr) |
|
87 |
||
88 |
/* Same as above, but also return err_code from func. XXX: use 'return SET_ERROR...' instead */ |
|
89 |
#define RETURN_SET_ERROR(err_info_ptr, err_code) do { SET_ERROR(err_info_ptr, err_code); return (err_code); } while (0) |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
90 |
#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) |
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
91 |
#define RETURN_SET_ERROR_ERRNO(err_info_ptr, err_code) do { _SET_ERROR_ERRNO(err_info_ptr, err_code); return (err_code); } while (0) |
3 | 92 |
|
5 | 93 |
/* Same as above, but also do a 'goto error' */ |
94 |
#define JUMP_SET_ERROR(err_info_ptr, err_code) do { SET_ERROR(err_info_ptr, err_code); goto error; } while (0) |
|
95 |
#define JUMP_SET_ERROR_INFO(err_info_ptr, from_ptr) do { SET_ERROR_INFO(err_info_ptr, from_ptr); goto error; } while (0) |
|
96 |
||
3 | 97 |
#endif |