I use AngularJs for a single page application and communicate with serverside PHP via JSON. The php header sets JSON, but the error reporting from php:
php_flag display_errors 1
php_flag display_startup_errors 1
php_value error_reporting 32767
is html and not matching the content-type header of the regular answer header('Content-Type: application/json;charset=utf-8');
Therefore angularjs constantly throws on php errors. Should I use multicontent types or what to do?
If you must return PHP error/exceptions to the client-side, which is not recommended (but I know, it's easier for development), you gonna need a custom error/uncaught-exception handler for PHP. This way you're able to customize how the errors/exceptions are shown.
Here's a sample code that outputs errors and uncaught exceptions as JSON objects.
But that's not all; Since user-defined error handlers are not able to handle fatal errors, fatal error messages will still be displayed. You need to disable displaying errors with a call to
ini_set()
:So how to handle fatal errors? Fatal errors can be handled on shutdown with
register_shutdown_function()
. In the shutdown handler, we need to get the last error information with a call toerror_get_last()
. So:Then on the javascript side of things, you have to add an error callback and show the error information to the user.
After all, why not using a mature error/exception handler package instead of implementing all of these? Meet Whoops.