|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2018 SURFnet bv |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller; |
|
22
|
|
|
|
|
23
|
|
|
use DateTime; |
|
24
|
|
|
use DateTimeInterface; |
|
25
|
|
|
use Surfnet\StepupBundle\Exception\Art; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
27
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
28
|
|
|
use Throwable; |
|
29
|
|
|
use Surfnet\StepupBundle\Controller\ExceptionController as BaseExceptionController; |
|
30
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\MissingRequiredAttributeException; |
|
31
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
32
|
|
|
|
|
33
|
|
|
final class ExceptionController extends BaseExceptionController |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
|
|
|
|
|
36
|
|
|
* @return array View parameters 'title' and 'description' |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function getPageTitleAndDescription(Throwable $exception): array |
|
39
|
|
|
{ |
|
40
|
|
|
$translator = $this->getTranslator(); |
|
41
|
|
|
|
|
42
|
|
|
if ($exception instanceof HttpException && $exception instanceof MissingRequiredAttributeException) { |
|
|
|
|
|
|
43
|
|
|
$title = $translator->trans('stepup.error.missing_required_attribute.title'); |
|
44
|
|
|
$description = $exception->getMessage(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (isset($title) && isset($description)) { |
|
48
|
|
|
return [ |
|
49
|
|
|
'title' => $title, |
|
50
|
|
|
'description' => $description, |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return parent::getPageTitleAndDescription($exception); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function show(Request $request, Throwable $exception): Response |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$statusCode = $this->getStatusCode($exception); |
|
60
|
|
|
|
|
61
|
|
|
$template = '@default/bundles/TwigBundle/Exception/error.html.twig'; |
|
62
|
|
|
if ($statusCode == 404) { |
|
63
|
|
|
$template = '@default/bundles/TwigBundle/Exception/error404.html.twig'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$response = new Response('', $statusCode); |
|
67
|
|
|
|
|
68
|
|
|
$timestamp = (new DateTime)->format(DateTimeInterface::ATOM); |
|
69
|
|
|
$hostname = $request->getHost(); |
|
70
|
|
|
$requestId = $this->requestId; |
|
71
|
|
|
$errorCode = Art::forException($exception); |
|
72
|
|
|
$userAgent = $request->headers->get('User-Agent'); |
|
73
|
|
|
$ipAddress = $request->getClientIp(); |
|
74
|
|
|
|
|
75
|
|
|
return $this->render( |
|
76
|
|
|
$template, |
|
77
|
|
|
[ |
|
78
|
|
|
'timestamp' => $timestamp, |
|
79
|
|
|
'hostname' => $hostname, |
|
80
|
|
|
'request_id' => $requestId->get(), |
|
81
|
|
|
'error_code' => $errorCode, |
|
82
|
|
|
'user_agent' => $userAgent, |
|
83
|
|
|
'ip_address' => $ipAddress, |
|
84
|
|
|
] + $this->getPageTitleAndDescription($exception), |
|
85
|
|
|
$response |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|