-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathExceptionHandler.php
More file actions
104 lines (90 loc) · 3.82 KB
/
ExceptionHandler.php
File metadata and controls
104 lines (90 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace JMS\DebuggingBundle\Kernel;
use JMS\DebuggingBundle\Util\ObjectFinder;
use JMS\DebuggingBundle\DataCollector\RealExceptionDataCollector;
use JMS\DebuggingBundle\Exception\RuntimeException;
use JMS\DebuggingBundle\Listener\ResponseListener;
use JMS\DebuggingBundle\Serializer\ProfilerNormalizer;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\ExceptionDataCollector;
use Symfony\Component\HttpKernel\DataCollector\ConfigDataCollector;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Profiler\Profiler;
/**
* Handles exceptions thrown by the Kernel itself.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ExceptionHandler
{
private $kernel;
private $finder;
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
$this->finder = new ObjectFinder();
}
public function register()
{
set_exception_handler(array($this, 'handle'));
}
public function handle($exception)
{
if ( ! $exception instanceof \Exception) {
throw $exception;
}
try {
try {
$request = $this->finder->find('Symfony\Component\HttpFoundation\Request', $exception);
} catch (RuntimeException $requestRetrievalFailed) {
$request = Request::createFromGlobals();
}
$origException = $exception;
$exception = FlattenException::create($exception);
$codeHelper = new CodeHelper(null, $this->kernel->getRootDir(), 'UTF-8');
$title = $exception->getMessage().' (500 Internal Server Error)';
$template = 'Exception/exception.html.php';
ob_start();
include __DIR__.'/../Resources/views/layout.html.php';
$content = ob_get_contents();
ob_end_clean();
$event = new FilterResponseEvent(new NullHttpKernel(), $request, HttpKernelInterface::MASTER_REQUEST, new Response($content, 500));
$this->filterResponse($event, $origException);
$event->getResponse()->send();
} catch (\Exception $ex) {
echo "Exception while handling exception: ".$ex->getMessage()."<br/>Original Exception: ".$exception->getMessage();
}
}
private function filterResponse(FilterResponseEvent $event, \Exception $ex)
{
$profiler = new Profiler(new NullProfilerStorage());
$profiler->add(new ConfigDataCollector($this->kernel));
$profiler->add(new ExceptionDataCollector());
$profiler->add(new RealExceptionDataCollector());
$profiler->collect($event->getRequest(), $event->getResponse(), $ex);
$normalizer = new ProfilerNormalizer($this->kernel);
$listener = new ResponseListener($normalizer, $profiler);
$listener->onKernelResponse($event);
}
}