1 <?php rcs_id('$Id: ErrorManager.php,v 1.11 2002-01-21 06:55:47 dairiki Exp $');
4 define ('EM_FATAL_ERRORS',
5 E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
6 define ('EM_WARNING_ERRORS',
7 E_WARNING | E_CORE_WARNING | E_COMPILE_WARNING | E_USER_WARNING);
8 define ('EM_NOTICE_ERRORS', E_NOTICE | E_USER_NOTICE);
12 * A class which allows custom handling of PHP errors.
14 * This is a singleton class. There should only be one instance
15 * of it --- you can access the one instance via $GLOBALS['ErrorManager'].
24 * As this is a singleton class, you should never call this.
27 function ErrorManager() {
28 $this->_handlers = array();
29 $this->_fatal_handler = false;
30 $this->_postpone_mask = 0;
31 $this->_postponed_errors = array();
33 set_error_handler('ErrorManager_errorHandler');
37 * Get mask indicating which errors are currently being postponed.
39 * @return int The current postponed error mask.
41 function getPostponedErrorMask() {
42 return $this->_postpone_mask;
46 * Set mask indicating which errors to postpone.
48 * The default value of the postpone mask is zero (no errors postponed.)
50 * When you set this mask, any queue errors which do not match tne new
54 * @param $newmask int The new value for the mask.
56 function setPostponedErrorMask($newmask) {
57 $this->_postpone_mask = $newmask;
58 $this->_flush_errors($newmask);
62 * Report any queued error messages.
65 function flushPostponedErrors() {
66 $this->_flush_errors();
70 * Get postponed errors, formatted as HTML.
72 * This also flushes the postponed error queue.
74 * @return string HTML describing any queued errors.
76 function getPostponedErrorsAsHTML() {
78 $this->flushPostponedErrors();
79 $html = ob_get_contents();
85 return HTML::div(array('class' => 'errors'),
86 HTML::h4("PHP Warnings"),
91 * Push a custom error handler on the handler stack.
93 * Sometimes one is performing an operation where one expects
94 * certain errors or warnings. In this case, one might not want
95 * these errors reported in the normal manner. Installing a custom
96 * error handler via this method allows one to intercept such
99 * An error handler installed via this method should be either a
100 * function or an object method taking one argument: a PhpError
103 * The error handler should return either:
105 * <dt> False <dd> If it has not handled the error. In this case,
106 * error processing will proceed as if the handler
107 * had never been called: the error will be passed
108 * to the next handler in the stack, or the
109 * default handler, if there are no more handlers
112 * <dt> True <dd> If the handler has handled the error. If the
113 * error was a non-fatal one, no further processing
114 * will be done. If it was a fatal error, the
115 * ErrorManager will still terminate the PHP
116 * process (see setFatalHandler.)
118 * <dt> A PhpError object <dd> The error is not considered
119 * handled, and will be passed on to
120 * the next handler(s) in the stack
121 * (or the default handler). The
122 * returned PhpError need not be the
123 * same as the one passed to the
124 * handler. This allows the handler to
125 * "adjust" the error message.
128 * @param $handler WikiCallback Handler to call.
130 function pushErrorHandler($handler) {
131 array_unshift($this->_handlers, $handler);
135 * Pop an error handler off the handler stack.
138 function popErrorHandler() {
139 return array_shift($this->_handlers);
143 * Set a termination handler.
145 * This handler will be called upon fatal errors. The handler
146 * gets passed one argument: a PhpError object describing the
150 * @param $handler WikiCallback Callback to call on fatal errors.
152 function setFatalHandler($handler) {
153 $this->_fatal_handler = $handler;
159 * The error is passed through any registered error handlers, and
160 * then either reported or postponed.
163 * @param $error object A PhpError object.
165 function handleError($error) {
168 if (!empty($in_handler)) {
169 echo "<p>ErrorManager: "._("error while handling error:")."</p>\n";
170 echo $error->printError();
175 foreach ($this->_handlers as $handler) {
176 $result = $handler->call($error);
178 continue; // Handler did not handle error.
180 elseif (is_object($result)) {
181 // handler filtered the result. Still should pass to
182 // the rest of the chain.
183 if ($error->isFatal()) {
184 // Don't let handlers make fatal errors non-fatal.
185 $result->errno = $error->errno;
190 // Handler handled error.
191 if (!$error->isFatal()) {
199 // Error was either fatal, or was not handled by a handler.
200 // Handle it ourself.
201 if ($error->isFatal()) {
204 else if (($error->errno & error_reporting()) != 0) {
205 if (($error->errno & $this->_postpone_mask) != 0) {
206 $this->_postponed_errors[] = $error;
209 $error->printError();
218 function _die($error) {
219 $error->printError();
220 $this->_flush_errors();
221 if ($this->_fatal_handler)
222 $this->_fatal_handler->call($error);
229 function _flush_errors($keep_mask = 0) {
230 $errors = &$this->_postponed_errors;
231 foreach ($errors as $key => $error) {
232 if (($error->errno & $keep_mask) != 0)
234 unset($errors[$key]);
235 $error->printError();
241 * Global error handler for class ErrorManager.
243 * This is necessary since PHP's set_error_handler() does not allow
244 * one to set an object method as a handler.
248 function ErrorManager_errorHandler($errno, $errstr, $errfile, $errline)
250 global $ErrorManager;
251 $error = new PhpError($errno, $errstr, $errfile, $errline);
252 $ErrorManager->handleError($error);
257 * A class representing a PHP error report.
259 * @see The PHP documentation for set_error_handler at
260 * http://php.net/manual/en/function.set-error-handler.php .
269 * The PHP error message.
274 * The source file where the error occurred.
279 * The line number (in $this->errfile) where the error occured.
284 * Construct a new PhpError.
286 * @param $errstr string
287 * @param $errfile string
288 * @param $errline int
290 function PhpError($errno, $errstr, $errfile, $errline) {
291 $this->errno = $errno;
292 $this->errstr = $errstr;
293 $this->errfile = $errfile;
294 $this->errline = $errline;
298 * Determine whether this is a fatal error.
299 * @return boolean True if this is a fatal error.
302 return ($this->errno & (EM_WARNING_ERRORS|EM_NOTICE_ERRORS)) == 0;
306 * Determine whether this is a warning level error.
309 function isWarning() {
310 return ($this->errno & EM_WARNING_ERRORS) != 0;
314 * Determine whether this is a notice level error.
317 function isNotice() {
318 return ($this->errno & EM_NOTICE_ERRORS) != 0;
322 * Get a printable, HTML, message detailing this error.
323 * @return string The detailed error message.
325 function getDetail() {
326 if ($this->isNotice())
328 else if ($this->isWarning())
333 $errfile = ereg_replace('^' . getcwd() . '/', '', $this->errfile);
335 $lines = explode("\n", $this->errstr);
336 $errstr = htmlspecialchars(array_shift($lines));
337 foreach ($lines as $key => $line)
338 $lines[$key] = "<li>" . htmlspecialchars($line) . "</li>";
340 $errstr .= "<ul>\n" . join("\n", $lines) . "\n</ul>";
342 return sprintf("<p class='error'>%s:%d: %s[%d]: %s</p>\n",
343 htmlspecialchars($errfile),
344 $this->errline, $what, $this->errno,
349 * Print an HTMLified version of this error.
352 function printError() {
353 echo $this->getDetail();
357 if (!isset($GLOBALS['ErrorManager'])) {
358 $GLOBALS['ErrorManager'] = new ErrorManager;
361 // (c-file-style: "gnu")
366 // c-hanging-comment-ender-p: nil
367 // indent-tabs-mode: nil