From 31fa4cca2615b7402df9b1637027a1a059840794 Mon Sep 17 00:00:00 2001 From: Jacob Nilsson <166003882+jacnils@users.noreply.github.com> Date: Tue, 15 Sep 2026 00:07:37 +0200 Subject: [PATCH 1/2] Add files via upload --- include/netkit/body/read_status_enum.hpp | 5 ++- include/netkit/except.hpp | 41 ++++++++++++++++++- include/netkit/http/async_client.hpp | 7 ++-- include/netkit/http/basic_request_handler.hpp | 5 ++- include/netkit/http/client.hpp | 7 ++-- include/netkit/http/header.hpp | 7 ++-- include/netkit/http/request_handler.hpp | 12 +++--- include/netkit/io/cancellation.hpp | 5 ++- src/dns/cache.cpp | 3 +- src/http/predefined.cpp | 3 +- src/io/bsd/io_backend.cpp | 18 ++++---- src/io/linux/io_backend.cpp | 14 +++---- src/network/network_interface.cpp | 4 +- src/stream/async_socket_stream.cpp | 3 +- src/stream/socket_stream.cpp | 9 ++-- src/stream/wolfssl/async_tls_stream.cpp | 11 ++--- src/stream/wolfssl/tls_stream.cpp | 11 ++--- src/udp/udp_datagram.cpp | 3 +- src/utility.cpp | 7 ++-- 19 files changed, 112 insertions(+), 63 deletions(-) diff --git a/include/netkit/body/read_status_enum.hpp b/include/netkit/body/read_status_enum.hpp index 594cb80..2112378 100644 --- a/include/netkit/body/read_status_enum.hpp +++ b/include/netkit/body/read_status_enum.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -65,7 +66,7 @@ namespace netkit::body { auto res = reader.read(buffer, sizeof(buffer)); if (res.get_status() == read_status::error) - throw std::runtime_error("read failed"); + throw netkit::io_error("read failed"); if (res.get_status() == read_status::timeout) continue; @@ -96,7 +97,7 @@ namespace netkit::body { auto res = co_await reader.read(buffer, sizeof(buffer)); if (res.get_status() == read_status::error) - throw std::runtime_error("read failed"); + throw netkit::io_error("read failed"); if (res.get_status() == read_status::timeout) continue; diff --git a/include/netkit/except.hpp b/include/netkit/except.hpp index 37e0e03..310d3a7 100644 --- a/include/netkit/except.hpp +++ b/include/netkit/except.hpp @@ -11,6 +11,7 @@ */ #pragma once +#include #include #include @@ -51,11 +52,47 @@ namespace netkit { explicit dns_error(std::string msg) : generic_error(std::move(msg)) {} }; -#ifdef NETKIT_OPENSSL + /** + * @brief Thrown for TLS/SSL failures (handshake, context/session setup, certificate export, etc.). + * @note Not guarded behind a TLS backend macro: the class itself has no backend dependency, + * only the code paths that throw it (e.g. src/stream/wolfssl/*, src/stream/openssl/*) do. + */ class NETKIT_API ssl_error : public generic_error { public: ssl_error() : generic_error("SSL error") {} explicit ssl_error(std::string msg) : generic_error(std::move(msg)) {} }; -#endif + + /** + * @brief Thrown when netkit is used incorrectly: an object is used in an invalid state, + * a precondition/argument is violated, or an operation is not supported. + * @note Mirrors the role std::logic_error plays in the standard library, but lives in + * netkit's own hierarchy so callers only ever need to catch netkit::generic_error. + */ + class NETKIT_API logic_error : public generic_error { + public: + logic_error() : generic_error("Logic error") {} + explicit logic_error(std::string msg) : generic_error(std::move(msg)) {} + }; + + /** + * @brief Thrown when an operation would exceed (or exceeded) an explicit size/length bound. + * @note Mirrors std::length_error, and like it, derives from the "logic error" branch of + * the hierarchy since exceeding a caller-specified bound is a precondition violation. + */ + class NETKIT_API length_error : public logic_error { + public: + length_error() : logic_error("Length error") {} + explicit length_error(std::string msg) : logic_error(std::move(msg)) {} + }; + + /** + * @brief Thrown for low-level I/O failures that aren't specific to a socket, IP, or DNS + * operation: file access, and OS event-loop backends (epoll/kqueue/IOCP/etc.). + */ + class NETKIT_API io_error : public generic_error { + public: + io_error() : generic_error("I/O error") {} + explicit io_error(std::string msg) : generic_error(std::move(msg)) {} + }; } \ No newline at end of file diff --git a/include/netkit/http/async_client.hpp b/include/netkit/http/async_client.hpp index 32e7cba..044b614 100644 --- a/include/netkit/http/async_client.hpp +++ b/include/netkit/http/async_client.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -113,11 +114,11 @@ namespace netkit::http { ss << method; if (path.empty()) { - throw std::logic_error("path is empty"); + throw netkit::logic_error("path is empty"); } if (path.at(0) != '/') { - throw std::logic_error("path must start with /"); + throw netkit::logic_error("path must start with /"); } ss << " " << path << " HTTP/1.1" << "\r\n"; @@ -145,7 +146,7 @@ namespace netkit::http { const auto line_end = header_data.find_first_of("\r\n"); if (line_end == std::string::npos) - throw std::logic_error{"invalid HTTP async_response"}; + throw netkit::parsing_error{"invalid HTTP async_response"}; const auto status_line = std::string_view{ header_data.data(), diff --git a/include/netkit/http/basic_request_handler.hpp b/include/netkit/http/basic_request_handler.hpp index 37af7cd..5b137b4 100644 --- a/include/netkit/http/basic_request_handler.hpp +++ b/include/netkit/http/basic_request_handler.hpp @@ -14,6 +14,7 @@ #ifdef NETKIT_HTTP #include +#include #include #include @@ -22,10 +23,10 @@ namespace netkit::http::server { class basic_request_handler { public: virtual void handle(std::unique_ptr, server_settings&, const request_callback&) const { - throw std::runtime_error("not implemented"); + throw netkit::logic_error("not implemented"); } virtual io::task handle(std::unique_ptr, server_settings&, const async_request_callback&) const { - throw std::runtime_error("not implemented"); + throw netkit::logic_error("not implemented"); } virtual ~basic_request_handler() = default; }; diff --git a/include/netkit/http/client.hpp b/include/netkit/http/client.hpp index 2dfdb02..9f26b1b 100644 --- a/include/netkit/http/client.hpp +++ b/include/netkit/http/client.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -108,11 +109,11 @@ namespace netkit::http { ss << method; if (path.empty()) { - throw std::logic_error("path is empty"); + throw netkit::logic_error("path is empty"); } if (path.at(0) != '/') { - throw std::logic_error("path must start with /"); + throw netkit::logic_error("path must start with /"); } ss << " " << path << " HTTP/1.1" << "\r\n"; @@ -140,7 +141,7 @@ namespace netkit::http { const auto line_end = header_data.find_first_of("\r\n"); if (line_end == std::string::npos) - throw std::logic_error{"invalid HTTP response"}; + throw netkit::parsing_error{"invalid HTTP response"}; const auto status_line = std::string_view{ header_data.data(), diff --git a/include/netkit/http/header.hpp b/include/netkit/http/header.hpp index 4fc7b87..9f569cc 100644 --- a/include/netkit/http/header.hpp +++ b/include/netkit/http/header.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -230,13 +231,13 @@ namespace netkit::http { const auto first_space = status_line.find(' '); if (first_space == std::string_view::npos) - throw std::logic_error{"invalid HTTP status line"}; + throw netkit::parsing_error{"invalid HTTP status line"}; const auto code_start = first_space + 1; const auto second_space = status_line.find(' ', code_start); if (second_space == std::string_view::npos) - throw std::logic_error{"invalid HTTP status line"}; + throw netkit::parsing_error{"invalid HTTP status line"}; const auto code_str = status_line.substr( code_start, @@ -248,7 +249,7 @@ namespace netkit::http { const auto [ptr, ec] = std::from_chars(code_str.data(), code_str.data() + code_str.size(), code); if (ec != std::errc{} || ptr != code_str.data() + code_str.size()) - throw std::logic_error{"invalid HTTP status code"}; + throw netkit::parsing_error{"invalid HTTP status code"}; return code; } diff --git a/include/netkit/http/request_handler.hpp b/include/netkit/http/request_handler.hpp index d99bcbd..2723681 100644 --- a/include/netkit/http/request_handler.hpp +++ b/include/netkit/http/request_handler.hpp @@ -65,7 +65,7 @@ namespace netkit::http::server { } if (!file.is_open()) { - throw std::runtime_error("failed to open session file (read_from_session_file()): " + f); + throw netkit::io_error("failed to open session file (read_from_session_file()): " + f); } std::string line{}; @@ -91,7 +91,7 @@ namespace netkit::http::server { std::ofstream file(f, std::ios::trunc); if (!file.is_open() || !file.good()) { - throw std::runtime_error("failed to open session file (write_to_session_file()): " + f); + throw netkit::io_error("failed to open session file (write_to_session_file()): " + f); } for (const auto& it : session) { @@ -467,7 +467,7 @@ namespace netkit::http::server { switch (result.get_status()) { case status_t::error: - throw std::runtime_error("Body read error"); + throw netkit::io_error("Body read error"); case status_t::timeout: continue; @@ -487,7 +487,7 @@ namespace netkit::http::server { )); if (write_status == netkit::stream::stream_status::error) - throw std::runtime_error("Socket write error"); + throw netkit::socket_error("Socket write error"); total_sent += sent; } @@ -788,7 +788,7 @@ namespace netkit::http::server { switch (result.get_status()) { case status_t::error: - throw std::runtime_error("Body read error"); + throw netkit::io_error("Body read error"); case status_t::timeout: continue; @@ -808,7 +808,7 @@ namespace netkit::http::server { )); if (write_status == netkit::stream::stream_status::error) - throw std::runtime_error("Socket write error"); + throw netkit::socket_error("Socket write error"); total_sent += sent; } diff --git a/include/netkit/io/cancellation.hpp b/include/netkit/io/cancellation.hpp index d81344c..7b57c62 100644 --- a/include/netkit/io/cancellation.hpp +++ b/include/netkit/io/cancellation.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -10,9 +11,9 @@ namespace netkit::io { /** * @brief Exception thrown when a task is cancelled. */ -class NETKIT_API cancelled_error : public std::runtime_error { +class NETKIT_API cancelled_error : public netkit::generic_error { public: - cancelled_error() : std::runtime_error("Task was cancelled") {} + cancelled_error() : netkit::generic_error("Task was cancelled") {} }; /** diff --git a/src/dns/cache.cpp b/src/dns/cache.cpp index 2004120..7ee20d7 100644 --- a/src/dns/cache.cpp +++ b/src/dns/cache.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -104,7 +105,7 @@ void netkit::dns::standard_cache::store(const std::string& hostname, const std:: std::ofstream os(utility::get_standard_cache_location(), std::ios::binary | std::ios::trunc); if (!os) { - throw std::runtime_error("failed to open DNS cache file for writing"); + throw io_error("failed to open DNS cache file for writing"); } for (const auto& [name, records] : cache) { diff --git a/src/http/predefined.cpp b/src/http/predefined.cpp index 9f786cb..e6b93b4 100644 --- a/src/http/predefined.cpp +++ b/src/http/predefined.cpp @@ -10,6 +10,7 @@ * @brief Implementation of predefined HTTP status codes and messages. */ #include +#include #include #include @@ -50,7 +51,7 @@ std::string netkit::http::get_method_string(netkit::http::method method) { case method::method_patch: ss << "PATCH"; break; case method::method_undefined: - throw std::logic_error("undefined method"); + throw netkit::logic_error("undefined method"); } return ss.str(); } \ No newline at end of file diff --git a/src/io/bsd/io_backend.cpp b/src/io/bsd/io_backend.cpp index 47a166e..adfba6f 100644 --- a/src/io/bsd/io_backend.cpp +++ b/src/io/bsd/io_backend.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include @@ -21,7 +21,7 @@ netkit::io::io_backend::io_backend() { kqueue_fd_ = kqueue(); if (kqueue_fd_ == -1) - throw std::runtime_error("kqueue failed"); + throw netkit::io_error("kqueue failed"); struct kevent ev{}; @@ -39,7 +39,7 @@ netkit::io::io_backend::io_backend() { close(kqueue_fd_); kqueue_fd_ = -1; - throw std::runtime_error("failed to add kqueue wake event"); + throw netkit::io_error("failed to add kqueue wake event"); } } @@ -85,7 +85,7 @@ void netkit::io::io_backend::update_state( ); if (kevent(kqueue_fd_, &ev, 1, nullptr, 0, nullptr) == -1) - throw std::runtime_error( + throw netkit::io_error( "failed to add kqueue read filter: " + std::string(std::strerror(errno)) ); @@ -104,7 +104,7 @@ void netkit::io::io_backend::update_state( if (kevent(kqueue_fd_, &ev, 1, nullptr, 0, nullptr) == -1 && errno != ENOENT) { - throw std::runtime_error("failed to remove kqueue read filter: " + std::string(std::strerror(errno))); + throw netkit::io_error("failed to remove kqueue read filter: " + std::string(std::strerror(errno))); } } @@ -122,7 +122,7 @@ void netkit::io::io_backend::update_state( ); if (kevent(kqueue_fd_, &ev, 1, nullptr, 0, nullptr) == -1) - throw std::runtime_error( + throw netkit::io_error( "failed to add kqueue write filter: " + std::string(std::strerror(errno)) ); @@ -141,7 +141,7 @@ void netkit::io::io_backend::update_state( if (kevent(kqueue_fd_, &ev, 1, nullptr, 0, nullptr) == -1 && errno != ENOENT) { - throw std::runtime_error( + throw netkit::io_error( "failed to remove kqueue write filter: " + std::string(std::strerror(errno)) ); @@ -188,7 +188,7 @@ void netkit::io::io_backend::run() { if (errno == EINTR) continue; - throw std::runtime_error("kevent failed"); + throw netkit::io_error("kevent failed"); } for (int i = 0; i < n; ++i) { @@ -266,7 +266,7 @@ void netkit::io::io_backend::poll(int timeout_ms) { if (errno == EINTR) return; - throw std::runtime_error("kevent failed"); + throw netkit::io_error("kevent failed"); } for (int i = 0; i < n; ++i) { diff --git a/src/io/linux/io_backend.cpp b/src/io/linux/io_backend.cpp index 70e2901..7c98004 100644 --- a/src/io/linux/io_backend.cpp +++ b/src/io/linux/io_backend.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include #include @@ -18,19 +18,19 @@ netkit::io::io_backend::io_backend() { epoll_fd_ = epoll_create1(0); if (epoll_fd_ == -1) - throw std::runtime_error("epoll_create1 failed"); + throw netkit::io_error("epoll_create1 failed"); wake_fd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (wake_fd_ == -1) - throw std::runtime_error("eventfd failed"); + throw netkit::io_error("eventfd failed"); epoll_event ev{}; ev.events = EPOLLIN; ev.data.fd = wake_fd_; if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, wake_fd_, &ev) == -1) { - throw std::runtime_error("failed to add wake fd"); + throw netkit::io_error("failed to add wake fd"); } } @@ -73,14 +73,14 @@ void netkit::io::io_backend::update_state(int fd, const io_handle_state& state) ret = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &ev); if (ret == -1) - throw std::runtime_error(std::strerror(errno)); + throw netkit::io_error(std::strerror(errno)); registered_fds_.insert(fd); } else { ret = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, fd, &ev); if (ret == -1) - throw std::runtime_error(std::strerror(errno)); + throw netkit::io_error(std::strerror(errno)); } } @@ -105,7 +105,7 @@ void netkit::io::io_backend::run() { if (n == -1) { if (errno == EINTR) continue; - throw std::runtime_error("epoll_wait failed"); + throw netkit::io_error("epoll_wait failed"); } for (int i = 0; i < n; ++i) { diff --git a/src/network/network_interface.cpp b/src/network/network_interface.cpp index 53e51c5..f4c13a6 100644 --- a/src/network/network_interface.cpp +++ b/src/network/network_interface.cpp @@ -12,8 +12,6 @@ #include #include #include -#include - #include #include #include @@ -239,7 +237,7 @@ std::vector netkit::network::get_interfaces( DWORD ret = GetAdaptersAddresses(family, flags, nullptr, adapters, &out_buf_len); if (ret != NO_ERROR) { - throw std::runtime_error("GetAdaptersAddresses() failed in get_interfaces()"); + throw ip_error("GetAdaptersAddresses() failed in get_interfaces()"); } for (IP_ADAPTER_ADDRESSES* adapter = adapters; adapter != nullptr; adapter = adapter->Next) { diff --git a/src/stream/async_socket_stream.cpp b/src/stream/async_socket_stream.cpp index 7cacd22..4c8d56f 100644 --- a/src/stream/async_socket_stream.cpp +++ b/src/stream/async_socket_stream.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -42,5 +43,5 @@ netkit::socket::addr netkit::stream::async_socket_stream::peer() const { if (socket_) return socket_->get_peer(); - throw std::runtime_error{"peer(): socket_ not initialized"}; + throw netkit::logic_error{"peer(): socket_ not initialized"}; } diff --git a/src/stream/socket_stream.cpp b/src/stream/socket_stream.cpp index d5de98d..459fd53 100644 --- a/src/stream/socket_stream.cpp +++ b/src/stream/socket_stream.cpp @@ -1,15 +1,16 @@ #include +#include void netkit::stream::socket_stream::connect() { if (!socket_) - throw std::logic_error("socket closed"); + throw netkit::logic_error("socket closed"); socket_->connect(); } netkit::stream::stream_result netkit::stream::socket_stream::read(std::span buffer) { if (!socket_) - throw std::logic_error("socket closed"); + throw netkit::logic_error("socket closed"); auto result = socket_->recv( buffer.data(), @@ -38,7 +39,7 @@ netkit::stream::stream_result netkit::stream::socket_stream::read(std::span buffer) { if (!socket_) - throw std::logic_error("socket closed"); + throw netkit::logic_error("socket closed"); auto result = socket_->send( buffer.data(), @@ -74,7 +75,7 @@ bool netkit::stream::socket_stream::is_open() const noexcept { netkit::socket::addr netkit::stream::socket_stream::peer() const { if (!socket_) - throw std::logic_error("socket closed"); + throw netkit::logic_error("socket closed"); return socket_->get_peer(); } diff --git a/src/stream/wolfssl/async_tls_stream.cpp b/src/stream/wolfssl/async_tls_stream.cpp index d4a212e..a9b7e60 100644 --- a/src/stream/wolfssl/async_tls_stream.cpp +++ b/src/stream/wolfssl/async_tls_stream.cpp @@ -9,6 +9,7 @@ #endif #include +#include #include #include #include @@ -55,7 +56,7 @@ netkit::stream::async_tls_stream::async_tls_stream(std::unique_ptr +#include #include #include @@ -53,7 +54,7 @@ netkit::stream::tls_stream::tls_stream(std::unique_ptr stream, ctx_ = wolfSSL_CTX_new(wolfTLS_client_method()); if (!ctx_) - throw std::runtime_error("wolfSSL_CTX_new failed"); + throw netkit::ssl_error("wolfSSL_CTX_new failed"); // TODO: maybe we shouldn't store the version in the class? haven't yet decided on this switch (version_) { @@ -104,7 +105,7 @@ netkit::stream::tls_stream::tls_stream(std::unique_ptr stream, if (!loaded_ca && crypto::windows::is_outdated(path.wstring())) { std::filesystem::remove(path); if (!crypto::windows::export_certs(path.wstring())) { - throw std::runtime_error("failed to export certificates"); + throw netkit::ssl_error("failed to export certificates"); } } @@ -132,7 +133,7 @@ netkit::stream::tls_stream::tls_stream(std::unique_ptr stream, #endif if (!loaded_ca && verification_ == verification::peer) { - throw std::runtime_error( + throw netkit::ssl_error( "No trusted CA certificates available" ); } @@ -169,7 +170,7 @@ netkit::stream::tls_stream::tls_stream(std::unique_ptr stream, ssl_ = wolfSSL_new(ctx_); if (!ssl_) - throw std::runtime_error("wolfSSL_new failed"); + throw netkit::ssl_error("wolfSSL_new failed"); wolfSSL_SetIOReadCtx(ssl_, this); wolfSSL_SetIOWriteCtx(ssl_, this); @@ -188,7 +189,7 @@ void netkit::stream::tls_stream::perform_handshake() const { if (ret != WOLFSSL_SUCCESS) { int err = wolfSSL_get_error(ssl_, ret); - throw std::runtime_error("TLS handshake failed: " + std::to_string(err)); + throw netkit::ssl_error("TLS handshake failed: " + std::to_string(err)); } } diff --git a/src/udp/udp_datagram.cpp b/src/udp/udp_datagram.cpp index a7d27e9..40134aa 100644 --- a/src/udp/udp_datagram.cpp +++ b/src/udp/udp_datagram.cpp @@ -1,4 +1,5 @@ #include +#include #include netkit::udp::udp_datagram::udp_datagram(const socket::addr& addr) @@ -25,7 +26,7 @@ netkit::udp::udp_datagram::recv_from(std::span buffer) { buffer.size() ); - throw std::runtime_error{"recv_from() failed: sock_ == nullptr"}; + throw netkit::logic_error{"recv_from() failed: sock_ == nullptr"}; } void netkit::udp::udp_datagram::close() noexcept { if (sock_) diff --git a/src/utility.cpp b/src/utility.cpp index 4bf33d7..a1ca7cf 100644 --- a/src/utility.cpp +++ b/src/utility.cpp @@ -10,6 +10,7 @@ * @brief Implementation of various utility functions used throughout the Netkit library. */ #include +#include #include #include @@ -267,7 +268,7 @@ namespace netkit::utility { [[nodiscard]] std::string read_file(const std::string& path) { std::ifstream file(path, std::ios::in | std::ios::binary | std::ios::ate); if (!file) { - throw std::runtime_error("failed to open file: " + path); + throw io_error("failed to open file: " + path); } std::streamsize size = file.tellg(); @@ -275,7 +276,7 @@ namespace netkit::utility { file.seekg(0, std::ios::beg); if (!file.read(&buffer[0], size)) { - throw std::runtime_error("failed to read file: " + path); + throw io_error("failed to read file: " + path); } return buffer; @@ -388,7 +389,7 @@ namespace netkit::utility { ret.push_back(static_cast((four >> three) & 0xFF)); } } else { - throw std::runtime_error{"invalid string"}; + throw parsing_error{"invalid string"}; } } From f7af05ca83792013bd34139a3cdd7ea2d31538cf Mon Sep 17 00:00:00 2001 From: Jacob Nilsson <166003882+jacnils@users.noreply.github.com> Date: Tue, 15 Sep 2026 00:08:39 +0200 Subject: [PATCH 2/2] Add files via upload --- include/netkit/io/timeout.hpp | 5 +++-- .../socket/native/basic_native_async_listener.hpp | 5 +++-- .../socket/native/basic_native_async_socket.hpp | 13 +++++++------ .../socket/native/basic_native_sync_listener.hpp | 5 +++-- .../socket/native/basic_native_sync_socket.hpp | 9 +++++---- include/netkit/stream/basic_async_stream.hpp | 4 ++-- include/netkit/stream/basic_stream.hpp | 6 +++--- 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/include/netkit/io/timeout.hpp b/include/netkit/io/timeout.hpp index 68cc252..a43ac4d 100644 --- a/include/netkit/io/timeout.hpp +++ b/include/netkit/io/timeout.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -13,9 +14,9 @@ namespace netkit::io { /** * @brief Exception thrown when an operation times out. */ -class NETKIT_API timeout_error : public std::runtime_error { +class NETKIT_API timeout_error : public netkit::generic_error { public: - timeout_error() : std::runtime_error("Operation timed out") {} + timeout_error() : netkit::generic_error("Operation timed out") {} }; /** diff --git a/include/netkit/socket/native/basic_native_async_listener.hpp b/include/netkit/socket/native/basic_native_async_listener.hpp index 4de3949..f7fdb91 100644 --- a/include/netkit/socket/native/basic_native_async_listener.hpp +++ b/include/netkit/socket/native/basic_native_async_listener.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -19,10 +20,10 @@ namespace netkit::socket::native { virtual void close() noexcept = 0; [[nodiscard]] virtual const addr& get_local_endpoint() const { - throw std::logic_error{"socket does not have an addr object"}; + throw netkit::logic_error{"socket does not have an addr object"}; } [[nodiscard]] virtual fd_t native_handle() const { - throw std::logic_error{"socket does not have a native handle"}; + throw netkit::logic_error{"socket does not have a native handle"}; } }; } \ No newline at end of file diff --git a/include/netkit/socket/native/basic_native_async_socket.hpp b/include/netkit/socket/native/basic_native_async_socket.hpp index cceeef1..17dedd1 100644 --- a/include/netkit/socket/native/basic_native_async_socket.hpp +++ b/include/netkit/socket/native/basic_native_async_socket.hpp @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -38,22 +39,22 @@ namespace netkit::socket::native { virtual void unbind() noexcept = 0; virtual addr& get_addr() { - throw std::logic_error{"socket does not have an addr object"}; + throw netkit::logic_error{"socket does not have an addr object"}; } [[nodiscard]] virtual const addr& get_addr() const { - throw std::logic_error{"socket does not have an addr object"}; + throw netkit::logic_error{"socket does not have an addr object"}; } [[nodiscard]] virtual addr get_peer() const { - throw std::logic_error{"socket does not have a peer"}; + throw netkit::logic_error{"socket does not have a peer"}; }; virtual void set_sock_opts(opt opts) { - throw std::logic_error{"socket does not have opts to set"}; + throw netkit::logic_error{"socket does not have opts to set"}; } [[nodiscard]] virtual fd_t native_handle() const { - throw std::logic_error{"socket does not have a native handle"}; + throw netkit::logic_error{"socket does not have a native handle"}; } [[nodiscard]] virtual io::io_context& native_io_context() { - throw std::logic_error{"socket does not have an io context"}; + throw netkit::logic_error{"socket does not have an io context"}; } }; } \ No newline at end of file diff --git a/include/netkit/socket/native/basic_native_sync_listener.hpp b/include/netkit/socket/native/basic_native_sync_listener.hpp index 951782c..f646de3 100644 --- a/include/netkit/socket/native/basic_native_sync_listener.hpp +++ b/include/netkit/socket/native/basic_native_sync_listener.hpp @@ -4,6 +4,7 @@ #include #include +#include #include namespace netkit::socket::native { @@ -21,12 +22,12 @@ namespace netkit::socket::native { [[nodiscard]] virtual std::unique_ptr accept() = 0; [[nodiscard]] virtual const addr& get_local_endpoint() const { - throw std::logic_error{"socket does not have an addr object"}; + throw netkit::logic_error{"socket does not have an addr object"}; } virtual void close() noexcept = 0; [[nodiscard]] virtual fd_t native_handle() const { - throw std::logic_error{"socket does not have a native handle"}; + throw netkit::logic_error{"socket does not have a native handle"}; } }; } \ No newline at end of file diff --git a/include/netkit/socket/native/basic_native_sync_socket.hpp b/include/netkit/socket/native/basic_native_sync_socket.hpp index 503c287..6601a1d 100644 --- a/include/netkit/socket/native/basic_native_sync_socket.hpp +++ b/include/netkit/socket/native/basic_native_sync_socket.hpp @@ -13,6 +13,7 @@ #include #include +#include #include namespace netkit::socket::native { @@ -34,17 +35,17 @@ namespace netkit::socket::native { virtual void unbind() noexcept = 0; [[nodiscard]] virtual bool is_open() const noexcept = 0; virtual addr& get_addr() { - throw std::logic_error{"socket does not have an addr object"}; + throw netkit::logic_error{"socket does not have an addr object"}; } [[nodiscard]] virtual const addr& get_addr() const { - throw std::logic_error{"socket does not have an addr object"}; + throw netkit::logic_error{"socket does not have an addr object"}; } virtual void close() noexcept = 0; [[nodiscard]] virtual fd_t native_handle() const { - throw std::logic_error{"socket does not have a native handle"}; + throw netkit::logic_error{"socket does not have a native handle"}; } virtual void set_sock_opts(opt opts) { - throw std::logic_error{"socket does not have opts to set"}; + throw netkit::logic_error{"socket does not have opts to set"}; } [[nodiscard]] virtual addr get_peer() const = 0; diff --git a/include/netkit/stream/basic_async_stream.hpp b/include/netkit/stream/basic_async_stream.hpp index 4288a91..daeb217 100644 --- a/include/netkit/stream/basic_async_stream.hpp +++ b/include/netkit/stream/basic_async_stream.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -8,7 +9,6 @@ #include #include -#include #include #include #include @@ -105,7 +105,7 @@ class NETKIT_API basic_async_stream { break; if (res.status != stream::stream_status::success) - throw std::runtime_error("read failed"); + throw netkit::io_error("read failed"); result.insert( result.end(), diff --git a/include/netkit/stream/basic_stream.hpp b/include/netkit/stream/basic_stream.hpp index 43bae91..9fd202d 100644 --- a/include/netkit/stream/basic_stream.hpp +++ b/include/netkit/stream/basic_stream.hpp @@ -1,10 +1,10 @@ #pragma once #include +#include #include #include #include -#include #include #include #include @@ -105,7 +105,7 @@ class NETKIT_API basic_stream { break; if (res.status != stream_status::success) - throw std::runtime_error("read failed"); + throw netkit::io_error("read failed"); auto amount = (std::min)(res.bytes, max_bytes - total); @@ -118,7 +118,7 @@ class NETKIT_API basic_stream { total += amount; if (amount != res.bytes) - throw std::length_error("read_all exceeded maximum size"); + throw netkit::length_error("read_all exceeded maximum size"); } return result;