• set_error_handler(): duplicate error

    From alex@21:1/5 to All on Thu Jun 10 11:04:04 2021
    <?php
    //error_reporting(0);
    set_error_handler(
    static function (int $severity, string $message, string $file, int
    $line): void {
    throw new InternalErrorException($message, 0, $severity, $file, $line);
    }
    );

    xxx();

    Output:

    Fatal error: Uncaught Error: Call to undefined function xxx() in /tmp/tmp.Ie1UVhkWcT/test.php on line 9

    Error: Call to undefined function xxx() in /tmp/tmp.Ie1UVhkWcT/test.php
    on line 9

    Call Stack:
    0.0001 392928 1. {main}() /tmp/tmp.Ie1UVhkWcT/test.php:0

    Why error message showing is duplicate?
    Furthermore, if uncommenting the error_reporting(0) instruction does not
    show any errors.
    is there a good compromise?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ii Ii@21:1/5 to All on Wed Jun 16 02:39:26 2021
    I have no idea, why it is like this.



    alex kirjutas neljapäev, 10. juuni 2021 kl 12:04:11 UTC+3:
    <?php
    //error_reporting(0);
    set_error_handler(
    static function (int $severity, string $message, string $file, int
    $line): void {
    throw new InternalErrorException($message, 0, $severity, $file,
    $line);
    }
    );

    xxx();

    Output:

    Fatal error: Uncaught Error: Call to undefined function xxx() in /tmp/tmp.Ie1UVhkWcT/test.php on line 9

    Error: Call to undefined function xxx() in /tmp/tmp.Ie1UVhkWcT/test.php
    on line 9

    Call Stack:
    0.0001 392928 1. {main}() /tmp/tmp.Ie1UVhkWcT/test.php:0

    Why error message showing is duplicate?
    Furthermore, if uncommenting the error_reporting(0) instruction does not show any errors.
    is there a good compromise?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Wed Jun 16 13:48:06 2021
    alex:

    <?php
    //error_reporting(0);
    set_error_handler(
    static function (int $severity, string $message, string $file, int $line): void {
    throw new InternalErrorException($message, 0, $severity, $file, $line);
    }
    );

    xxx();

    Output:

    Fatal error: Uncaught Error: Call to undefined function xxx() in /tmp/tmp.Ie1UVhkWcT/test.php on line 9

    Error: Call to undefined function xxx() in /tmp/tmp.Ie1UVhkWcT/test.php
    on line 9

    Call Stack:
    0.0001 392928 1. {main}() /tmp/tmp.Ie1UVhkWcT/test.php:0

    Why error message showing is duplicate?

    I can't reproduce this. When I run this script with PHP 7.4 as CLI, I
    only get the expected result:

    PHP Fatal error: Uncaught Error: Call to undefined function xxx() in D:\test-errorhandler.php:11
    Stack trace:
    #0 {main}
    thrown in D:\test-errorhandler.php on line 11

    Do you run the above script within a larger application which already
    has some kind of error handling in place?


    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From alex@21:1/5 to All on Fri Jun 18 14:53:14 2021
    Il 16/06/21 13:48, Arno Welzel ha scritto:
    I can't reproduce this.

    ????

    When I run this script with PHP 7.4 as CLI, I
    only get the expected result:

    PHP Fatal error: Uncaught Error: Call to undefined function xxx() in D:\test-errorhandler.php:11
    Stack trace:
    #0 {main}
    thrown in D:\test-errorhandler.php on line 11

    Do you run the above script within a larger application which already
    has some kind of error handling in place?

    No.

    $ php -v
    PHP 7.4.3 (cli) (built: Oct 6 2020 15:47:56) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
    with Xdebug v2.9.2, Copyright (c) 2002-2020, by Derick Rethans tmp.MLHFJ8krVq$ cat test.php
    <?php
    set_error_handler(
    static function (int $severity, string $message, string $file, int
    $line): void {
    throw new InternalErrorException($message, 0, $severity, $file, $line);
    }
    );
    xxx();
    tmp.MLHFJ8krVq$ php test.php

    Fatal error: Uncaught Error: Call to undefined function xxx() in /tmp/tmp.MLHFJ8krVq/test.php on line 7

    Error: Call to undefined function xxx() in /tmp/tmp.MLHFJ8krVq/test.php
    on line 7

    Call Stack:
    0.0001 394848 1. {main}() /tmp/tmp.MLHFJ8krVq/test.php:0

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jerry Stuckle@21:1/5 to alex on Fri Jun 18 22:00:55 2021
    On 6/18/2021 8:53 AM, alex wrote:
    Il 16/06/21 13:48, Arno Welzel ha scritto:
    I can't reproduce this.

    ????

    When I run this script with PHP 7.4 as CLI, I
    only get the expected result:

    PHP Fatal error:  Uncaught Error: Call to undefined function xxx() in
    D:\test-errorhandler.php:11
    Stack trace:
    #0 {main}
       thrown in D:\test-errorhandler.php on line 11

    Do you run the above script within a larger application which already
    has some kind of error handling in place?

    No.

    $ php -v
    PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
        with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
        with Xdebug v2.9.2, Copyright (c) 2002-2020, by Derick Rethans tmp.MLHFJ8krVq$ cat test.php
    <?php
    set_error_handler(
        static function (int $severity, string $message, string $file, int $line): void {
            throw new InternalErrorException($message, 0, $severity, $file, $line);
        }
    );
    xxx();
    tmp.MLHFJ8krVq$ php test.php

    Fatal error: Uncaught Error: Call to undefined function xxx() in /tmp/tmp.MLHFJ8krVq/test.php on line 7

    Error: Call to undefined function xxx() in /tmp/tmp.MLHFJ8krVq/test.php
    on line 7

    Call Stack:
        0.0001     394848   1. {main}() /tmp/tmp.MLHFJ8krVq/test.php:0


    set_error_handler() requires a real function as its parameter, not
    something dynamically defined.


    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    jstucklex@attglobal.net
    ==================

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From alex@21:1/5 to All on Sat Jun 19 16:41:14 2021
    Il 19/06/21 04:00, Jerry Stuckle ha scritto:

    set_error_handler() requires a real function as its parameter, not
    something dynamically defined.

    class C {
    static function f($severity, $message) {
    throw new InternalErrorException($message);
    }
    }

    set_error_handler('C::f');

    xxx();

    Output

    Fatal error: Uncaught Error: Call to undefined function xxx() in /tmp/tmp.F06vf0Q7H0/test.php on line 10

    Error: Call to undefined function xxx() in /tmp/tmp.F06vf0Q7H0/test.php
    on line 10

    Call Stack:
    0.0001 394552 1. {main}() /tmp/tmp.F06vf0Q7H0/test.php:0

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jerry Stuckle@21:1/5 to alex on Sat Jun 19 19:21:51 2021
    On 6/19/2021 10:41 AM, alex wrote:
    Il 19/06/21 04:00, Jerry Stuckle ha scritto:

    set_error_handler() requires a real function as its parameter, not
    something dynamically defined.

    class C {
        static function f($severity, $message) {
            throw new InternalErrorException($message);
        }
    }

    set_error_handler('C::f');

    xxx();

    Output

    Fatal error: Uncaught Error: Call to undefined function xxx() in /tmp/tmp.F06vf0Q7H0/test.php on line 10

    Error: Call to undefined function xxx() in /tmp/tmp.F06vf0Q7H0/test.php
    on line 10

    Call Stack:
        0.0001     394552   1. {main}() /tmp/tmp.F06vf0Q7H0/test.php:0



    Normal operation. See the restrictions under set_error_handler(), namely

    "The following error types cannot be handled with a user defined
    function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING,
    E_COMPILE_ERROR, E_COMPILE_WARNING independent of where they were
    raised, and most of E_STRICT raised in the file where
    set_error_handler() is called. "


    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    jstucklex@attglobal.net
    ==================

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Sun Jun 20 13:02:40 2021
    Jerry Stuckle:

    [...]
    set_error_handler() requires a real function as its parameter, not
    something dynamically defined.

    set_error_handler() requires a *callable* not a function. An anonymous
    function is just fine - as this *is* a callable. This applies to *every* function in PHP which wants a callable and is quite handy to avoid
    declaring new functions if you just need it once for a specific callback:

    -------------------------------------------

    <?php
    $array = [
    [ 'x' => 1, 'y' => 2 ],
    [ 'x' => 6, 'y' => 1 ],
    [ 'x' => 3, 'y' => 7 ],
    ];

    usort(
    $array,
    function($a, $b) {
    if ($a['x'] == $b['x']) {
    return 0;
    } else if ($a['x'] > $b['x']) {
    return 1;
    } else {
    return -1;
    }
    }
    );

    print_r($array);

    -------------------------------------------

    Result:

    Array
    (
    [0] => Array
    (
    [x] => 1
    [y] => 2
    )

    [1] => Array
    (
    [x] => 3
    [y] => 7
    )

    [2] => Array
    (
    [x] => 6
    [y] => 1
    )

    )



    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Sun Jun 20 13:10:28 2021
    Jerry Stuckle:

    On 6/19/2021 10:41 AM, alex wrote:
    Il 19/06/21 04:00, Jerry Stuckle ha scritto:

    set_error_handler() requires a real function as its parameter, not
    something dynamically defined.

    class C {
        static function f($severity, $message) {
            throw new InternalErrorException($message);
        }
    }

    set_error_handler('C::f');

    xxx();

    Output

    Fatal error: Uncaught Error: Call to undefined function xxx() in
    /tmp/tmp.F06vf0Q7H0/test.php on line 10

    Error: Call to undefined function xxx() in /tmp/tmp.F06vf0Q7H0/test.php
    on line 10

    Call Stack:
        0.0001     394552   1. {main}() /tmp/tmp.F06vf0Q7H0/test.php:0



    Normal operation. See the restrictions under set_error_handler(), namely

    "The following error types cannot be handled with a user defined
    function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING,
    E_COMPILE_ERROR, E_COMPILE_WARNING independent of where they were
    raised, and most of E_STRICT raised in the file where
    set_error_handler() is called. "

    Thanks for pointing this out.

    Also see: <https://www.php.net/manual/en/function.set-error-handler.php>

    This means, if such an error ocurrs - like calling an undefined function
    - it will *not* be passed to the handler defined in set_error_handler().

    So this works:

    ------------------------------

    <?php
    set_error_handler(
    function(
    int $severity,
    string $message,
    string $file,
    int $line
    ): void {
    throw new Exception('Error catched: '.$message);
    }
    );

    $a = 1/0;

    ------------------------------

    But this *not*, because calling an undefined function will *not* be
    passed to the user error handler:

    <?php
    set_error_handler(
    function(
    int $severity,
    string $message,
    string $file,
    int $line
    ): void {
    throw new Exception('Error catched: '.$message);
    }
    );

    xxx();


    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Sun Jun 20 12:51:26 2021
    alex:

    Il 16/06/21 13:48, Arno Welzel ha scritto:
    I can't reproduce this.

    ????

    When I run your script, I only get *one* error output, not two.

    When I run this script with PHP 7.4 as CLI, I
    only get the expected result:

    PHP Fatal error: Uncaught Error: Call to undefined function xxx() in
    D:\test-errorhandler.php:11
    Stack trace:
    #0 {main}
    thrown in D:\test-errorhandler.php on line 11

    Do you run the above script within a larger application which already
    has some kind of error handling in place?

    No.

    $ php -v
    PHP 7.4.3 (cli) (built: Oct 6 2020 15:47:56) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
    with Xdebug v2.9.2, Copyright (c) 2002-2020, by Derick Rethans tmp.MLHFJ8krVq$ cat test.php
    <?php
    set_error_handler(
    static function (int $severity, string $message, string $file, int $line): void {
    throw new InternalErrorException($message, 0, $severity, $file, $line);
    }
    );
    xxx();
    tmp.MLHFJ8krVq$ php test.php

    Fatal error: Uncaught Error: Call to undefined function xxx() in /tmp/tmp.MLHFJ8krVq/test.php on line 7

    Error: Call to undefined function xxx() in /tmp/tmp.MLHFJ8krVq/test.php
    on line 7

    Call Stack:
    0.0001 394848 1. {main}() /tmp/tmp.MLHFJ8krVq/test.php:0

    Does not happen here:

    $ php -v
    PHP 7.4.20 (cli) (built: Jun 4 2021 21:24:37) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.20, Copyright (c), by Zend Technologies
    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans

    $ cat error-test.php
    <?php
    set_error_handler(
    static function (int $severity, string $message, string $file, int
    $line): void {
    throw new InternalErrorException($message, 0, $severity, $file, $line);
    }
    );
    xxx();

    $ php error-test.php

    $ php error-test.php
    PHP Fatal error: Uncaught Error: Call to undefined function xxx() in /home/arno/error-test.php:7
    Stack trace:
    #0 {main}
    thrown in /home/arno/error-test.php on line 7

    So this is a local problem specific to your setup.

    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From alex@21:1/5 to All on Mon Jun 21 17:06:52 2021
    Il 20/06/21 12:51, Arno Welzel ha scritto:
    So this is a local problem specific to your setup.

    But I would like to understand what it is.
    Meanwhile, here's another curious thing.

    <?php
    echo $xxx;// one message (logical)
    xxx();// two messages (not logical)

    Output:

    Notice: Undefined variable: xxx in /tmp/tmp.5vTeAJd7Py/test.php on line 2

    Call Stack:
    0.0001 391384 1. {main}() /tmp/tmp.5vTeAJd7Py/test.php:0


    Fatal error: Uncaught Error: Call to undefined function xxx() in /tmp/tmp.5vTeAJd7Py/test.php on line 3

    Error: Call to undefined function xxx() in /tmp/tmp.5vTeAJd7Py/test.php
    on line 3

    Call Stack:
    0.0001 391384 1. {main}() /tmp/tmp.5vTeAJd7Py/test.php:0

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Mon Jun 21 19:43:45 2021
    alex:

    Il 20/06/21 12:51, Arno Welzel ha scritto:
    So this is a local problem specific to your setup.

    But I would like to understand what it is.

    After playing around a bit with the code:

    Propably because you have "display_errors = On" in your php.ini.

    In my php.ini I have:

    display_errors = Off

    But when I change this to:

    display_errors = On

    Tada! Now I also get every error twice:

    ---------

    $ php error-test.php
    PHP Fatal error: Uncaught Error: Call to undefined function xxx() in /home/arno/error-test.php:9
    Stack trace:
    #0 {main}
    thrown in /home/arno/error-test.php on line 9

    Fatal error: Uncaught Error: Call to undefined function xxx() in /home/arno/error-test.php:9
    Stack trace:
    #0 {main}
    thrown in /home/arno/error-test.php on line 9

    ---------

    And even if set_error_handler() will be used, it stays like this:

    ---------

    $ php error-test.php
    PHP Fatal error: Uncaught Exception: Error catched: Division by zero in /home/arno/error-test.php:4
    Stack trace:
    #0 /home/arno/error-test.php(8): {closure}()
    #1 {main}
    thrown in /home/arno/error-test.php on line 4

    Fatal error: Uncaught Exception: Error catched: Division by zero in /home/arno/error-test.php:4
    Stack trace:
    #0 /home/arno/error-test.php(8): {closure}()
    #1 {main}
    thrown in /home/arno/error-test.php on line 4

    ---------

    So my verdict: make sure you have "display_errors = Off" in your php.ini.


    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From alex@21:1/5 to All on Tue Jun 22 16:10:44 2021
    Il 21/06/21 19:43, Arno Welzel ha scritto:
    alex:

    Il 20/06/21 12:51, Arno Welzel ha scritto:
    So this is a local problem specific to your setup.

    But I would like to understand what it is.

    After playing around a bit with the code:

    Propably because you have "display_errors = On" in your php.ini.

    In my php.ini I have:

    display_errors = Off

    But when I change this to:

    display_errors = On

    Tada! Now I also get every error twice:

    Exact.
    When it is off, no errors are seen.
    Bah...
    Strange behavior: you can't get the right compromise.

    $ php error-test.php
    PHP Fatal error: Uncaught Error: Call to undefined function xxx() in /home/arno/error-test.php:9
    Stack trace:
    #0 {main}
    thrown in /home/arno/error-test.php on line 9

    Fatal error: Uncaught Error: Call to undefined function xxx() in /home/arno/error-test.php:9
    Stack trace:
    #0 {main}
    thrown in /home/arno/error-test.php on line 9

    ---------

    And even if set_error_handler() will be used, it stays like this:

    ---------

    $ php error-test.php
    PHP Fatal error: Uncaught Exception: Error catched: Division by zero in /home/arno/error-test.php:4
    Stack trace:
    #0 /home/arno/error-test.php(8): {closure}()
    #1 {main}
    thrown in /home/arno/error-test.php on line 4

    Fatal error: Uncaught Exception: Error catched: Division by zero in /home/arno/error-test.php:4
    Stack trace:
    #0 /home/arno/error-test.php(8): {closure}()
    #1 {main}
    thrown in /home/arno/error-test.php on line 4

    ---------

    So my verdict: make sure you have "display_errors = Off" in your php.ini.



    So as already mentioned (if no error handler is implemented) I can't see
    any errors :|

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From J.O. Aho@21:1/5 to alex on Tue Jun 22 18:13:05 2021
    On 22/06/2021 16.10, alex wrote:

    So as already mentioned (if no error handler is implemented) I can't see
    any errors :|

    php.ini:
    error_log = syslog

    if enabled, errors will be logged to the systems log system


    example:
    Jun 22 16:09:39 machine php: PHP Fatal error: Uncaught Error: Call to undefined function xx() in /tmp/a.php:2
    Jun 22 16:09:39 machine php: Stack trace:
    Jun 22 16:09:39 machine php: #0 {main}
    Jun 22 16:09:39 machine php: thrown in /tmp/a.php on line 2

    file in question:
    <?php
    xx();


    --

    //Aho

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Wed Jun 23 10:24:27 2021
    alex:

    Il 21/06/21 19:43, Arno Welzel ha scritto:
    [...]
    So my verdict: make sure you have "display_errors = Off" in your php.ini.



    So as already mentioned (if no error handler is implemented) I can't see
    any errors :|

    I talk about display_errors and NOT error_reporting!



    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From alex@21:1/5 to All on Wed Jun 23 13:35:36 2021
    Il 22/06/21 18:13, J.O. Aho ha scritto:

    On 22/06/2021 16.10, alex wrote:

    So as already mentioned (if no error handler is implemented) I can't
    see any errors :|

    php.ini:
    error_log = syslog

    if enabled, errors will be logged to the systems log system


    example:
    Jun 22 16:09:39 machine php: PHP Fatal error:  Uncaught Error: Call to undefined function xx() in /tmp/a.php:2
    Jun 22 16:09:39 machine php: Stack trace:
    Jun 22 16:09:39 machine php: #0 {main}
    Jun 22 16:09:39 machine php:   thrown in /tmp/a.php on line 2

    file in question:
    <?php
    xx();



    <?php
    var_dump(ini_get('error_log'));
    xxx();
    ------------------
    /tmp/tmp.PaOxmRpwmD/test.php:2:
    string(6) "syslog"

    Fatal error: Uncaught Error: Call to undefined function xxx() in /tmp/tmp.PaOxmRpwmD/test.php on line 3

    Error: Call to undefined function xxx() in /tmp/tmp.PaOxmRpwmD/test.php
    on line 3

    Call Stack:
    0.0049 393400 1. {main}() /tmp/tmp.PaOxmRpwmD/test.php:0

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)