Adding a global exception handler BSExceptionHandler
authorRobert Sesek <rsesek@bluestatic.org>
Wed, 2 Jul 2008 02:13:25 +0000 (22:13 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Wed, 2 Jul 2008 02:13:25 +0000 (22:13 -0400)
* ExceptionHandler.php: New file
* App.php: set_exception_handler() to use BSExceptionHandler

App.php
CHANGES
ExceptionHandler.php [new file with mode: 0644]

diff --git a/App.php b/App.php
index 7318fcc6de8875870162985b8c1bf12248373e39..5e0fce70184614e821ea09d8948825754353a701 100644 (file)
--- a/App.php
+++ b/App.php
@@ -51,7 +51,8 @@ if ((bool)ini_get('register_globals') === true)
        }
 }
 
-require_once(ISSO . '/Functions.php');
+require_once ISSO . '/ExceptionHandler.php';
+set_exception_handler(array('BSExceptionHandler', 'handle'));
 
 /**
  * Application Class
diff --git a/CHANGES b/CHANGES
index 7493747d18d3d5db7cf0e8990a8d4df71c5ae065..edc044ab2861732c896890cacce89e72e368f94d 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -13,3 +13,4 @@
 - New: Add BSTemplate::$globalVars to be substituted into every template
 - Change: BSTemplate will better detect parse errors in nested templates in BSTemplate::evaluate()
 - Change: ApiException will now report all of its messages in ::getMessage()
+- New: Added BSExceptionHandler to handle all exceptions in a pretty fasion
diff --git a/ExceptionHandler.php b/ExceptionHandler.php
new file mode 100644 (file)
index 0000000..bff2812
--- /dev/null
@@ -0,0 +1,164 @@
+<?php
+/*=====================================================================*\
+|| ###################################################################
+|| # Blue Static ISSO Framework
+|| # Copyright (c)2005-2008 Blue Static
+|| #
+|| # This program is free software; you can redistribute it and/or modify
+|| # it under the terms of the GNU General Public License as published by
+|| # the Free Software Foundation; version 2 of the License.
+|| #
+|| # This program is distributed in the hope that it will be useful, but
+|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+|| # more details.
+|| #
+|| # You should have received a copy of the GNU General Public License along
+|| # with this program; if not, write to the Free Software Foundation, Inc.,
+|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+|| ###################################################################
+\*=====================================================================*/
+
+/**
+ * Exception Handler (ExceptionHandler.php)
+ *
+ * @package    ISSO
+ */
+
+/**
+ * Exception Handler
+ *
+ * This class will handle all exceptions that are uncaught in an application
+ *
+ * @author             rsesek
+ * @copyright  Copyright (c)2005 - 2008, Blue Static
+ * @package            ISSO
+ *
+ */
+class BSExceptionHandler
+{
+       private function __construct()
+       {}
+       
+       /**
+        * This is the handling dispatcher function that will examine, process
+        * and properly display exceptions
+        */
+       public static function handle(Exception $e)
+       {
+               // see if this was an application-level or framework-level exception
+               $trace = $e->getTrace();
+               $framework = false;
+               foreach ($trace as $i => $frame)
+               {
+                       if (!isset($frame['file']))
+                       {
+                               continue;
+                       }
+                       if (strpos($frame['file'], ISSO) !== false || substr($frame['class'], 0, 2) == 'BS')
+                       {
+                               $framework = true;
+                               break;
+                       }
+               }
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
+       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<head>
+       <title>Uncaught Exception</title>
+       
+       <style type="text/css">
+       /*<![CDATA[*/
+               body
+               {
+                       font-family: Helvetica, Arial, Verdana, sans-serif;
+               }
+               
+               #warning
+               {
+                       background-image: url('data:image/png;base64,<?php echo self::get_alert_image() ?>');
+                       background-repeat: no-repeat;
+                       
+                       margin: 75px 0px 0px 150px;
+                       width: 600px;
+               }
+               
+               h1
+               {
+                       margin-left: 60px;
+                       padding-top: 5px;
+                       color: rgb(175, 0, 0);
+               }
+               
+               pre
+               {
+                       overflow: scroll;
+               }
+       /*]]>*/
+       </style>
+</head>
+
+<body>
+
+<div id="warning">
+       <h1>Uncaught Exception</h1>
+       
+       <p>
+               <em>An exception was encountered during the exection of this program. Because the program could not recover
+               from this error it terminated prematurely. Please contact a system administrator if this problem persists.</em>
+       </p>
+       
+       <br/>
+       
+       <p><strong>Time:</strong> <?php echo date('r') ?></p>
+       
+       <p><strong>Context:</strong> <?php echo ($framework ? 'Framework' : 'Application')?></p>
+       
+       <p><strong>Class:</strong> <?php echo get_class($e) . (get_parent_class($e) ? ' &lt; ' . get_parent_class($e) : '') ?></p>
+       
+       <p><strong>Message:</strong> <?php echo $e->getMessage() ?></p>
+       
+       <p>
+               <strong>Stack Trace:</strong><br/>
+               <pre>
+<?php 
+                       $trace = $e->getTraceAsString();
+                       echo (BSApp::get_debug() ? $trace : str_replace(getcwd(), '', $trace));
+?>
+               </pre>
+       </p>
+</div>
+
+</body>
+</html>
+<?php
+       }
+       
+       /**
+        * Returns a base-64 representation of an alert sign
+        *
+        * @return      string
+        */
+       protected static function get_alert_image()
+       {
+               return 'iVBORw0KGgoAAAANSUhEUgAAADcAAAA3CAYAAACo29JGAAAABGdBTUEAANbY1E9YMgAAABl0RVh0U2' .
+               '9mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAOlSURBVHja7Jq9jtNAEMediAcwb2CuQIgGIx6A' .
+               '8ATHFVDQXNwc5SUdXZIOqoQSmoSGhoJQc1JyPehSIXQSIjxBwhOE2dM/0t4y+2F77Tgnj7Ry7Djr/X' .
+               'k+dmY3jc1mE9xUadRwNVz1pFnWgz42Gi1qK2rjG6U5AmrTQYZaUHvyYrNZ77XmoClVWzG1C/ou3kvN' .
+               '0cBDOgyptQ23raHBxd7AAWwGDdlEAHYJcFJ5szSACYiH1FQIcf8YflldOBpgpAETZndHmB+1hD4PmJ' .
+               '97B/RmlggOM2gisEVGJoJuZUD39iujOQPYRBfy4WMJzFWWnq+5MLfmTGAwwUJ/XxgcfOwi78AMgCPq' .
+               'p1s6nCEqZnrjBsAk6zSRCS4t2N179w/ocCBfu/z546xowKwBJa3GnlH7qrSACTJXkZUJMmKaeFo4HC' .
+               'KZCjbN6/wMIDcPxoXBUed9JldcIKR7EwAmTCYzg0v4hYNZ9FwmaAdZOQBO8gI2U0SyMZMrHjmCfbec' .
+               'mwBHTLk09AKHtzRmIpjQ2LLoehDz3FS53HbJQ10012MCSFJUDaaRBC4gy9AWYJoOftZhQv6kRLAApq' .
+               '/moaEm8bbDSeYoizDDbrADgaWoz44RwVNrrsf42VHRizoOAWbKVBGRMxxsucPUWYtg98KVSeM0mhsy' .
+               '5jiqANjW/1TzFGuiLSscblJv7OY0x0zznMU854wbWTV3rJzPqbNpnsFQBbBKm6E4yMCmvSYTIduWTo' .
+               'KKmOec0d6xSXNqWbFEJ1WVgWn8Ktyhcv62wmBb7ckpYCib5i3VbtU6zeNYXvkKKMwYOwrD/Boc/C1U' .
+               'TNJbYkxB5U1BCjxX4B5wmouZuc2bSOsoKwL1qbk1U/PlWkNJC3ZCh19YO/lG5+/KeK4JLvT4nNfK+Q' .
+               'kBPioNjgn5sS4hzSC3Ha9lkUOdOzWZNRFZTj0N4IzJUHL7nSbpONfBqfNax9O20ktq7wH5idpzJiXL' .
+               'AqYu4K7lQvq/FWf60W86REy506/K5C0tWMVMgj8ywV1txjN9LgE52SFUBDPsMV+LBP/aYi67V2DYGN' .
+               'xCiqzgSxl5J4BaCBy6JXV2/VS7EYIc7bPDlDBH53+2ASkLNCAiPC9GphEzLsKlXwlXbxp3eRz/buGS' .
+               'QSw082ie/6Es4WPa/NdpCwtv9RSQ4Y7jibCKDy6+n3p/DmuZj+EHcQkwawCJ+WuaJpn3sSfegl9E8J' .
+               'NQUz7ZTGwpBYe/AMpVmdT/t6zhargazpv8E2AAczzQD3C2jBgAAAAASUVORK5CYII=';
+       }
+}
\ No newline at end of file