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-- |
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 |
||
54 |
/** No error, evaulates as logical false */ |
|
55 |
#define SUCCESS (0) |
|
56 |
||
57 |
/* 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
|
58 |
#define ERROR_CODE(err_info_ptr) ((err_info_ptr)->code) |
3 | 59 |
|
60 |
/* 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
|
61 |
#define ERROR_EXTRA(err_info_ptr) ((err_info_ptr)->extra) |
3 | 62 |
|
63 |
/* 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
|
64 |
#define RESET_ERROR(err_info_ptr) ((err_info_ptr)->code = SUCCESS) |
3 | 65 |
|
66 |
/* 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
|
67 |
#define IS_ERROR(err_info_ptr) (!!(err_info_ptr)->code) |
3 | 68 |
|
69 |
/* 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
|
70 |
#define SET_ERROR(err_info_ptr, err_code) ((err_info_ptr)->code = (err_code)) |
3 | 71 |
|
72 |
/* 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
|
73 |
#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
|
74 |
#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 | 75 |
|
5 | 76 |
/* 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
|
77 |
#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
|
78 |
#define SET_ERROR_ERRNO(err_info_ptr, err_code) SET_ERROR_EXTRA(err_info_ptr, err_code, errno); |
3 | 79 |
|
5 | 80 |
/* Set error_info from another error_info. Evaluates to the new error_info */ |
81 |
#define SET_ERROR_INFO(err_info_ptr, from_ptr) (*err_info_ptr = *from_ptr) |
|
82 |
||
83 |
/* Same as above, but also return err_code from func. XXX: use 'return SET_ERROR...' instead */ |
|
84 |
#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
|
85 |
#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
|
86 |
#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 | 87 |
|
5 | 88 |
/* Same as above, but also do a 'goto error' */ |
89 |
#define JUMP_SET_ERROR(err_info_ptr, err_code) do { SET_ERROR(err_info_ptr, err_code); goto error; } while (0) |
|
90 |
#define JUMP_SET_ERROR_INFO(err_info_ptr, from_ptr) do { SET_ERROR_INFO(err_info_ptr, from_ptr); goto error; } while (0) |
|
91 |
||
3 | 92 |
#endif |