feat(tcp): a failed send or receive says which errno it failed with

Send() and the receives threw a bare std::runtime_error with the
strerror text, so a caller that reports the cause the way Python does -
"the connection broke ([Errno 104] Connection reset by peer)", which a
language model reads in Crafter.Review's SQL helper output - could not
tell the code. They throw SocketErrorException now, as a failed connect
already does: kind System, the errno, and the same message, still a
std::runtime_error. A receive timeout stays SocketTimeoutException and
a closed peer SocketClosedException.

ShouldReportIoErrors: a peer that resets the connection (SO_LINGER 0)
fails the next receive with ECONNRESET and a send into it with EPIPE
or ECONNRESET.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-10-31 07:06:24 +01:00
commit 16da0a1979
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
5 changed files with 124 additions and 7 deletions

View file

@ -38,10 +38,13 @@ namespace {
return read;
}
// A failed recv. EAGAIN is the receive timeout (SO_RCVTIMEO) running out.
// A failed recv. EAGAIN is the receive timeout (SO_RCVTIMEO) running out;
// anything else carries its errno, under the message a receive always
// threw.
[[noreturn]] void ThrowRecieveError() {
if (errno == EAGAIN || errno == EWOULDBLOCK) throw SocketTimeoutException();
throw std::runtime_error(std::strerror(errno));
const int failure = errno;
if (failure == EAGAIN || failure == EWOULDBLOCK) throw SocketTimeoutException();
throw SocketErrorException(SocketErrorKind::System, failure, std::strerror(failure));
}
// One connect attempt to one address: a connected, blocking socket, or a
@ -211,7 +214,8 @@ void ClientTCP::Send(const void* buffer, std::uint32_t size) const {
throw SocketClosedException();
} else if (status < 0) {
if (errno == EINTR) continue;
throw std::runtime_error(std::strerror(errno));
const int failure = errno;
throw SocketErrorException(SocketErrorKind::System, failure, std::strerror(failure));
}
sent += static_cast<std::uint32_t>(status);
}