Jump to content

Need some suggestions on exception handling and redirect to an error page


fredyap1234

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.