-
Notifications
You must be signed in to change notification settings - Fork 2
/
throw_error.cpp
48 lines (44 loc) · 1.08 KB
/
throw_error.cpp
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
#include "throw_error.h"
#include "errno.h"
#include <cstring>
#include <sstream>
#include <stdexcept>
namespace
{
char const* error_enum_name(int err)
{
switch (err)
{
case EBADF:
return "EBADF";
case EAGAIN:
return "EAGAIN";
case EACCES:
return "EACCES";
case EINVAL:
return "EINVAL";
case EMFILE:
return "EMFILE";
case EPIPE:
return "EPIPE";
case EADDRINUSE:
return "EADDRINUSE";
case EADDRNOTAVAIL:
return "EADDRNOTAVAIL";
case ECONNRESET:
return "ECONNRESET";
case ECONNREFUSED:
return "ECONNREFUSED";
}
return "<unknown error>";
}
}
void throw_error [[noreturn]] (int err, char const* action)
{
std::stringstream ss;
ss << action << " failed, error: " << error_enum_name(err);
char tmp[2048];
char const* err_msg = strerror_r(err, tmp, sizeof tmp);
ss << " (" << err << ", " << err_msg << ")";
throw std::runtime_error(ss.str());
}