2

From my understanding of PHP documentation, setting custom error handler (which cannot catch all the errors) deactivates the default php error handler (which I guess can catch all the errors:).

If this is correct, is there any purpose of setting custom error handler? I always keep error_reporting(-1), so I should get all errors in the error log anyway, right?

As for the user experience, I cannot see the purpose of making these errors manifest in a custom way - why would you ever want users to see them? In production I always set display_errors to off.

1
  • 2
    From docs: "If the function returns FALSE then the normal error handler continues." Commented Jun 16, 2011 at 13:40

3 Answers 3

4

In production you probably want to tell the user something when your site crashes, and set_error_handler provides a way of rendering pretty error messages while preventing the user from seeing ugly things like code and line numbers. It catches any runtime errors that the regular PHP error handler catches.

There is no reason to avoid using set_error_handler, and it's certainly preferable to simply displaying nothing when an error occurs.

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

Comments

3

Setting a custom error handler is for setting a catch-all handler that takes care of the errors that aren't handled elsewhere. It can catch all errors, except for the ones where your script isn't ran at all (such as fatal syntax errors).

I should also point out that if you return false from your error handling function, then the built-in PHP error handler will step in.

You may want this for special logging of any specific kinds of exceptions in your application.

1 Comment

Just wanted to add that custom error logger works well with debug_backtrace().
3

If this is correct, is there any purpose of setting custom error handler? I always keep error_reporting(-1), so I should get all errors in the error log anyway, right?

error_reporting dictates which errors should be reported (e.g. warning, notices, etc.). This has nothing to do with them showing up in the logs; if you set error_reporting to 0 it won't be logged. If you want it to be logged, but not displayed to the customer, use display_errors = Off.

As for the user experience, I cannot see the purpose of making these errors manifest in a custom way - why would you ever want users to see them? In production I always set display_errors to off.

I'd want to see a message that "something" went wrong, rather than seeing a "white screen of death". This may be simple to do with a custom error handler. So, yeah, it does have a purpose.

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.