Skip to content

Commit b615bca

Browse files
authored
Move the custom Error- and Exception handlers to their own classes (#1858)
* Move the custom Error- and Exception handlers to their own classes
1 parent b9809f6 commit b615bca

File tree

3 files changed

+128
-78
lines changed

3 files changed

+128
-78
lines changed

public/_include.php

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -9,87 +9,13 @@
99
use SimpleSAML\Compat\SspContainer;
1010
use SimpleSAML\Configuration;
1111
use SimpleSAML\Error;
12-
use SimpleSAML\Logger;
13-
use SimpleSAML\Module;
1412
use SimpleSAML\Utils;
1513

16-
/**
17-
* show error page on unhandled exceptions
18-
* @param \Throwable $exception
19-
* @return void
20-
*/
21-
function SimpleSAML_exception_handler(Throwable $exception): void
22-
{
23-
Module::callHooks('exception_handler', $exception);
14+
$exceptionHandler = new Error\ExceptionHandler();
15+
set_exception_handler([$exceptionHandler, 'customExceptionHandler']);
2416

25-
if ($exception instanceof Error\Error) {
26-
$exception->show();
27-
} elseif ($exception instanceof Exception) {
28-
$e = new Error\Error('UNHANDLEDEXCEPTION', $exception);
29-
$e->show();
30-
} elseif (class_exists('Error') && $exception instanceof \Error) {
31-
$e = new Error\Error('UNHANDLEDEXCEPTION', $exception);
32-
$e->show();
33-
}
34-
}
35-
36-
set_exception_handler('SimpleSAML_exception_handler');
37-
38-
// log full backtrace on errors and warnings
39-
/**
40-
* @param int $errno
41-
* @param string $errstr
42-
* @param string|null $errfile
43-
* @param int $errline
44-
* @param string|null $errcontext
45-
* @return false
46-
*/
47-
function SimpleSAML_error_handler(
48-
int $errno,
49-
string $errstr,
50-
?string $errfile = null,
51-
int $errline = 0,
52-
): bool {
53-
if (Logger::isErrorMasked($errno)) {
54-
// masked error
55-
return false;
56-
}
57-
58-
static $limit = 5;
59-
$limit -= 1;
60-
if ($limit < 0) {
61-
// we have reached the limit in the number of backtraces we will log
62-
return false;
63-
}
64-
65-
$levels = [
66-
\E_DEPRECATED => 'Deprecated',
67-
\E_USER_DEPRECATED => 'User Deprecated',
68-
\E_NOTICE => 'Notice',
69-
\E_USER_NOTICE => 'User Notice',
70-
\E_STRICT => 'Runtime Notice',
71-
\E_WARNING => 'Warning',
72-
\E_USER_WARNING => 'User Warning',
73-
\E_COMPILE_WARNING => 'Compile Warning',
74-
\E_CORE_WARNING => 'Core Warning',
75-
\E_USER_ERROR => 'User Error',
76-
\E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
77-
\E_COMPILE_ERROR => 'Compile Error',
78-
\E_PARSE => 'Parse Error',
79-
\E_ERROR => 'Error',
80-
\E_CORE_ERROR => 'Core Error',
81-
];
82-
83-
// show an error with a full backtrace
84-
$context = (is_null($errfile) ? '' : " at $errfile:$errline");
85-
$e = new Error\Exception($levels[$errno] . ' - ' . $errstr . $context);
86-
$e->logError();
87-
88-
// resume normal error processing
89-
return false;
90-
}
91-
92-
set_error_handler('SimpleSAML_error_handler');
17+
$errorHandler = new Error\ErrorHandler();
18+
set_error_handler([$errorHandler, 'customErrorHandler']);
9319

9420
try {
9521
Configuration::getInstance();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Error;
6+
7+
use E_DEPRECATED;
8+
use E_USER_DEPRECATED;
9+
use E_NOTICE;
10+
use E_USER_NOTICE;
11+
use E_STRICT;
12+
use E_WARNING;
13+
use E_USER_WARNING;
14+
use E_COMPILE_WARNING;
15+
use E_CORE_WARNING;
16+
use E_USER_ERROR;
17+
use E_RECOVERABLE_ERROR;
18+
use E_COMPILE_ERROR;
19+
use E_PARSE;
20+
use E_ERROR;
21+
use E_CORE_ERROR;
22+
use SimpleSAML\Logger;
23+
24+
use function is_null;
25+
26+
/**
27+
* SimpleSAMLphp's custom error handler that logs full backtraces on errors and warnings
28+
*
29+
* @package SimpleSAMLphp
30+
*/
31+
class ErrorHandler
32+
{
33+
/**
34+
* @param int $errno
35+
* @param string $errstr
36+
* @param string|null $errfile
37+
* @param int $errline
38+
* @param string|null $errcontext
39+
* @return false
40+
*/
41+
public function customErrorHandler(
42+
int $errno,
43+
string $errstr,
44+
?string $errfile = null,
45+
int $errline = 0,
46+
): bool {
47+
if (Logger::isErrorMasked($errno)) {
48+
// masked error
49+
return false;
50+
}
51+
52+
static $limit = 5;
53+
$limit -= 1;
54+
if ($limit < 0) {
55+
// we have reached the limit in the number of backtraces we will log
56+
return false;
57+
}
58+
59+
$levels = [
60+
E_DEPRECATED => 'Deprecated',
61+
E_USER_DEPRECATED => 'User Deprecated',
62+
E_NOTICE => 'Notice',
63+
E_USER_NOTICE => 'User Notice',
64+
E_STRICT => 'Runtime Notice',
65+
E_WARNING => 'Warning',
66+
E_USER_WARNING => 'User Warning',
67+
E_COMPILE_WARNING => 'Compile Warning',
68+
E_CORE_WARNING => 'Core Warning',
69+
E_USER_ERROR => 'User Error',
70+
E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
71+
E_COMPILE_ERROR => 'Compile Error',
72+
E_PARSE => 'Parse Error',
73+
E_ERROR => 'Error',
74+
E_CORE_ERROR => 'Core Error',
75+
];
76+
77+
// show an error with a full backtrace
78+
$context = (is_null($errfile) ? '' : " at $errfile:$errline");
79+
$e = new Exception($levels[$errno] . ' - ' . $errstr . $context);
80+
$e->logError();
81+
82+
// resume normal error processing
83+
return false;
84+
}
85+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Error;
6+
7+
use Error as BuiltinError;
8+
use Exception as BuiltinException;
9+
use SimpleSAML\Module;
10+
use Throwable;
11+
12+
use function class_exists;
13+
14+
/**
15+
* SimpleSAMLphp's custom exception handler.
16+
*
17+
* @package SimpleSAMLphp
18+
*/
19+
class ExceptionHandler
20+
{
21+
/**
22+
* @param \Throwable $exception
23+
* @return void
24+
*/
25+
public function customExceptionHandler(Throwable $exception): void
26+
{
27+
Module::callHooks('exception_handler', $exception);
28+
29+
if ($exception instanceof Error) {
30+
$exception->show();
31+
} elseif ($exception instanceof BuiltinException) {
32+
$e = new Error('UNHANDLEDEXCEPTION', $exception);
33+
$e->show();
34+
} elseif (class_exists('Error') && $exception instanceof BuiltinError) {
35+
$e = new Error('UNHANDLEDEXCEPTION', $exception);
36+
$e->show();
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)