fredyap1234 Posted December 3, 2010 Share Posted December 3, 2010 Hi guys, I'm reviewing a piece of small web application and the current application does not have any error / exception handling capability. If there is any error, it would simply show an error message followed by die;. I'm planning to implement a simple exception handling class to handle the errors. What I'm thinking is a simple redirect when an error is being caught together with an error code that correspond to an error message in a simple flat text file. The error page will then show an error message that corresponds to the code. Here's what I have so far. Would appreciate if the PHP experts here would give simple pointers to enhance it. <?php class MyException extends Exception {} try { throw new MyException("error.php"); } catch (MyException $e) { $file = $e->getMessage(); header("Location: $file?e=1"); } ?> This is what I have on my error.php page <?php $errorcode = $_GET['e']; function getErrorMessage($errorcode) { $errors = file("english.txt"); foreach ($errors as $error) { list ($key,$value) = explode(",",$error,2); $errorArray[$key] = $value; } return $errorArray[$errorcode]; } echo "Test <br />"; echo getMessageMap($errorcode); ?> As you can see here, exception class would redirect user to error.php if an error is caught together with a GET variable on the URL. On error.php page, it would GET the error code and then run it through a function to get the error message of the corresponding error code and then echos it out. Was wondering if this is a good practice? My ultimate goal here is to avoid displaying the error message itself on private includes file. Thank you in advance for your suggestions. Link to comment https://forums.phpfreaks.com/topic/220577-need-some-suggestions-on-exception-handling-and-redirect-to-an-error-page/ Share on other sites More sharing options...
JakeTheSnake3.0 Posted December 7, 2010 Share Posted December 7, 2010 It seems like a lot of overhead and upkeep for all that can go wrong. What you can do is have a global array which holds notices, alerts and errors. For every error encountered, add it to the array in string format right from the code...not a database. So for example: maincode: <?php if (!isset($_POST['name']) $GLOBALS['ERRORS'][] = 'You forgot the name!'; ?> lower down in the script... <?php if (count($GLOBAL['ERRORS']) > 0) { // Display errors } else { // Continue processing } ?> That way you don't need to redirect the user away from the form they were on (which is an annoyance). Link to comment https://forums.phpfreaks.com/topic/220577-need-some-suggestions-on-exception-handling-and-redirect-to-an-error-page/#findComment-1143834 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.