Skip to content

Commit 5ee7a4a

Browse files
committed
refactor: exceptions
1 parent cb95968 commit 5ee7a4a

File tree

1 file changed

+81
-49
lines changed

1 file changed

+81
-49
lines changed

include/base_exceptions.hpp

+81-49
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,129 @@
11
#ifndef BASE_EXCEPTIONS_H_
22
#define BASE_EXCEPTIONS_H_
3-
#include <exception>
3+
4+
#include <stdexcept>
5+
#include <string>
6+
#include <sstream>
47

58
namespace tpt
69
{
7-
class FileNotFoundException : public std::exception
10+
class BaseException : public std::runtime_error
811
{
912
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
1123
{
12-
return "File not found";
24+
return m_errorCode;
1325
}
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;
1437
};
1538

16-
class InvalidJSONException : public std::exception
39+
class FileNotFoundException : public BaseException
1740
{
1841
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) {}
2346
};
2447

25-
class IPBlackListedException : public std::exception
48+
class InvalidJSONException : public BaseException
2649
{
2750
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) {}
3255
};
3356

34-
class SocketCreationException : public std::exception
57+
class IPBlackListedException : public BaseException
3558
{
3659
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) {}
4164
};
4265

43-
class SocketCloseException : public std::exception
66+
class SocketCreationException : public BaseException
4467
{
4568
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) {}
5073
};
5174

52-
class SocketBindingException : public std::exception
75+
class SocketCloseException : public BaseException
5376
{
5477
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) {}
5982
};
6083

61-
class SocketListenException : public std::exception
84+
class SocketBindingException : public BaseException
6285
{
6386
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) {}
6891
};
6992

70-
class SocketAcceptException : public std::exception
93+
class SocketListenException : public BaseException
7194
{
7295
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) {}
77100
};
78101

79-
class SocketReceiveException : public std::exception
102+
class SocketAcceptException : public BaseException
80103
{
81104
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) {}
86109
};
87110

88-
class SocketSendException : public std::exception
111+
class SocketReceiveException : public BaseException
89112
{
90113
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) {}
95127
};
96128
}
97129

0 commit comments

Comments
 (0)