From 2a489e0c2a421405970a37e7a3883535135dd3e8 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Mon, 13 Jul 2009 06:15:19 +0000
Subject: [PATCH] _include.php: Improve handling of unhandled errors and
 exceptions.

This patch adds an exception handler which will catch all unhandled
exceptions and display an error page to the user. It also adds a
handler for PHP errors and warnings. This handler will show a stacktrace
of the error, and then pass the error to the normal PHP error handler.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1561 44740490-163a-0410-bde0-09ae8108e29a
---
 www/_include.php | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/www/_include.php b/www/_include.php
index a3d64029e..988d937df 100644
--- a/www/_include.php
+++ b/www/_include.php
@@ -22,6 +22,26 @@ if(get_magic_quotes_gpc()) {
 /* Initialize the autoloader. */
 require_once(dirname(dirname(__FILE__)) . '/lib/_autoload.php');
 
+/* Show error page on unhandled exceptions. */
+function SimpleSAML_exception_handler(Exception $exception) {
+	$e = new SimpleSAML_Error_Error('UNHANDLEDEXCEPTION', $exception);
+	$e->show();
+}
+set_exception_handler('SimpleSAML_exception_handler');
+
+/* Log full backtrace on errors and warnings. */
+function SimpleSAML_error_handler($errno, $errstr, $errfile = NULL, $errline = 0, $errcontext = NULL) {
+
+	/* Show an error with a full backtrace. */
+	$e = new SimpleSAML_Error_Exception('Error ' . $errno . ' - ' . $errstr);
+	$e->logError();
+
+	/* Resume normal error processing. */
+	return FALSE;
+}
+set_error_handler('SimpleSAML_error_handler');
+
+
 $path_extra = dirname(dirname(__FILE__)) . '/lib';
 
 
-- 
GitLab