|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Enum\Environment; |
|
6
|
|
|
use AppBundle\Service\HashGenerator; |
|
7
|
|
|
use Monolog\Logger; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Vehsamrak |
|
15
|
|
|
*/ |
|
16
|
|
|
class ExceptionListener |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
private $environment; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $adminEmail; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Logger */ |
|
26
|
|
|
private $logger; |
|
27
|
|
|
|
|
28
|
28 |
|
public function __construct(string $environment, string $adminEmail, Logger $logger) |
|
29
|
|
|
{ |
|
30
|
28 |
|
$this->environment = $environment; |
|
31
|
28 |
|
$this->adminEmail = $adminEmail; |
|
32
|
28 |
|
$this->logger = $logger; |
|
33
|
28 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function onKernelException(GetResponseForExceptionEvent $event) |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var \Exception $exception */ |
|
38
|
|
|
$exception = $event->getException(); |
|
39
|
|
|
|
|
40
|
|
|
if ($exception instanceof HttpExceptionInterface && $exception->getStatusCode() < 500) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($this->environment === Environment::PRODUCTION) { |
|
45
|
|
|
$bugSerialNumber = HashGenerator::generate(); |
|
46
|
|
|
|
|
47
|
|
|
$this->logger->info( |
|
48
|
|
|
sprintf( |
|
49
|
|
|
'#%s - %s in %s at line %s', |
|
50
|
|
|
$bugSerialNumber, |
|
51
|
|
|
$exception->getMessage(), |
|
52
|
|
|
$exception->getFile(), |
|
53
|
|
|
$exception->getLine() |
|
54
|
|
|
) |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$errorMessage = sprintf( |
|
58
|
|
|
'Wow! You probably found a bug with serial number #%s. Please report it to %s.', |
|
59
|
|
|
$bugSerialNumber, |
|
60
|
|
|
$this->adminEmail |
|
61
|
|
|
); |
|
62
|
|
|
} else { |
|
63
|
|
|
$errorMessage = sprintf( |
|
64
|
|
|
'%s in %s at line %s', |
|
65
|
|
|
$exception->getMessage(), |
|
66
|
|
|
$exception->getFile(), |
|
67
|
|
|
$exception->getLine() |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$data = [ |
|
72
|
|
|
'errors' => [ |
|
73
|
|
|
$errorMessage, |
|
74
|
|
|
], |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
$response = new JsonResponse(); |
|
78
|
|
|
$response->setContent(json_encode($data)); |
|
79
|
|
|
|
|
80
|
|
|
if ($exception instanceof HttpExceptionInterface) { |
|
81
|
|
|
$response->setStatusCode($exception->getStatusCode()); |
|
82
|
|
|
$response->headers->replace($exception->getHeaders()); |
|
83
|
|
|
} else { |
|
84
|
|
|
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$event->setResponse($response); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|