0

If your website catches an exception that you want reported back to the users as feedback, what is an elegant way to handle it without running into these problems:

  1. The error happened on the middle of the page, and if you directly print the error message when caught it will be sitting somewhere in the middle of a broken page.

  2. Using ob_start() can make sure you only print content after it has been processed and there are no errors, fixing the issue at 1), but it carries a performance hit that would best be avoided.

  3. Redirecting to a new URL or the same one after catching an error can work really well (like redirecting back to a login form if the user posted the wrong password, with an error messsage, or to a generic page for more serious, application crashing errors). This leaves you prone to a redirection chain of errors though (recursive), that will time out the requests eventually leaving the user or the developer with no clear error message at all. Always redirecting on errors, especially the application crashing errors, seems to be a bad idea because of this.

  4. I noticed using ob_clean() WITHOUT ob_start() works on my project, deleting the echoed content already on a page. But the php manual states it should not work without ob_start(), and it should not even clear the echoed HTML, merely what was on the buffer. What is going on here?

  5. Rather than ob_start, you could save all your print/echo statements in an $array = array(). The performance hit is lesser as far as I understand, but that can make the code ugly to deal with.

    Appreciate the help

Community
  • 1
  • 1
Rob
  • 21
  • 1
  • 5
  • First of all, if there is some runtime error (exception, fatal) normal visitor should see it. Instead you should catch exceptions and implement error handlers that will display some *visitor-friendly* text (e.g. *There was an unexpected error, we are working hardly to fix it!*) and log the error/exception so that you can really fix it. If it is user error (wrong input) then all you need is to just display the user appropriate error message somewhere on the page (in the form under the field or in the *message area*). – shadyyx Oct 09 '14 at 12:05
  • yes, I throw custom and regular exceptions on my project, loading appropriate messages to the users. The problem however, is how to then display those messages without falling on one of the pitfalls above – Rob Oct 09 '14 at 12:11

1 Answers1

0

Something that I do is that I have an index that includes the pages needed, so for example, if you wanted to see the contact page, you'd request index.php?page=contact

The code looks something like this

$return_code = 1;
$return_code = include($page.'.php');
if ($return_code != 1) {
    echo '<div class="error">'.$return_code.'</div>';
}

Inside your contact page, you do whatever stuff you need to do, and if there is an error and you'd like to stop execution of the page and just display the error, simple do a return

<?php
// If an error happens, simply
return "Error text";
?>
Ali
  • 3,479
  • 4
  • 16
  • 31
  • Wouldn't that cause the contact's page already echoed/printed content to display on the page, falling on the pitfall 1) I mentioned? – Rob Oct 09 '14 at 12:21
  • No, if there's a return on the page then it won't display anything but the error. – Ali Oct 09 '14 at 12:27
  • Sorry but I don't understand how the return on the contacts.php will prevent any content from being displayed. If your contacts.php had these two lines 1) print "contact" 2) return errorMessage; contact would still be displayed – Rob Oct 09 '14 at 12:51
  • From the php documentation: Handling Returns: include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1. It is possible to execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it. – Ali Oct 09 '14 at 12:58
  • I understand the return terminates the script, but what if that script hat already outputed HTML to the user? – Rob Oct 09 '14 at 12:59
  • Then that HTML won't be displayed, because there is a return, that means the variable including the file will not hold the value 1, and thus it's never displayed. You can test it yourself. – Ali Oct 09 '14 at 13:01
  • I did actually, before replying to you. I created a file.php that echoed a line, and then did a return. Then I included that file from another file, but the echoed line showed up – Rob Oct 09 '14 at 13:02
  • file1.php: file 2.php: Sample text shows when you access file1.php – Rob Oct 09 '14 at 13:08