When I'm developing my REST API in PHP I'm working with application/json
output, so when I get errors while testing in the browser they look like this:
<b>Fatal error</b>: Uncaught exception 'Exception' with message 'PDO caught an error:
array(3) {
[0]=>
string(5) "42000"
[1]=>
int(1065)
[2]=>
string(15) "Query was empty"
}
And it gets worse when I get large stack traces and stuff. So is there a flag I can set telling PHP that I want my errors unescaped and in raw text?
The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.
Exception handling is a powerful mechanism of PHP, which is used to handle runtime errors (runtime errors are called exceptions). So that the normal flow of the application can be maintained. The main purpose of using exception handling is to maintain the normal execution of the application.
An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.
Fatal errors or recoverable fatal errors now throw instances of Error in PHP 7 or higher versions. Like any other exceptions, Error objects can be caught using a try/catch block.
In your php.ini you can set three different settings to change how HTML errors are displayed.
html_errors
error_prepend_string
String to output before an error messageerror_append_string
String to output after an error messageI use the following in my development environment:
html_errors = On
error_prepend_string = "<pre>"
error_append_string = "</pre>"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With