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

4

9

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?

Eugeneeugenia answered 20/1, 2012 at 12:9 Comment(0)
H
9

It is a php.ini setting called html_errors.

Huddleston answered 20/1, 2012 at 12:11 Comment(0)
S
7

In your php.ini you can set three different settings to change how HTML errors are displayed.

I use the following in my development environment:

html_errors = On
error_prepend_string = "<pre>"
error_append_string = "</pre>"
Stamey answered 20/1, 2012 at 12:14 Comment(1)
Also don't forget to set enough log_errors_max_len.Overshine
E
0

There may be some configuration option that I can't find to do this - I suspect there is, since the HTML is not present on the command line.

A dirty work around is to pass the strings through html_entity_decode(strip_tags($str)).

EDIT As Andy Pieters has pointed out (he is obviously better at reading docs than I am) there is a configuration option called html_errors to control this. I will leave this answer here in case for some reason you cannot modify this, but you should probably accept his answer.

Elias answered 20/1, 2012 at 12:12 Comment(0)
I
0

If you are running the CLI and only want to do this temporarily, you can set these options on the command line to avoid editing php.ini:

/usr/bin/php -d html_errors=0 -d error_prepend_string= -d error_append_string= example.php

This will hide all HTML from the error messages while running that script (assuming the script itself doesn't re-enable them) without needing to edit php.ini.

Iraq answered 23/5 at 2:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.