-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetworkErrors.hpp
93 lines (72 loc) · 2.17 KB
/
NetworkErrors.hpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
# pragma once
#include <map>
#include <stdexcept>
#include <string>
namespace ErrorsModule
{
enum class NetworkErrorStatus
{
OVERSIZED_PACKET,
CONNECTOPN_CLOSED,
CONNECTION_REFUSED,
UNKNOWN_ERROR,
SERVER_DOWN,
MAX_RETRY_REACHED,
SEND_PACKET_EXCEPTION,
ENDPOINT_CREATION_ERROR,
IP_CONVERSION_ERROR,
SOCKET_CREATION_ERROR,
CONNECTION_ERROR
};
class NetworkException : public std::runtime_error
{
public:
explicit NetworkException(NetworkErrorStatus status)
: std::runtime_error(getMessageForStatus(status)), status_(status)
{
}
explicit NetworkException(const std::string& customMessage)
: std::runtime_error(customMessage),
status_(NetworkErrorStatus::UNKNOWN_ERROR)
{
}
NetworkException(const std::string& extraInfo, NetworkErrorStatus status)
: std::runtime_error(getFullMessage(status, extraInfo)),
status_(status),
extraInfo_(extraInfo)
{
}
virtual NetworkErrorStatus getStatus() const { return status_; }
const std::string& getExtraInfo() const { return extraInfo_; }
static const std::string getFullMessage(NetworkErrorStatus status,
const std::string& extraInfo);
static const std::string getMessageForStatus(NetworkErrorStatus status);
protected:
NetworkErrorStatus status_;
std::string extraInfo_;
static std::map<NetworkErrorStatus, std::string> statusMessages;
};
enum class SendStatus
{
SUCCESS,
FAILURE_NETWORK,
OVERSIZED_PACKET,
UNDERSIZED_PACKET
};
// Custom Exception class
class SendPacketException : public NetworkException
{
public:
SendPacketException(const std::string& message, SendStatus status)
: NetworkException(message), status(status)
{
}
SendStatus getSendStatus() const { return status; }
static const std::string getMessageForStatus(SendStatus status);
static const std::string getFullMessage(SendStatus status,
const std::string& extraInfo);
private:
SendStatus status;
static std::map<SendStatus, std::string> SendStatusMessages;
};
} // namespace ErrorsModule