jsocketpp 1.0
A cross-platform C++20 socket library.
Loading...
Searching...
No Matches
SocketException.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "common.hpp"
12
13#include <exception>
14#include <sstream>
15#include <stdexcept>
16#include <string>
17#include <utility>
18
19namespace jsocketpp
20{
21
62class SocketException : public std::runtime_error
63{
64 public:
77 explicit SocketException(const std::string& message = "SocketException")
78 : std::runtime_error(message), _errorCode(0)
79 {
80 }
81
97 explicit SocketException(const int code, const std::string& message = "SocketException")
98 : std::runtime_error(buildErrorMessage(message, code)), _errorCode(code)
99 {
100 }
101
127 SocketException(const std::string& message, std::exception_ptr nested)
128 : std::runtime_error(message), _errorCode(0), _nested(std::move(nested))
129 {
130 }
131
144 [[nodiscard]] int getErrorCode() const noexcept { return _errorCode; }
145
164 [[nodiscard]] std::exception_ptr getNestedException() const noexcept { return _nested; }
165
176 ~SocketException() override = default;
177
178 private:
180 std::exception_ptr _nested{};
181
196 static std::string buildErrorMessage(const std::string& msg, const int code)
197 {
198 std::ostringstream oss;
199 oss << msg << " (error code " << code << ")";
200 return oss.str();
201 }
202};
203
204} // namespace jsocketpp
SocketException(const std::string &message, std::exception_ptr nested)
Constructs a SocketException with a message and a nested exception.
Definition SocketException.hpp:127
int getErrorCode() const noexcept
Retrieves the platform-specific error code associated with this exception.
Definition SocketException.hpp:144
std::exception_ptr getNestedException() const noexcept
Retrieves the nested exception captured at construction time, if any.
Definition SocketException.hpp:164
std::exception_ptr _nested
Captured nested exception for chaining, if any.
Definition SocketException.hpp:180
SocketException(const int code, const std::string &message="SocketException")
Constructs a SocketException with a platform-specific error code and custom message.
Definition SocketException.hpp:97
~SocketException() override=default
Destroys the SocketException.
static std::string buildErrorMessage(const std::string &msg, const int code)
Builds a formatted error message combining a textual description and an error code.
Definition SocketException.hpp:196
int _errorCode
Platform-specific error code (e.g., errno, WSA error).
Definition SocketException.hpp:179
Common platform and utility includes for jsocketpp.
SocketException(const std::string &message="SocketException")
Constructs a SocketException with a custom error message and no associated error code.
Definition SocketException.hpp:77
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:16