Changeset:
        335c4cdbb935
        
https://sourceforge.net/p/mrbs/hg-code/ci/335c4cdbb935a0b05e8ee6ef44601d55ac4d499f
Author:
        Campbell Morrison <[email protected]>
Date:
        Wed Sep 28 17:07:02 2016 +0100
Log message:

Added uncaught exception handler

diffstat:

 web/defaultincludes.inc    |    2 +-
 web/functions_error.inc    |  108 +++++++++++++++++++++++++++++++++++----------
 web/internalconfig.inc.php |    3 +-
 web/lang/lang.en           |    1 +
 4 files changed, 88 insertions(+), 26 deletions(-)

diffs (172 lines):

diff -r a8c96ae5446a -r 335c4cdbb935 web/defaultincludes.inc
--- a/web/defaultincludes.inc   Wed Sep 28 14:09:22 2016 +0100
+++ b/web/defaultincludes.inc   Wed Sep 28 17:07:02 2016 +0100
@@ -21,13 +21,13 @@
 require "grab_globals.inc.php";   // this must be included before 
mrbs_auth.inc (due to WordPress - see comment in file)
 require_once "systemdefaults.inc.php";
 require_once "areadefaults.inc.php";
+require_once "functions_error.inc";
 require_once "config.inc.php";
 require_once "internalconfig.inc.php";
 require_once "language.inc";
 require_once "trailer.inc";
 require_once "theme.inc";
 require_once "functions.inc";
-require_once "functions_error.inc";
 require_once "dbsys.inc";
 require_once("password_compat/password.php");
 require_once "mrbs_auth.inc";
diff -r a8c96ae5446a -r 335c4cdbb935 web/functions_error.inc
--- a/web/functions_error.inc   Wed Sep 28 14:09:22 2016 +0100
+++ b/web/functions_error.inc   Wed Sep 28 17:07:02 2016 +0100
@@ -2,12 +2,68 @@
 namespace MRBS;
 
 
+// Translate an error constant value into the name of the constant
+function get_error_name($errno)
+{
+  $constants = get_defined_constants(true);
+  $keys = array_keys($constants['Core'], $errno);
+  $keys = array_filter($keys, function($value) {
+      return (strpos($value, 'E_') === 0);
+    });
+  return implode('|', $keys); // There should only be one member of the array, 
all being well.
+}
+
+
+// Define our own error handler, to allow us to format the message nicely for 
the screen 
+// and also because it doesn't seem possible to get error_log() to output to 
the browser.
+function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
+{
+  $display_errors = ini_get('display_errors');
+  
+  $title = get_error_name($errno) . " in $errfile at line $errline\n";
+  
+  if ($display_errors)
+  {
+    echo nl2br("<b>$title</b>");
+    echo nl2br($errstr);
+  }
+  else
+  {
+    error_log($title . $errstr);
+  }
+}
+
+
+function exception_handler($exception)
+{
+  $class = get_class($exception);
+  
+  $message = "Uncaught exception ($class) in " . $exception->getFile() . " at 
line " . $exception->getLine() . "\n" .
+             $exception->getMessage() . "\n" .
+             $exception->getTraceAsString();
+             
+  trigger_error($message, E_USER_WARNING);
+  
+  switch ($class)
+  {
+    case 'PDOException':
+      $message = get_vocab("fatal_db_error");
+      break;
+    default:
+      $message = get_vocab("fatal_error");
+      break;
+  }
+  
+  fatal_error(true, $message, false, false);
+}
+
+
 // Error handler - this is used to display serious errors such as database
 // errors without sending incomplete HTML pages. This is only used for
 // errors which "should never happen", not those caused by bad inputs.
 // If $need_header!=0 output the top of the page too, else assume the
 // caller did that. Always outputs the bottom of the page and exits.
-function fatal_error($need_header, $message, $show_form_data = true)
+function fatal_error($need_header, $message, $show_form_data = true, 
$show_debug = true)
 {
   global $simple_trailer, $weekstarts, $view_week_number, $strftime_format;
 
@@ -16,32 +72,36 @@
     print_header(0, 0, 0, 0, "");
   }
   
-  error_log("MRBS: $message");
-  
-  $sql_error = sql_error();
-  if (!empty($sql_error))
+  if ($show_debug)
   {
-    error_log("MRBS: $sql_error");
+    error_log("MRBS: $message");
+    
+    $sql_error = sql_error();
+    if (!empty($sql_error))
+    {
+      error_log("MRBS: $sql_error");
+    }
+    
+    $e = new Exception();
+    error_log($e->getTraceAsString());
+    
+    if ($show_form_data)
+    {
+      if (!empty($_GET))
+      {
+        error_log("MRBS GET: " . print_r($_GET, true));
+      }
+      if (!empty($_POST))
+      {
+        error_log("MRBS POST: " . print_r($_POST, true));
+      }
+    }
+    if (!empty($_SESSION))
+    {
+      error_log("MRBS SESSION: " . print_r($_SESSION, true));
+    }
   }
   
-  $e = new Exception();
-  error_log($e->getTraceAsString());
-  
-  if ($show_form_data)
-  {
-    if (!empty($_GET))
-    {
-      error_log("MRBS GET: " . print_r($_GET, true));
-    }
-    if (!empty($_POST))
-    {
-      error_log("MRBS POST: " . print_r($_POST, true));
-    }
-  }
-  if (!empty($_SESSION))
-  {
-    error_log("MRBS SESSION: " . print_r($_SESSION, true));
-  }
   echo "<p>$message</p>";
   output_trailer();
   exit;
diff -r a8c96ae5446a -r 335c4cdbb935 web/internalconfig.inc.php
--- a/web/internalconfig.inc.php        Wed Sep 28 14:09:22 2016 +0100
+++ b/web/internalconfig.inc.php        Wed Sep 28 17:07:02 2016 +0100
@@ -527,4 +527,5 @@
 }
 
 error_reporting ($error_level);
-
+set_error_handler(__NAMESPACE__ . "\\error_handler");
+set_exception_handler(__NAMESPACE__ . "\\exception_handler");
diff -r a8c96ae5446a -r 335c4cdbb935 web/lang/lang.en
--- a/web/lang/lang.en  Wed Sep 28 14:09:22 2016 +0100
+++ b/web/lang/lang.en  Wed Sep 28 17:07:02 2016 +0100
@@ -518,6 +518,7 @@
 $vocab["type.E"]            = "External";
  
 // General
+$vocab["fatal_error"]        = "Whoops!  Unfortunately MRBS has encountered a 
fatal error.  Please consult your system administrator.";
 $vocab["fatal_db_error"]     = "Fatal error: unfortunately the database is not 
available at the moment.";
 $vocab["fatal_no_tables"]    = "Fatal error: the MRBS tables do not exist or 
cannot be accessed.";
 $vocab["back"]               = "Back";

------------------------------------------------------------------------------
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to