error_get_last() and custom error handler

前端 未结 2 1968
-上瘾入骨i
-上瘾入骨i 2021-01-05 09:46

odbc_errormsg does not report error messages from odbc_execute the way it\'s supposed to. It merely throws a warning. So I\'ve been forced to write a hack to parse the erro

相关标签:
2条回答
  • 2021-01-05 10:25

    You could change your custom error handler to return false, just when the error is ignored (@-operator used).

    function custom_error_handler($errno, $errstr, $errfile, $errline){
        $ignore = ($errno & error_reporting()) == 0;
        if ($ignore) {
            return FALSE;
        }
        echo "[Error happened: $errstr]\n";
        return TRUE;
    }
    
    0 讨论(0)
  • 2021-01-05 10:40

    Right, this is a bizarre solution, but I think it will suit your purposes.

    After a bit of playing around, I have discovered that this:

    function my_error_handler ($errno, $errstr, $errfile = '', $errline = 0, $errcontext = array()) {
    
      // Handle the error here
    
      @trigger_error($errstr);
      return TRUE;
    
    }
    
    // Just to make sure PHP is not outputting anything
    error_reporting(-1);
    ini_set('display_errors',1);
    
    set_error_handler('my_error_handler');
    
    // An E_USR error...
    trigger_error('Some error');
    var_dump(error_get_last());
    
    // ...and a native one
    $key = count();
    var_dump(error_get_last());
    

    Results in this:

    array(4) {
      ["type"]=>
      int(1024)
      ["message"]=>
      string(10) "Some error"
      ["file"]=>
      string(69) "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test.php"
      ["line"]=>
      int(7)
    }
    array(4) {
      ["type"]=>
      int(1024)
      ["message"]=>
      string(45) "count() expects at least 1 parameter, 0 given"
      ["file"]=>
      string(69) "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test.php"
      ["line"]=>
      int(7)
    }
    

    Calling @trigger_error() from within your error handler, and not returning FALSE, causes error_get_last() to return something other than NULL, but because the error is suppressed with @, PHP doesn't output anything. It seems that in the interests of avoiding an infinite recursion, calling trigger_error() from within the registered error handler function does not call the error handler - which works to our advantage here.

    Obviously, the error code has been modified, but you could convert it to the relevant E_USR_* code if you need - but I suspect what you really want is the string value, which this method will allow you get. You have, unfortunately, also lost the line number and file information - although you could possibly get this back by doing something involving a stack trace inside the error handler, or at the very least including it in the string from the arguments passed.

    This is a horrible, horrible, horrible hack - but since there is no officially sanctioned way to do this, a hack is essentially what you are asking for.

    0 讨论(0)
提交回复
热议问题