Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell PHP to dump exceptions as raw text instead of HTML?

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]=&gt;
  string(5) &quot;42000&quot;
  [1]=&gt;
  int(1065)
  [2]=&gt;
  string(15) &quot;Query was empty&quot;
}

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?

like image 583
Hubro Avatar asked Jan 20 '12 12:01

Hubro


People also ask

How do you use PHP to handle exceptions?

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.

What is an exception how you can handle multiple exceptions in PHP?

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.

What are types exception in PHP?

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.

In which PHP version fatal errors become exceptions?

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.


1 Answers

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 message
  • error_append_string String to output after an error message

I use the following in my development environment:

html_errors = On
error_prepend_string = "<pre>"
error_append_string = "</pre>"
like image 136
sshow Avatar answered Sep 20 '22 18:09

sshow