1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\Controller; |
20
|
|
|
|
21
|
|
|
use DateTime; |
22
|
|
|
use Exception; |
23
|
|
|
use Surfnet\StepupBundle\EventListener\RequestIdRequestResponseListener; |
24
|
|
|
use Surfnet\StepupBundle\Exception\Art; |
25
|
|
|
use Surfnet\StepupBundle\Exception\UserErrorProviderInterface; |
26
|
|
|
use Surfnet\StepupBundle\Exception\UserMessageException; |
27
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller as FrameworkController; |
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
30
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
31
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
32
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
33
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
34
|
|
|
|
35
|
|
|
class ExceptionController extends FrameworkController |
36
|
|
|
{ |
37
|
|
|
public function showAction(Request $request, Exception $exception) |
38
|
|
|
{ |
39
|
|
|
$statusCode = $this->getStatusCode($exception); |
40
|
|
|
|
41
|
|
|
if ($statusCode == 404) { |
42
|
|
|
$template = 'SurfnetStepupBundle:Exception:error404.html.twig'; |
43
|
|
|
} else { |
44
|
|
|
$template = 'SurfnetStepupBundle:Exception:error.html.twig'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$response = new Response('', $statusCode); |
48
|
|
|
|
49
|
|
|
$timestamp = (new DateTime)->format(DateTime::ISO8601); |
50
|
|
|
$hostname = $request->getHost(); |
51
|
|
|
$requestId = $request->headers->get(RequestIdRequestResponseListener::HEADER_NAME, '-'); |
52
|
|
|
$errorCode = Art::forException($exception); |
53
|
|
|
$userAgent = $request->headers->get('User-Agent'); |
54
|
|
|
$ipAddress = $request->getClientIp(); |
55
|
|
|
|
56
|
|
|
return $this->render( |
57
|
|
|
$template, |
58
|
|
|
[ |
59
|
|
|
'timestamp' => $timestamp, |
60
|
|
|
'hostname' => $hostname, |
61
|
|
|
'request_id' => $requestId, |
62
|
|
|
'error_code' => $errorCode, |
63
|
|
|
'user_agent' => $userAgent, |
64
|
|
|
'ip_address' => $ipAddress, |
65
|
|
|
] + $this->getPageTitleAndDescription($exception), |
66
|
|
|
$response |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Exception $exception |
72
|
|
|
* @return int HTTP status code |
73
|
|
|
*/ |
74
|
|
|
private function getStatusCode(Exception $exception) |
75
|
|
|
{ |
76
|
|
|
if ($exception instanceof AuthenticationException) { |
77
|
|
|
return Response::HTTP_UNAUTHORIZED; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($exception instanceof AccessDeniedException) { |
81
|
|
|
return Response::HTTP_FORBIDDEN; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($exception instanceof HttpExceptionInterface) { |
85
|
|
|
return $exception->getStatusCode(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Unknown exceptions are server errors! |
89
|
|
|
return 500; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Exception $exception |
94
|
|
|
* @return array View parameters 'title' and 'description' |
95
|
|
|
*/ |
96
|
|
|
private function getPageTitleAndDescription(Exception $exception) |
97
|
|
|
{ |
98
|
|
|
$translator = $this->getTranslator(); |
99
|
|
|
$parameters = []; |
100
|
|
|
|
101
|
|
|
if ($exception instanceof UserErrorProviderInterface) { |
102
|
|
|
$parameters['title'] = $translator->trans( |
103
|
|
|
$exception->getErrorPageTitleMessageId() |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$parameters['description'] = $translator->trans( |
107
|
|
|
$exception->getErrorPageDescriptionMessageId(), |
108
|
|
|
$exception->getErrorPageDescriptionArguments() |
109
|
|
|
); |
110
|
|
|
} elseif ($exception instanceof AuthenticationException) { |
111
|
|
|
$parameters['title'] = $translator->trans('stepup.error.authentication_error_title'); |
112
|
|
|
$parameters['description'] = $translator->trans('stepup.error.authentication_error_description'); |
113
|
|
|
} else { |
114
|
|
|
$parameters['title'] = $translator->trans('stepup.error.generic_error_title'); |
115
|
|
|
$parameters['description'] = $translator->trans('stepup.error.generic_error_description'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $parameters; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return TranslatorInterface |
123
|
|
|
*/ |
124
|
|
|
private function getTranslator() |
125
|
|
|
{ |
126
|
|
|
return $this->get('translator'); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|