-
Notifications
You must be signed in to change notification settings - Fork 0
Exceptions
The 2.x release introduced a marker interface so a single catch covers every failure mode in the package.
SocketExceptionInterface (marker — extends \Throwable)
├─ SocketException (extends \RuntimeException)
│ ├─ SocketConnectionException
│ └─ SocketListenException
└─ SocketInvalidArgumentException (extends \InvalidArgumentException)
All four concrete classes live under InitPHP\Socket\Exception.
A marker — no methods of its own. Catch it to handle every socket failure in one place:
use InitPHP\Socket\Exception\SocketExceptionInterface;
try {
$server->listen();
$client->connect();
} catch (SocketExceptionInterface $e) {
// bind failed, connect refused, invalid argument — anything from the package
$logger->error('socket boom', ['err' => $e->getMessage()]);
}Because the interface extends \Throwable, IDEs and static analysers understand it as a real exception type.
The runtime base. Thrown when something failed at the network layer that is not an explicit "could not connect" / "could not listen" / "invalid argument". Extends \RuntimeException.
Common situations:
-
socket_createreturnedfalse. -
socket_bindfailed (port in use, permission denied, address not available). -
socket_select/stream_selectreported an error other thanEINTR. -
listen()called twice. -
connect()called twice on a client. -
tick()called beforelisten(). -
crypto()called beforeconnect()on a TLS / SSL client.
try {
$server->listen();
$server->listen(); // second call
} catch (\InitPHP\Socket\Exception\SocketException $e) {
// "Server is already listening."
}Subclass of SocketException. Thrown when an outbound connection fails:
- TCP
socket_connectrefused (no listener, network unreachable, …). - UDP
socket_connectrefused (rare — usually anEACCES-class error). - TLS / SSL
stream_socket_clientfailed (handshake error, certificate verification, timeout). - Server-side
socket_accepterrored (non-EINTR).
try {
$client->connect();
} catch (\InitPHP\Socket\Exception\SocketConnectionException $e) {
// "Socket Connection Error : Connection refused"
}The exception message includes PHP's underlying error string.
Subclass of SocketException. Specific to bind / listen failures on the server side:
-
socket_listenfailed. -
stream_socket_serverfailed (TLS / SSL bind).
try {
$server->listen();
} catch (\InitPHP\Socket\Exception\SocketListenException $e) {
// "stream_socket_server failed (98): Address already in use"
}The split between SocketException (bind failure) and SocketListenException (listen failure) mirrors PHP's two-step API. In practice most callers catch the more general SocketException.
Extends \InvalidArgumentException and implements SocketExceptionInterface. Thrown synchronously by constructors and configuration setters when the input is wrong:
- Empty host.
- Port outside
1‒65535. -
Domain::fromName('ipx')— unknown domain string. -
CryptoMethod::fromName('tlsv9')— unknown crypto method. -
TCP\backlog(-1)— non-positive backlog. -
StreamChannel::__construct($notAResource)— handed a non-resource.
Catch either the native parent or the marker interface, depending on whether you want package-only granularity:
try {
Socket::server(Transport::TCP, '', 80);
} catch (\InvalidArgumentException $e) { // generic
// ...
} catch (SocketExceptionInterface $e) { // package-only
// ...
}Every package message starts with the PHP-level call that failed when possible, so the cause is searchable:
"socket_bind failed: Address already in use"
"stream_socket_client failed (61): Connection refused"
"Server is already listening."
"backlog must be at least 1."
If a system call returned a numeric errno, the format is (errno) description. For PHP-level guards the message is a plain English sentence.
| Goal | Catch |
|---|---|
| Anything from the package | SocketExceptionInterface |
| Runtime failures only (bind / listen / connect / select) | SocketException |
| "Could not connect to peer" | SocketConnectionException |
| "Could not bind / listen on port" | SocketListenException |
| "Caller passed something invalid" |
SocketInvalidArgumentException (or generic \InvalidArgumentException) |
- Server Lifecycle — which methods throw which exception.
- Client Lifecycle — symmetric story for clients.
- FAQ — common error messages and what they mean.
initphp/socket · MIT · PHP 8.1+ · part of the InitPHP family · file issues at InitPHP/Socket/issues
Getting started
Transports
Concepts
Reference
Recipes
Operational