|
1 | 1 | #ifndef BASE_EXCEPTIONS_H_
|
2 | 2 | #define BASE_EXCEPTIONS_H_
|
3 |
| -#include <exception> |
| 3 | + |
| 4 | +#include <stdexcept> |
| 5 | +#include <string> |
| 6 | +#include <sstream> |
4 | 7 |
|
5 | 8 | namespace tpt
|
6 | 9 | {
|
7 |
| - class FileNotFoundException : public std::exception |
| 10 | + class BaseException : public std::runtime_error |
8 | 11 | {
|
9 | 12 | public:
|
10 |
| - const char *what() const throw() |
| 13 | + BaseException(const std::string &message = "An error occurred", int errorCode = -1) |
| 14 | + : std::runtime_error(message), m_errorCode(errorCode) |
| 15 | + { |
| 16 | + // Prepare the full message including the error code |
| 17 | + std::ostringstream ss; |
| 18 | + ss << message << " Error code: " << errorCode << "."; |
| 19 | + m_fullMessage = ss.str(); |
| 20 | + } |
| 21 | + |
| 22 | + int getErrorCode() const noexcept |
11 | 23 | {
|
12 |
| - return "File not found"; |
| 24 | + return m_errorCode; |
13 | 25 | }
|
| 26 | + |
| 27 | + virtual const char *what() const noexcept override |
| 28 | + { |
| 29 | + return m_fullMessage.c_str(); |
| 30 | + } |
| 31 | + |
| 32 | + protected: |
| 33 | + int m_errorCode; |
| 34 | + |
| 35 | + private: |
| 36 | + std::string m_fullMessage; |
14 | 37 | };
|
15 | 38 |
|
16 |
| - class InvalidJSONException : public std::exception |
| 39 | + class FileNotFoundException : public BaseException |
17 | 40 | {
|
18 | 41 | public:
|
19 |
| - const char *what() const throw() |
20 |
| - { |
21 |
| - return "JSON parsing error"; |
22 |
| - } |
| 42 | + FileNotFoundException( |
| 43 | + const std::string &message = "File not found", |
| 44 | + int errorCode = -1) |
| 45 | + : BaseException(message, errorCode) {} |
23 | 46 | };
|
24 | 47 |
|
25 |
| - class IPBlackListedException : public std::exception |
| 48 | + class InvalidJSONException : public BaseException |
26 | 49 | {
|
27 | 50 | public:
|
28 |
| - const char *what() const throw() |
29 |
| - { |
30 |
| - return "IP is blacklisted"; |
31 |
| - } |
| 51 | + InvalidJSONException( |
| 52 | + const std::string &message = "JSON parsing error", |
| 53 | + int errorCode = -1) |
| 54 | + : BaseException(message, errorCode) {} |
32 | 55 | };
|
33 | 56 |
|
34 |
| - class SocketCreationException : public std::exception |
| 57 | + class IPBlackListedException : public BaseException |
35 | 58 | {
|
36 | 59 | public:
|
37 |
| - const char *what() const throw() |
38 |
| - { |
39 |
| - return "Error when creating socket"; |
40 |
| - } |
| 60 | + IPBlackListedException( |
| 61 | + const std::string &message = "IP is blacklisted", |
| 62 | + int errorCode = -1) |
| 63 | + : BaseException(message, errorCode) {} |
41 | 64 | };
|
42 | 65 |
|
43 |
| - class SocketCloseException : public std::exception |
| 66 | + class SocketCreationException : public BaseException |
44 | 67 | {
|
45 | 68 | public:
|
46 |
| - const char *what() const throw() |
47 |
| - { |
48 |
| - return "Error closing socket"; |
49 |
| - } |
| 69 | + SocketCreationException( |
| 70 | + const std::string &message = "Error when creating socket", |
| 71 | + int errorCode = -1) |
| 72 | + : BaseException(message, errorCode) {} |
50 | 73 | };
|
51 | 74 |
|
52 |
| - class SocketBindingException : public std::exception |
| 75 | + class SocketCloseException : public BaseException |
53 | 76 | {
|
54 | 77 | public:
|
55 |
| - const char *what() const throw() |
56 |
| - { |
57 |
| - return "Error binding socket"; |
58 |
| - } |
| 78 | + SocketCloseException( |
| 79 | + const std::string &message = "Error closing socket", |
| 80 | + int errorCode = -1) |
| 81 | + : BaseException(message, errorCode) {} |
59 | 82 | };
|
60 | 83 |
|
61 |
| - class SocketListenException : public std::exception |
| 84 | + class SocketBindingException : public BaseException |
62 | 85 | {
|
63 | 86 | public:
|
64 |
| - const char *what() const throw() |
65 |
| - { |
66 |
| - return "Error listening to connections"; |
67 |
| - } |
| 87 | + SocketBindingException( |
| 88 | + const std::string &message = "Error binding socket", |
| 89 | + int errorCode = -1) |
| 90 | + : BaseException(message, errorCode) {} |
68 | 91 | };
|
69 | 92 |
|
70 |
| - class SocketAcceptException : public std::exception |
| 93 | + class SocketListenException : public BaseException |
71 | 94 | {
|
72 | 95 | public:
|
73 |
| - const char *what() const throw() |
74 |
| - { |
75 |
| - return "Error accepting connections"; |
76 |
| - } |
| 96 | + SocketListenException( |
| 97 | + const std::string &message = "Error listening to connections", |
| 98 | + int errorCode = -1) |
| 99 | + : BaseException(message, errorCode) {} |
77 | 100 | };
|
78 | 101 |
|
79 |
| - class SocketReceiveException : public std::exception |
| 102 | + class SocketAcceptException : public BaseException |
80 | 103 | {
|
81 | 104 | public:
|
82 |
| - const char *what() const throw() |
83 |
| - { |
84 |
| - return "Error receiving data"; |
85 |
| - } |
| 105 | + SocketAcceptException( |
| 106 | + const std::string &message = "Error accepting connections", |
| 107 | + int errorCode = -1) |
| 108 | + : BaseException(message, errorCode) {} |
86 | 109 | };
|
87 | 110 |
|
88 |
| - class SocketSendException : public std::exception |
| 111 | + class SocketReceiveException : public BaseException |
89 | 112 | {
|
90 | 113 | public:
|
91 |
| - const char *what() const throw() |
92 |
| - { |
93 |
| - return "Error sending data"; |
94 |
| - } |
| 114 | + SocketReceiveException( |
| 115 | + const std::string &message = "Error receiving data", |
| 116 | + int errorCode = -1) |
| 117 | + : BaseException(message, errorCode) {} |
| 118 | + }; |
| 119 | + |
| 120 | + class SocketSendException : public BaseException |
| 121 | + { |
| 122 | + public: |
| 123 | + SocketSendException( |
| 124 | + const std::string &message = "Error sending data", |
| 125 | + int errorCode = -1) |
| 126 | + : BaseException(message, errorCode) {} |
95 | 127 | };
|
96 | 128 | }
|
97 | 129 |
|
|
0 commit comments