[php-src] Issue #10109: Change error code of fsockopen from E_WARNING to EXCEPTION

From: Date: Thu, 15 Dec 2022 19:07:47 +0000
Subject: [php-src] Issue #10109: Change error code of fsockopen from E_WARNING to EXCEPTION
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-243153@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/10109
Author: kaioker

### Description

If fsockopen threw an EXCEPTION instead of E_WARNING the script would still error and halt execution
on failure however the try catch block shown below would work without having to suppress error
reporting for the script. With the removal of the @ suppressor, there is no valid way of determining
if a socket is available without throwing uncatchable errors that halt script execution without
disabling the error reporting system. While i agree with the statement that 'errors should be
handled, not suppressed', there are some scenarios currently where there is simply no other
option. For example, determining if a socket exists:

The following code:

```php
<?php
$ip = "1.2.3.4"; //any non existing socket

try{
    $exists = fsockopen($ip, 13, $errno, $errstr);
} catch($e){
    $exists = false;
}

if(!$exists){
    // do a thing if offline such as transmit a restart command
    echo "service offline. intervention required.";
}else{
    echo "service OK";
}
?>
```

Resulted in this fatal output:
```
E_WARNING
```

But I would have expected this output instead:
```
service offline. intervention required.
```


The only way to get the desired output currently is to disable ALL error reporting for the line,
which just _feels_ wrong:

```php
<?php
$ip = "1.2.3.4"; //any non existing socket

set_error_handler(function(){}); // disable all error reporting entirely
$exists = fsockopen($ip, 13, $errno, $errstr);
restore_error_handler(); // re enable error handling

if(!$exists){
    // do a thing if offline such as transmit a restart command
    echo "service offline. intervention required.";
}else{
    echo "service OK";
}
?>
```


Thread (1 message)

  • kaioker
« previous php.bugs (#243153) next »