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));
            }
1
  • Maybe there's no JSON encoding for exceptions. Commented Mar 24, 2017 at 3:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.