views:

195

answers:

3

Currently I don't have a very good system for managing error or success messages, they are pretty much written inline with the code. Can someone suggest a best practice or something you are doing to be able to properly manage a large list of error and success messages through out an application.

Think internationalization ... some applications will have language files, then the app just pulls the strings from the specific file ... I'm thinking of implementing a similar concept for error and success messages and am curious what others have done along the same lines?

+1  A: 

Here is a function that I use:

function checkErrors($type, $msg, $file, $line, $context) {
   echo "<h1>Error!</h1>";
   echo "An error occurred while executing this script. Please contact the <a href=mailto:[email protected]>webmaster</a> to report this error.";
   echo "<p />";
   echo "Here is the information provided by the script:";
   echo "<hr><pre>";
   echo "Error code: $type<br />";
   echo "Error message: $msg<br />";
   echo "Script name and line number of error: $file:$line<br />";
   $variable_state = array_pop($context);
   echo "Variable state when error occurred: ";
   print_r($variable_state);
   echo "</pre><hr>";
} 
set_error_handler('checkErrors');

it will give you all the errors and warnings that your PHP code produces, along with create an error page for you in the case that someone visits a page that contains an error.

Hope this helps!

Frederico
ya but the actual messages are still spread across your entire application, im looking for a way to manage the actual messages that are used throughout an application
farinspace
A: 

Since I use a single-access-point style system that bootstraps the application and includes the correct file (it's basically a controller system) I can use:

<?php
try {
    require 'bootstrap.php';
    // dispatch controller
    require "controllers/$controller.php";
} catch (Exception $e) {
    // echo info
} catch (LibraryException $le) {
    // library specific exception
}

Then when I want to throw an error I just:

throw new Exception('An error occurred.');

Can someone suggest a best practice or something you are doing to be able to properly manage a large list of error and success messages through out an application.

This is how I manage errors by bulk, but not how I could manage a list. I could grep for error messages I find, it's not really very organised though.

Ross
A best practice would have LibraryException extend Exception, then put the LE catch block before the Exception catch block.
James Socol
A: 

You might use something like PHP Documentor and include errors and messages in the docs for each class.

http://www.phpdoc.org/

Eli