1 <?php rcs_id('$Id: ErrorManager.php,v 1.20 2004-04-13 14:58:03 rurban Exp $');
3 require_once('lib/HtmlElement.php');
5 define ('EM_FATAL_ERRORS',
6 E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
7 define ('EM_WARNING_ERRORS',
8 E_WARNING | E_CORE_WARNING | E_COMPILE_WARNING | E_USER_WARNING);
9 define ('EM_NOTICE_ERRORS', E_NOTICE | E_USER_NOTICE);
11 assert_options (ASSERT_CALLBACK, 'wiki_assert_handler');
13 function wiki_assert_handler ($file, $line, $code) {
14 ErrorManager_errorHandler( $code, sprintf("<br />%s:%s: %s: Assertion failed <br />", $file, $line, $code), $file, $line);
18 * A class which allows custom handling of PHP errors.
20 * This is a singleton class. There should only be one instance
21 * of it --- you can access the one instance via $GLOBALS['ErrorManager'].
30 * As this is a singleton class, you should never call this.
33 function ErrorManager() {
34 $this->_handlers = array();
35 $this->_fatal_handler = false;
36 $this->_postpone_mask = 0;
37 $this->_postponed_errors = array();
39 set_error_handler('ErrorManager_errorHandler');
43 * Get mask indicating which errors are currently being postponed.
45 * @return int The current postponed error mask.
47 function getPostponedErrorMask() {
48 return $this->_postpone_mask;
52 * Set mask indicating which errors to postpone.
54 * The default value of the postpone mask is zero (no errors postponed.)
56 * When you set this mask, any queue errors which do not match the new
60 * @param $newmask int The new value for the mask.
62 function setPostponedErrorMask($newmask) {
63 $this->_postpone_mask = $newmask;
64 if (function_exists('PrintXML'))
65 PrintXML($this->_flush_errors($newmask));
67 echo($this->_flush_errors($newmask));
72 * Report any queued error messages.
75 function flushPostponedErrors() {
76 if (function_exists('PrintXML'))
77 PrintXML($this->_flush_errors());
79 echo $this->_flush_errors();
83 * Get postponed errors, formatted as HTML.
85 * This also flushes the postponed error queue.
87 * @return object HTML describing any queued errors (or false, if none).
89 function getPostponedErrorsAsHTML() {
90 $flushed = $this->_flush_errors();
91 if ($flushed->isEmpty())
93 $html = HTML::div(array('class' => 'errors'),
94 HTML::h4("PHP Warnings"));
95 $html->pushContent($flushed);
100 * Push a custom error handler on the handler stack.
102 * Sometimes one is performing an operation where one expects
103 * certain errors or warnings. In this case, one might not want
104 * these errors reported in the normal manner. Installing a custom
105 * error handler via this method allows one to intercept such
108 * An error handler installed via this method should be either a
109 * function or an object method taking one argument: a PhpError
112 * The error handler should return either:
114 * <dt> False <dd> If it has not handled the error. In this case,
115 * error processing will proceed as if the handler
116 * had never been called: the error will be passed
117 * to the next handler in the stack, or the
118 * default handler, if there are no more handlers
121 * <dt> True <dd> If the handler has handled the error. If the
122 * error was a non-fatal one, no further processing
123 * will be done. If it was a fatal error, the
124 * ErrorManager will still terminate the PHP
125 * process (see setFatalHandler.)
127 * <dt> A PhpError object <dd> The error is not considered
128 * handled, and will be passed on to
129 * the next handler(s) in the stack
130 * (or the default handler). The
131 * returned PhpError need not be the
132 * same as the one passed to the
133 * handler. This allows the handler to
134 * "adjust" the error message.
137 * @param $handler WikiCallback Handler to call.
139 function pushErrorHandler($handler) {
140 array_unshift($this->_handlers, $handler);
144 * Pop an error handler off the handler stack.
147 function popErrorHandler() {
148 return array_shift($this->_handlers);
152 * Set a termination handler.
154 * This handler will be called upon fatal errors. The handler
155 * gets passed one argument: a PhpError object describing the
159 * @param $handler WikiCallback Callback to call on fatal errors.
161 function setFatalHandler($handler) {
162 $this->_fatal_handler = $handler;
168 * The error is passed through any registered error handlers, and
169 * then either reported or postponed.
172 * @param $error object A PhpError object.
174 function handleError($error) {
177 if (!empty($in_handler)) {
178 $msg = $error->_getDetail();
179 $msg->unshiftContent(HTML::h2(fmt("%s: error while handling error:",
186 foreach ($this->_handlers as $handler) {
187 $result = $handler->call($error);
189 continue; // Handler did not handle error.
191 elseif (is_object($result)) {
192 // handler filtered the result. Still should pass to
193 // the rest of the chain.
194 if ($error->isFatal()) {
195 // Don't let handlers make fatal errors non-fatal.
196 $result->errno = $error->errno;
201 // Handler handled error.
202 if (!$error->isFatal()) {
210 // Error was either fatal, or was not handled by a handler.
211 // Handle it ourself.
212 if ($error->isFatal()) {
215 else if (($error->errno & error_reporting()) != 0) {
216 if (($error->errno & $this->_postpone_mask) != 0) {
217 $this->_postponed_errors[] = $error;
226 function warning($msg, $errno=E_USER_NOTICE) {
227 $this->handleError(new PhpWikiError($errno, $msg));
233 function _die($error) {
235 PrintXML($this->_flush_errors());
236 if ($this->_fatal_handler)
237 $this->_fatal_handler->call($error);
244 function _flush_errors($keep_mask = 0) {
245 $errors = &$this->_postponed_errors;
247 foreach ($errors as $key => $error) {
248 if (($error->errno & $keep_mask) != 0)
250 unset($errors[$key]);
251 $flushed->pushContent($error);
258 * Global error handler for class ErrorManager.
260 * This is necessary since PHP's set_error_handler() does not allow
261 * one to set an object method as a handler.
265 function ErrorManager_errorHandler($errno, $errstr, $errfile, $errline)
267 if (!isset($GLOBALS['ErrorManager'])) {
268 $GLOBALS['ErrorManager'] = new ErrorManager;
271 $error = new PhpError($errno, $errstr, $errfile, $errline);
272 $GLOBALS['ErrorManager']->handleError($error);
277 * A class representing a PHP error report.
279 * @see The PHP documentation for set_error_handler at
280 * http://php.net/manual/en/function.set-error-handler.php .
289 * The PHP error message.
294 * The source file where the error occurred.
299 * The line number (in $this->errfile) where the error occured.
304 * Construct a new PhpError.
306 * @param $errstr string
307 * @param $errfile string
308 * @param $errline int
310 function PhpError($errno, $errstr, $errfile, $errline) {
311 $this->errno = $errno;
312 $this->errstr = $errstr;
313 $this->errfile = $errfile;
314 $this->errline = $errline;
318 * Determine whether this is a fatal error.
319 * @return boolean True if this is a fatal error.
322 return ($this->errno & (EM_WARNING_ERRORS|EM_NOTICE_ERRORS)) == 0;
326 * Determine whether this is a warning level error.
329 function isWarning() {
330 return ($this->errno & EM_WARNING_ERRORS) != 0;
334 * Determine whether this is a notice level error.
337 function isNotice() {
338 return ($this->errno & EM_NOTICE_ERRORS) != 0;
342 * Get a printable, HTML, message detailing this error.
343 * @return object The detailed error message.
345 function _getDetail() {
346 if ($this->isNotice())
348 else if ($this->isWarning())
353 $errfile = ereg_replace('^' . getcwd() . '/', '', $this->errfile);
354 $lines = explode("\n", $this->errstr);
356 $msg = sprintf("%s:%d: %s[%d]: %s",
357 $errfile, $this->errline,
359 array_shift($lines));
361 $html = HTML::div(array('class' => 'error'), HTML::p($msg));
365 foreach ($lines as $line)
366 $list->pushContent(HTML::li($line));
367 $html->pushContent($list);
374 * Print an HTMLified version of this error.
377 function printXML() {
378 PrintXML($this->_getDetail());
382 * Return an HTMLified version of this error.
385 return AsXML($this->_getDetail());
389 * Return a plain-text version of this error.
391 function asString() {
392 return AsString($this->_getDetail());
397 * A class representing a PhpWiki warning.
399 * This is essentially the same as a PhpError, except that the
400 * error message is quieter: no source line, etc...
402 class PhpWikiError extends PhpError {
404 * Construct a new PhpError.
406 * @param $errstr string
408 function PhpWikiError($errno, $errstr) {
409 $this->PhpError($errno, $errstr, '?', '?');
412 function _getDetail() {
413 if ($this->isNotice())
415 else if ($this->isWarning())
420 return HTML::div(array('class' => 'error'), HTML::p("$what: $this->errstr"));
425 if (!isset($GLOBALS['ErrorManager'])) {
426 $GLOBALS['ErrorManager'] = new ErrorManager;
430 // (c-file-style: "gnu")
435 // c-hanging-comment-ender-p: nil
436 // indent-tabs-mode: nil