5 * The Simplicity Error module provides advanced error handling and logging.
6 * To enable error logging in your application you need to place this module
7 * near the start of your module chain.
9 * @author John Le Drew <jp@antz29.com>
10 * @copyright Copyright (c) 2002, John Le Drew
11 * @license http://www.opensource.org/licenses/mit-license.php MIT License
12 * @link http://www.simplicityphp.com/modules/auth Simplicity Auth
13 * @version 0.1.0-alpha
16 class smp_ErrorModule
extends smp_Module
18 private $_handled = false;
20 protected function init()
23 $this->setDefault('template',ROOT
."modules{$ds}admin{$ds}views{$ds}error{$ds}details{$ds}default.phtml");
24 $this->setDefault('logging',true);
27 public function exec($args=array())
29 ini_set('display_errors',1);
30 error_reporting(E_ALL | E_NOTICE | E_STRICT
);
31 register_shutdown_function(array($this,'shutdown'));
32 set_error_handler(array($this,'error'));
33 set_exception_handler(array($this,'exception'));
35 if ($this->getConfig('logging')) {
36 $db = realpath(sys_get_temp_dir()).DS
.md5(ROOT
).'.smp.errors.sq3';
37 $dsn = "pdo_sqlite:sqlite:{$db}";
38 smp_DataConnectionManager
::connect('errors',$dsn);
42 public function error($errno,$errstr,$errfile,$errline)
44 restore_error_handler();
45 restore_exception_handler();
47 $this->_handled
= true;
49 $e = new smp_Exception($errstr,$errline,$errno,$errfile);
54 public function shutdown()
56 restore_error_handler();
57 restore_exception_handler();
59 if ($this->_handled
) return false;
61 $e = error_get_last();
64 $e = new smp_Exception('Fatal error: '.$e['message'],$e['line'],$e['type'],$e['file'],$GLOBALS,array());
69 public function exception(Exception
$e)
71 restore_error_handler();
72 restore_exception_handler();
74 $this->_handled
= true;
76 if (!($e instanceof smp_Exception
)) {
77 $e = new smp_Exception($e->getMessage(),$e->getLine(),$e->getCode(),$e->getFile());
83 private function store(smp_Exception
$e)
85 if (!$this->getConfig('logging')) return $this->render($e);
87 if (!$db = smp_DataConnectionManager
::getConnection('errors')) return $this->render($e);
89 $db->query("create table if not exists error_log (id integer primary key autoincrement, date, msg, file, line, code, trace, headers, context)");
91 $t = smp_DataTable
::create('error_log',$db);
94 $t->msg
= $e->getMessage();
95 $t->file
= $e->getFile();
96 $t->line
= $e->getLine();
97 $t->code
= $e->getCode();
98 $t->trace
= serialize($e->getSafeTrace());
99 $t->headers
= serialize($e->getHeaders());
100 $t->context
= print_r($e->getContext(),true);
106 private function render(smp_Exception
$e)
108 restore_error_handler();
109 restore_exception_handler();
111 if (ob_get_level()) ob_end_clean();
113 ob_implicit_flush(true);
115 header("HTTP/1.0 500 Internal Server Error");
117 $t = new smp_Template($this->getConfig('template'));
119 $t->set('code',$e->getCode());
120 $t->set('message',$e->getMessage());
121 $t->set('file',$e->getFile());
122 $t->set('line',$e->getLine());
123 $t->set('trace',$e->getSafeTrace());
124 $t->set('context',print_r($e->getContext(),true));
125 $t->set('headers',$e->getHeaders());
127 die($t->renderTemplate());