1

I'm trying to follow Can I try/catch a warning? and treat all warnings as exceptions. I can get the error details in the custom error handler but the ErrorException I get in my catch block is always empty.

private function SetCustomExceptionHandler() {
        set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
            log_message('error', '$errno ' . $errno);
            log_message('error', '$errstr ' . $errstr);
            log_message('error', '$errfile ' . $errfile);
            log_message('error', '$errline ' . $errline);
            log_message('error', '$errcontext ' . json_encode($errcontext));

            throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
        });
    }

$this->SetCustomExceptionHandler();
            try {
                //LDAP query that returns: ldap_search(): Partial search results returned: Sizelimit exceeded
            }
            catch (ErrorException $e) {

                log_message('error', json_encode($e));
            }
Community
  • 1
  • 1
RedGazelle
  • 47
  • 1
  • 8

1 Answers1

1

This was answered here: Exception message not being shown if json_encode is applied to the output

In my code, all I had to do was the following:

catch (Exception $e) {
    $data['exception'] = $e->getMessage();
}
echo json_encode($data);

This was enough to get my exception data to come through my JSON array.

b1kjsh
  • 1,109
  • 2
  • 9
  • 21