author | Tero Marttila <terom@fixme.fi> |
Tue, 10 Mar 2009 02:34:11 +0200 | |
changeset 28 | 9c1050bc8709 |
parent 27 | e6639132bead |
child 29 | 3f0f2898fea3 |
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 |
/* |
|
7 | 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 |
/* |
|
3 | 35 |
* List of defined error codes, organized mostly by function name |
36 |
*/ |
|
37 |
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
|
38 |
/* Core functions */ |
7 | 39 |
_ERROR_CODE( ERR_CALLOC, 0x000100, NONE ), |
3 | 40 |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
41 |
/* Network resolver errors */ |
7 | 42 |
_ERROR_CODE( ERR_GETADDRINFO, 0x000200, GAI ), |
43 |
_ERROR_CODE( ERR_GETADDRINFO_EMPTY, 0x000201, GAI ), |
|
3 | 44 |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
45 |
/* Low-level network errors */ |
7 | 46 |
_ERROR_CODE( ERR_SOCKET, 0x000301, ERRNO ), |
47 |
_ERROR_CODE( ERR_CONNECT, 0x000302, ERRNO ), |
|
3 | 48 |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
49 |
/* Low-level IO errors */ |
7 | 50 |
_ERROR_CODE( ERR_READ, 0x000401, ERRNO ), |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
51 |
_ERROR_CODE( ERR_READ_EOF, 0x000402, NONE ), |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
52 |
_ERROR_CODE( ERR_WRITE, 0x000403, ERRNO ), |
12
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
53 |
_ERROR_CODE( ERR_WRITE_EOF, 0x000404, NONE ), |
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
54 |
_ERROR_CODE( ERR_FCNTL, 0x000405, ERRNO ), |
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
55 |
|
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
56 |
/* GnuTLS errors */ |
7 | 57 |
_ERROR_CODE( ERR_GNUTLS_CERT_ALLOC_CRED, 0x010101, GNUTLS ), |
58 |
_ERROR_CODE( ERR_GNUTLS_GLOBAL_INIT, 0x010102, GNUTLS ), |
|
59 |
_ERROR_CODE( ERR_GNUTLS_INIT, 0x010103, GNUTLS ), |
|
60 |
_ERROR_CODE( ERR_GNUTLS_SET_DEFAULT_PRIORITY, 0x010104, GNUTLS ), |
|
61 |
_ERROR_CODE( ERR_GNUTLS_CRED_SET, 0x010105, GNUTLS ), |
|
62 |
_ERROR_CODE( ERR_GNUTLS_HANDSHAKE, 0x010106, GNUTLS ), |
|
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
63 |
_ERROR_CODE( ERR_GNUTLS_RECORD_SEND, 0x010107, GNUTLS ), |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
64 |
_ERROR_CODE( ERR_GNUTLS_RECORD_RECV, 0x010108, GNUTLS ), |
12
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
65 |
_ERROR_CODE( ERR_GNUTLS_RECORD_GET_DIRECTION, 0x010109, GNUTLS ), |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
66 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
67 |
/* Libevent errors */ |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
68 |
_ERROR_CODE( ERR_EVENT_NEW, 0x010201, NONE ), |
11
14e79683c48c
working event-based operation for sock_tcp
Tero Marttila <terom@fixme.fi>
parents:
10
diff
changeset
|
69 |
_ERROR_CODE( ERR_EVENT_ADD, 0x010202, NONE ), |
17 | 70 |
|
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
71 |
/* Evsql errors */ |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
72 |
_ERROR_CODE( ERR_EVSQL_NEW_PQ, 0x010301, NONE ), |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
73 |
|
17 | 74 |
/* irc_line errors */ |
75 |
_ERROR_CODE( ERR_LINE_TOO_LONG, 0x100101, NONE ), |
|
76 |
_ERROR_CODE( ERR_LINE_INVALID_TOKEN, 0x100102, NONE ), |
|
27
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
77 |
|
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
78 |
/** irc_conn errors */ |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
79 |
_ERROR_CODE( ERR_IRC_CONN_REGISTER_STATE, 0x100201, NONE ), |
7 | 80 |
|
81 |
// mask of bits used for the error_code value |
|
82 |
_ERROR_CODE_MASK = 0xffffff, |
|
3 | 83 |
}; |
84 |
||
85 |
/* |
|
86 |
* An error code and associated extra infos |
|
87 |
*/ |
|
88 |
struct error_info { |
|
89 |
/* The base error code */ |
|
90 |
err_t code; |
|
91 |
||
27
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
92 |
/* Additional detail info, usually some third-party error code */ |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
93 |
int extra; |
3 | 94 |
}; |
95 |
||
6 | 96 |
/* |
7 | 97 |
* Translate an err_t into a function name. |
6 | 98 |
*/ |
99 |
const char *error_name (err_t code); |
|
100 |
||
7 | 101 |
/* |
102 |
* Maximum length of error messages returned by error_msg (including NUL byte) |
|
103 |
*/ |
|
104 |
#define ERROR_MSG_MAXLEN 1024 |
|
105 |
||
106 |
/* |
|
107 |
* Translate an error_info into a message. |
|
108 |
* |
|
109 |
* This is returned as a pointer into a statically allocated buffer. It is not re-entrant. |
|
110 |
*/ |
|
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
111 |
const char *error_msg (const struct error_info *err); |
7 | 112 |
|
3 | 113 |
/** No error, evaulates as logical false */ |
114 |
#define SUCCESS (0) |
|
115 |
||
116 |
/* 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
|
117 |
#define ERROR_CODE(err_info_ptr) ((err_info_ptr)->code) |
3 | 118 |
|
119 |
/* 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
|
120 |
#define ERROR_EXTRA(err_info_ptr) ((err_info_ptr)->extra) |
3 | 121 |
|
122 |
/* 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
|
123 |
#define RESET_ERROR(err_info_ptr) ((err_info_ptr)->code = SUCCESS) |
3 | 124 |
|
125 |
/* 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
|
126 |
#define IS_ERROR(err_info_ptr) (!!(err_info_ptr)->code) |
3 | 127 |
|
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
128 |
/* Compare the err_code/err_extra for an err_info */ |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
129 |
#define MATCH_ERROR(err_info_ptr, err_code, err_extra) ((err_info_ptr)->code == (err_code) && (err_info_ptr)->extra == (err_extra)) |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
130 |
|
3 | 131 |
/* 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
|
132 |
#define SET_ERROR(err_info_ptr, err_code) ((err_info_ptr)->code = (err_code)) |
3 | 133 |
|
134 |
/* 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
|
135 |
#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
|
136 |
#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 | 137 |
|
5 | 138 |
/* 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
|
139 |
#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
|
140 |
#define SET_ERROR_ERRNO(err_info_ptr, err_code) SET_ERROR_EXTRA(err_info_ptr, err_code, errno); |
3 | 141 |
|
5 | 142 |
/* Set error_info from another error_info. Evaluates to the new error_info */ |
143 |
#define SET_ERROR_INFO(err_info_ptr, from_ptr) (*err_info_ptr = *from_ptr) |
|
144 |
||
145 |
/* Same as above, but also return err_code from func. XXX: use 'return SET_ERROR...' instead */ |
|
146 |
#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
|
147 |
#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
|
148 |
#define RETURN_SET_ERROR_ERRNO(err_info_ptr, err_code) do { _SET_ERROR_ERRNO(err_info_ptr, err_code); return (err_code); } while (0) |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
149 |
#define RETURN_SET_ERROR_INFO(err_info_ptr, from_ptr) do { SET_ERROR_INFO(err_info_ptr, from_ptr); return (from_ptr->code); } while (0) |
3 | 150 |
|
5 | 151 |
/* Same as above, but also do a 'goto error' */ |
152 |
#define JUMP_SET_ERROR(err_info_ptr, err_code) do { SET_ERROR(err_info_ptr, err_code); goto error; } while (0) |
|
153 |
#define JUMP_SET_ERROR_INFO(err_info_ptr, from_ptr) do { SET_ERROR_INFO(err_info_ptr, from_ptr); goto error; } while (0) |
|
154 |
||
3 | 155 |
#endif |