|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace nystudio107\crafttwigsandbox\web; |
|
4
|
|
|
|
|
5
|
|
|
use craft\helpers\Template; |
|
6
|
|
|
use craft\web\ErrorHandler; |
|
7
|
|
|
use ReflectionMethod; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
use Twig\Sandbox\SecurityError; |
|
10
|
|
|
|
|
11
|
|
|
class SandboxErrorHandler extends ErrorHandler |
|
12
|
|
|
{ |
|
13
|
|
|
// Public Methods |
|
14
|
|
|
// ========================================================================= |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @inheritDoc |
|
18
|
|
|
*/ |
|
19
|
|
|
public function handleException($exception): void |
|
20
|
|
|
{ |
|
21
|
|
|
// If this is a Twig Runtime exception, use the previous one instead |
|
22
|
|
|
if ($exception instanceof SecurityError && ($previousException = $exception->getPrevious()) !== null) { |
|
23
|
|
|
$exception = $previousException; |
|
24
|
|
|
} |
|
25
|
|
|
parent::handleException($exception); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritDoc |
|
30
|
|
|
*/ |
|
31
|
|
|
public function renderCallStackItem($file, $line, $class, $method, $args, $index): string |
|
32
|
|
|
{ |
|
33
|
|
|
try { |
|
34
|
|
|
$templateInfo = Template::resolveTemplatePathAndLine($file ?? '', $line); |
|
35
|
|
|
if ($templateInfo !== false) { |
|
36
|
|
|
[$file, $line] = $templateInfo; |
|
37
|
|
|
} |
|
38
|
|
|
} catch (SecurityError $e) { |
|
39
|
|
|
$line = $e->getTemplateLine(); |
|
40
|
|
|
$file = $e->getSourceContext()->getPath() ?: null; |
|
41
|
|
|
} catch (Throwable $e) { |
|
42
|
|
|
// That's fine |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// Call the grandparent ErrorHandler::renderCallStackItem() so Craft's ErrorHandler::renderCallStackItem() |
|
46
|
|
|
// doesn't throw an additional exception when trying to render the callstack |
|
47
|
|
|
$reflectionMethod = new ReflectionMethod(get_parent_class(get_parent_class($this)), 'renderCallStackItem'); |
|
48
|
|
|
|
|
49
|
|
|
return $reflectionMethod->invokeArgs($this, [$file, $line, $class, $method, $args, $index]); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|