1 <?php rcs_id('$Id: ErrorManager.php,v 1.5 2001-12-28 09:53:06 carstenklapp 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 * Push a custom error handler on the handler stack.
72 * Sometimes one is performing an operation where one expects certain errors
73 * or warnings. In this case, one might not want these errors reported
74 * in the normal manner. Installing a custom error handler via this method
75 * allows one to intercept such errors.
77 * An error handler installed via this method should be either a
78 * function or an object method taking one argument: a PhpError object.
80 * The error handler should return either:
82 * <dt> False <dd> If it has not handled the error. In this case, error
83 * processing will proceed as if the handler had never been called:
84 * the error will be passed to the next handler in the stack,
85 * or the default handler, if there are no more handlers in the stack.
86 * <dt> True <dd> If the handler has handled the error. If the error was
87 * a non-fatal one, no further processing will be done.
88 * If it was a fatal error, the ErrorManager will still
89 * terminate the PHP process (see setFatalHandler.)
90 * <dt> A PhpError object
91 * <dd> The error is not considered
92 * handled, and will be passed on to the next handler(s) in the stack
93 * (or the default handler).
94 * The returned PhpError need not be the same as the one passed to the
95 * handler. This allows the handler to "adjust" the error message.
98 * @param $handler WikiCallback Handler to call.
100 function pushErrorHandler($handler) {
101 array_unshift($this->_handlers, $handler);
105 * Pop an error handler off the handler stack.
108 function popErrorHandler() {
109 return array_shift($this->_handlers);
113 * Set a termination handler.
115 * This handler will be called upon fatal errors. The handler gets passed
116 * one argument: a PhpError object describing the fatal error.
119 * @param $handler WikiCallback Callback to call on fatal errors.
121 function setFatalHandler($handler) {
122 $this->_fatal_handler = $handler;
128 * The error is passed through any registered error handlers,
129 * and then either reported or postponed.
132 * @param $error object A PhpError object.
134 function handleError($error) {
137 if (!empty($in_handler)) {
138 echo "<p>ErrorManager: "._("error while handling error:")."</p>\n";
139 echo $error->printError();
144 foreach ($this->_handlers as $handler) {
145 $result = $handler->call($error);
147 continue; // Handler did not handle error.
149 elseif (is_object($result)) {
150 // handler filtered the result. Still should pass to the
151 // rest of the chain.
152 if ($error->isFatal()) {
153 // Don't let handlers make fatal errors non-fatal.
154 $result->errno = $error->errno;
159 // Handler handled error.
160 if (!$error->isFatal()) {
168 // Error was either fatal, or was not handled by a handler.
169 // Handle it ourself.
170 if ($error->isFatal()) {
173 else if (($error->errno & error_reporting()) != 0) {
174 if (($error->errno & $this->_postpone_mask) != 0) {
175 $this->_postponed_errors[] = $error;
178 $error->printError();
187 function _die($error) {
188 $error->printError();
189 $this->_flush_errors();
190 if ($this->_fatal_handler)
191 $this->_fatal_handler->call($error);
198 function _flush_errors($keep_mask = 0) {
199 $errors = &$this->_postponed_errors;
200 foreach ($errors as $key => $error) {
201 if (($error->errno & $keep_mask) != 0)
203 unset($errors[$key]);
204 $error->printError();
210 * Global error handler for class ErrorManager.
212 * This is necessary since PHP's set_error_handler() does not allow one to
213 * set an object method as a handler.
217 function ErrorManager_errorHandler($errno, $errstr, $errfile, $errline)
219 global $ErrorManager;
220 $error = new PhpError($errno, $errstr, $errfile, $errline);
221 $ErrorManager->handleError($error);
226 * A class representing a PHP error report.
228 * @see The PHP documentation for set_error_handler at
229 * http://php.net/manual/en/function.set-error-handler.php .
238 * The PHP error message.
243 * The source file where the error occurred.
248 * The line number (in $this->errfile) where the error occured.
253 * Construct a new PhpError.
255 * @param $errstr string
256 * @param $errfile string
257 * @param $errline int
259 function PhpError($errno, $errstr, $errfile, $errline) {
260 $this->errno = $errno;
261 $this->errstr = $errstr;
262 $this->errfile = $errfile;
263 $this->errline = $errline;
267 * Determine whether this is a fatal error.
268 * @return boolean True if this is a fatal error.
271 return ($this->errno & (EM_WARNING_ERRORS|EM_NOTICE_ERRORS)) == 0;
275 * Determine whether this is a warning level error.
278 function isWarning() {
279 return ($this->errno & EM_WARNING_ERRORS) != 0;
283 * Determine whether this is a notice level error.
286 function isNotice() {
287 return ($this->errno & EM_NOTICE_ERRORS) != 0;
291 * Get a printable, HTML, message detailing this error.
292 * @return string The detailed error message.
294 function getDetail() {
295 if ($this->isNotice())
297 else if ($this->isWarning())
302 $errfile = ereg_replace('^' . getcwd() . '/', '', $this->errfile);
304 $lines = explode("\n", $this->errstr);
305 $errstr = htmlspecialchars(array_shift($lines));
306 foreach ($lines as $key => $line)
307 $lines[$key] = "<li>" . htmlspecialchars($line) . "</li>";
309 $errstr .= "<ul>\n" . join("\n", $lines) . "\n</ul>";
311 return sprintf("<p class='error'>%s:%d: %s[%d]: %s</p>\n",
312 htmlspecialchars($errfile),
313 $this->errline, $what, $this->errno,
318 * Print an HTMLified version of this error.
321 function printError() {
322 echo $this->getDetail();
326 if (!isset($GLOBALS['ErrorManager'])) {
327 $GLOBALS['ErrorManager'] = new ErrorManager;
330 // (c-file-style: "gnu")
335 // c-hanging-comment-ender-p: nil
336 // indent-tabs-mode: nil