Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Error handling

Tags:

php

Thank you one and all ahead of time.

I am currently in the process of tweaking/improving a MVC framework I wrote from scratch for my company. It is relatively new, so it is certainly incomplete. I need to incorporate error handling into the framework (everything should have access to error handling) and it should be able to handle different types and levels of errors (User errors and Framework errors). My question is what is the best way and best mechanism to do this? I know of PHP 5 exception handling and PEAR's different error mechanism, but I have never used any of them. I need something efficient and easy to use.

Would it be better to create my own error handling or use something already made? Any suggestions, tips, questions are certainly welcomed. I would ultimately think it sweetness to somehow register the error handler with PHP so that I would just need to throw the error and then decide what to do with it and whether to continue.

EDIT: Sorry, I should of provided a more details about what type of errors I wanted to log. I am looking to log 2 main types of errors: User and Framework.

For user errors, I mean things like bad urls (404), illegal access to restricted pages, etc. I know I could just reroute to the home page or just blurt out a JavaScript dialog box, but I want to be able to elegently handle these errors and add more user errors as they become evident.

By Framework errors I means things like cannot connect to the database, someone deleted a database table on accident or deleted a file somehow, etc.

Also, I will take care of development and live server handling.

like image 577
Robert DeBoer Avatar asked Jul 03 '09 22:07

Robert DeBoer


People also ask

How do I check PHP errors?

Quickly Show All PHP Errors The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

How do I stop PHP from showing errors?

To turn off or disable error reporting in PHP, set the value to zero.


2 Answers

I would ultimately think it sweetness to somehow register the error handler with PHP so that I would just need to throw the error and then decide what to do with it and whether to continue.

You can do exactly that, with set_error_handler() and set_exception_handler().

There is no "one right way" to do error handling, but here are some things to consider.

  • trigger_error() is similar to throw new Exception in that they both escape out of the current execution immediately, only trigger_error() is not catchable.
  • How do you want errors handled on a dev environment (show on screen?) versus in a production environment (logged and emailed?)
  • You can use the above functions to essentially "convert" errors into exceptions, or vice versa
  • Not all error types can be handled with a custom error handler
like image 102
Peter Bailey Avatar answered Oct 02 '22 13:10

Peter Bailey


Here's some things that I usually do:

  • Use a global configuration setting or flag to switch between development and production.
  • Don't use php errors when you have a choice: Prefer exceptions for your own error handeling. If you're using a library that doesn't use exceptions, detect the errors and throw your own exceptions.
  • Use a top level exception catcher that displays exceptions in a way that's easy to read. If you place this try-catch-block strategically, you don't have to register a global exception handler.
  • Always develop with error_handeling(E_ALL | E_STRICT)
  • Catch PHP warnings and notices using set_error_handler(), and halt execution. This elimates a lot of bugs in advance, with very solid code as a result.
  • The global error handeling code should be very light-weight, in order to avoid errors. There's always a risk for recursion when dealing with global error handlers.
  • If the system is in production mode, don't display any details: Log the error, and generate a unique identifier that the user can refer to if they want to file a bug or report the error.
like image 42
Emil H Avatar answered Oct 02 '22 14:10

Emil H