[eluser]CtheB[/eluser]
Oke here is my /application/libraries/Exceptions.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Exceptions {
// TODO: This function should be mapped to handleException and there be logged to a database / file.
public function log_exception($severity, $message, $filepath, $line){}
TODO: map function to handleException and after that show custom 404 page.
public function show_404($page = '')
{
$heading = "404 Page Not Found";
$message = "The page you requested was not found.";
log_message('error', '404 Page Not Found --> '.$page);
echo $this->show_error($heading, $message, 'error_404', 404);
exit;
}
// All general errors will be catched here
public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
try
{
throw new Exception($heading.' '.$message, $status_code);
}
catch(Exception $e)
{
self::handleException($e);
}
}
// All normal PHP errors will be catched here
public function show_php_error($severity, $message, $filepath, $line)
{
try
{
throw new Exception($filepath.' '.$message.' on line '.$line, 9);
}
catch(Exception $e)
{
self::handleException($e);
}
}
public static function printException(Exception $e)
{
echo 'Caught '.get_class($e).'<br /><strong>'.$e->getFile().'</strong> on line <strong>'.$e->getLine().'</strong> ,<br />code: ' . $e->getCode() . "<br />Message: <strong>".htmlentities($e->getMessage()).'</strong><br />'."\n";
}
// In your application you can catch your errors with this function CI_Exceptions::handleException($e);
// TODO: If production enviroment: Log message to database if possible and show 404 page, otherwise to file. If test enviroment: printException.
public static function handleException(Exception $e)
{
self::printException($e);
}
}
set_exception_handler(array("CI_Exceptions", "handleException"));
Note: Because the Exception class is autoloaded by codeigniter, we don't need to load it ourselfs.
If we try to load the library we get other errors (the main error (bug) i get is, that my javascript & css file in the <head> of my html is showed in the <body> part of the html inside of firebug. (and the design is looking strange in IE)
Here is how to handle it inside controllers / libraries:
Code:
try
{
// some code wich can fail
}
catch(Exception $e)
{
CI_Exceptions::handleException($e);
}
I want to use it also inside of my Models like this:
Code:
public function get_keywords($subject)
{
$query = " SELECT word, hits
FROM search
WHERE word LIKE :subject
ORDER BY count DESC
LIMIT 0, 10
";
try
{
$stmt = $this->db->prepare($query);
$stmt->bindValue(':subject', "$subject%", PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
}
catch(PDOException $e)
{
CI_Exceptions::handleException($e);
}
return $result;
}
This last code fails. It throws an Uncaught exception, it is not cached by the handleException method.
If i load the Exception class myself inside the controller or model, this code WILL work, however, i'll get some
problems with the css displaying strange on the page (because the css file is not in the <head> anymore but it
appears to be in the <body> part of the html (if i look in firebug).
So the real problem is that, while my Exception class is instantiated by codeigniter, it will not work inside my models.