This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
80 lines (62 loc) · 1.57 KB
/
error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* \file
*
* \internal
*/
#include "config.h"
#include <assert.h>
#include <string.h>
#include <netdb.h>
#include <gnutls/gnutls.h>
#include <nyanttp/local.h>
#include <nyanttp/error.h>
#include <nyanttp/expect.h>
/**
* \brief Error message buffer
*/
static ny_local char error_buf[256];
/**
* \brief Error message map
*/
static char const *const error_map[] = {
[NY_ERROR_VERSION] = "ny version mismatch",
[NY_ERROR_EVVER] = "libev version mismatch",
[NY_ERROR_EVINIT] = "Failed to initialise libev event loop",
[NY_ERROR_EVWATCH] = "libev watcher stopped",
[NY_ERROR_EOF] = "End of file"
};
char const *ny_error_r(struct ny_error const *restrict error, char *restrict buffer, size_t length) {
assert(error);
assert(buffer);
char const *string = "Unknown error";
switch (error->domain) {
case NY_ERROR_DOMAIN_NY:
/* Check if error code inside bounds */
if (unlikely(error->code >= sizeof error_map / sizeof *error_map))
break;
/* Check if error code is known */
if (unlikely(!error_map[error->code]))
break;
string = error_map[error->code];
break;
case NY_ERROR_DOMAIN_ERRNO:
#if STRERROR_R_CHAR_P
string = strerror_r(error->code, buffer, length);
#else
if (unlikely(strerror_r(error->code, buffer, length)))
break;
string = buffer;
#endif
break;
case NY_ERROR_DOMAIN_GAI:
string = gai_strerror(error->code);
break;
case NY_ERROR_DOMAIN_GTLS:
string = gnutls_strerror(error->code);
break;
}
return string;
}
char const *ny_error(struct ny_error const *restrict error) {
return ny_error_r(error, error_buf, sizeof error_buf);
}