1 <?php rcs_id('$Id: ErrorManager.php,v 1.7 2002-01-10 04:03:12 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
73 * certain errors or warnings. In this case, one might not want
74 * these errors reported in the normal manner. Installing a custom
75 * error handler via this method allows one to intercept such
78 * An error handler installed via this method should be either a
79 * function or an object method taking one argument: a PhpError
82 * The error handler should return either:
84 * <dt> False <dd> If it has not handled the error. In this case,
85 * error processing will proceed as if the handler
86 * had never been called: the error will be passed
87 * to the next handler in the stack, or the
88 * default handler, if there are no more handlers
91 * <dt> True <dd> If the handler has handled the error. If the
92 * error was a non-fatal one, no further processing
93 * will be done. If it was a fatal error, the
94 * ErrorManager will still terminate the PHP
95 * process (see setFatalHandler.)
97 * <dt> A PhpError object <dd> The error is not considered
98 * handled, and will be passed on to
99 * the next handler(s) in the stack
100 * (or the default handler). The
101 * returned PhpError need not be the
102 * same as the one passed to the
103 * handler. This allows the handler to
104 * "adjust" the error message.
107 * @param $handler WikiCallback Handler to call.
109 function pushErrorHandler($handler) {
110 array_unshift($this->_handlers, $handler);
114 * Pop an error handler off the handler stack.
117 function popErrorHandler() {
118 return array_shift($this->_handlers);
122 * Set a termination handler.
124 * This handler will be called upon fatal errors. The handler
125 * gets passed one argument: a PhpError object describing the
129 * @param $handler WikiCallback Callback to call on fatal errors.
131 function setFatalHandler($handler) {
132 $this->_fatal_handler = $handler;
138 * The error is passed through any registered error handlers, and
139 * then either reported or postponed.
142 * @param $error object A PhpError object.
144 function handleError($error) {
147 if (!empty($in_handler)) {
148 echo "<p>ErrorManager: "._("error while handling error:")."</p>\n";
149 echo $error->printError();
154 foreach ($this->_handlers as $handler) {
155 $result = $handler->call($error);
157 continue; // Handler did not handle error.
159 elseif (is_object($result)) {
160 // handler filtered the result. Still should pass to
161 // the rest of the chain.
162 if ($error->isFatal()) {
163 // Don't let handlers make fatal errors non-fatal.
164 $result->errno = $error->errno;
169 // Handler handled error.
170 if (!$error->isFatal()) {
178 // Error was either fatal, or was not handled by a handler.
179 // Handle it ourself.
180 if ($error->isFatal()) {
183 else if (($error->errno & error_reporting()) != 0) {
184 if (($error->errno & $this->_postpone_mask) != 0) {
185 $this->_postponed_errors[] = $error;
188 $error->printError();
197 function _die($error) {
198 $error->printError();
199 $this->_flush_errors();
200 if ($this->_fatal_handler)
201 $this->_fatal_handler->call($error);
208 function _flush_errors($keep_mask = 0) {
209 $errors = &$this->_postponed_errors;
210 foreach ($errors as $key => $error) {
211 if (($error->errno & $keep_mask) != 0)
213 unset($errors[$key]);
214 $error->printError();
220 * Global error handler for class ErrorManager.
222 * This is necessary since PHP's set_error_handler() does not allow
223 * one to set an object method as a handler.
227 function ErrorManager_errorHandler($errno, $errstr, $errfile, $errline)
229 global $ErrorManager;
230 $error = new PhpError($errno, $errstr, $errfile, $errline);
231 $ErrorManager->handleError($error);
236 * A class representing a PHP error report.
238 * @see The PHP documentation for set_error_handler at
239 * http://php.net/manual/en/function.set-error-handler.php .
248 * The PHP error message.
253 * The source file where the error occurred.
258 * The line number (in $this->errfile) where the error occured.
263 * Construct a new PhpError.
265 * @param $errstr string
266 * @param $errfile string
267 * @param $errline int
269 function PhpError($errno, $errstr, $errfile, $errline) {
270 $this->errno = $errno;
271 $this->errstr = $errstr;
272 $this->errfile = $errfile;
273 $this->errline = $errline;
277 * Determine whether this is a fatal error.
278 * @return boolean True if this is a fatal error.
281 return ($this->errno & (EM_WARNING_ERRORS|EM_NOTICE_ERRORS)) == 0;
285 * Determine whether this is a warning level error.
288 function isWarning() {
289 return ($this->errno & EM_WARNING_ERRORS) != 0;
293 * Determine whether this is a notice level error.
296 function isNotice() {
297 return ($this->errno & EM_NOTICE_ERRORS) != 0;
301 * Get a printable, HTML, message detailing this error.
302 * @return string The detailed error message.
304 function getDetail() {
305 if ($this->isNotice())
307 else if ($this->isWarning())
312 $errfile = ereg_replace('^' . getcwd() . '/', '', $this->errfile);
314 $lines = explode("\n", $this->errstr);
315 //$errstr = htmlspecialchars(array_shift($lines));
316 // Quick hack: by removing htmlspecialchars urls can be used
317 // in error messages. How will this affect other error
319 $errstr = array_shift($lines);
320 foreach ($lines as $key => $line)
321 $lines[$key] = "<li>" . htmlspecialchars($line) . "</li>";
323 $errstr .= "<ul>\n" . join("\n", $lines) . "\n</ul>";
325 return sprintf("<p class='error'>%s:%d: %s[%d]: %s</p>\n",
326 htmlspecialchars($errfile),
327 $this->errline, $what, $this->errno,
332 * Print an HTMLified version of this error.
335 function printError() {
336 echo $this->getDetail();
340 if (!isset($GLOBALS['ErrorManager'])) {
341 $GLOBALS['ErrorManager'] = new ErrorManager;
344 // (c-file-style: "gnu")
349 // c-hanging-comment-ender-p: nil
350 // indent-tabs-mode: nil