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.