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\UserMessageException; |
26
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller as FrameworkController; |
27
|
|
|
use Symfony\Component\HttpFoundation\Response; |
28
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
29
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
30
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
31
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
32
|
|
|
|
33
|
|
|
class ExceptionController extends FrameworkController |
34
|
|
|
{ |
35
|
|
|
public function showAction(Exception $exception) |
36
|
|
|
{ |
37
|
|
|
$statusCode = $this->getStatusCode($exception); |
38
|
|
|
|
39
|
|
|
if ($statusCode == 404) { |
40
|
|
|
$template = 'SurfnetStepupBundle:Exception:error404.html.twig'; |
41
|
|
|
} else { |
42
|
|
|
$template = 'SurfnetStepupBundle:Exception:error.html.twig'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$request = $this->getRequest(); |
|
|
|
|
46
|
|
|
$response = new Response('', $statusCode); |
47
|
|
|
|
48
|
|
|
$timestamp = (new DateTime)->format(DateTime::ISO8601); |
49
|
|
|
$hostname = $request->getHost(); |
50
|
|
|
$requestId = $request->headers->get(RequestIdRequestResponseListener::HEADER_NAME, '-'); |
51
|
|
|
$errorCode = Art::forException($exception); |
52
|
|
|
$userAgent = $request->headers->get('User-Agent'); |
53
|
|
|
$ipAddress = $request->getClientIp(); |
54
|
|
|
|
55
|
|
|
return $this->render( |
56
|
|
|
$template, |
57
|
|
|
[ |
58
|
|
|
'timestamp' => $timestamp, |
59
|
|
|
'hostname' => $hostname, |
60
|
|
|
'request_id' => $requestId, |
61
|
|
|
'error_code' => $errorCode, |
62
|
|
|
'user_agent' => $userAgent, |
63
|
|
|
'ip_address' => $ipAddress, |
64
|
|
|
] + $this->getPageTitleAndDescription($exception), |
65
|
|
|
$response |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Exception $exception |
71
|
|
|
* @return int HTTP status code |
72
|
|
|
*/ |
73
|
|
|
private function getStatusCode(Exception $exception) |
74
|
|
|
{ |
75
|
|
|
if ($exception instanceof AuthenticationException) { |
76
|
|
|
return Response::HTTP_UNAUTHORIZED; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($exception instanceof AccessDeniedException) { |
80
|
|
|
return Response::HTTP_FORBIDDEN; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($exception instanceof HttpExceptionInterface) { |
84
|
|
|
return $exception->getStatusCode(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Unknown exceptions are server errors! |
88
|
|
|
return 500; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Exception $exception |
93
|
|
|
* @return array View parameters 'title' and 'description' |
94
|
|
|
*/ |
95
|
|
|
private function getPageTitleAndDescription(Exception $exception) |
96
|
|
|
{ |
97
|
|
|
$translator = $this->getTranslator(); |
98
|
|
|
$parameters = []; |
99
|
|
|
|
100
|
|
|
if ($exception instanceof UserErrorProviderInterface) { |
|
|
|
|
101
|
|
|
$parameters['title'] = $translator->trans( |
102
|
|
|
$exception->getErrorPageTitleMessageId() |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$parameters['description'] = $translator->trans( |
106
|
|
|
$exception->getErrorPageDescriptionMessageId(), |
107
|
|
|
$exception->getErrorPageDescriptionArguments() |
108
|
|
|
); |
109
|
|
|
} elseif ($exception instanceof AuthenticationException) { |
110
|
|
|
$parameters['title'] = $translator->trans('stepup.error.authentication_error_title'); |
111
|
|
|
$parameters['description'] = $translator->trans('stepup.error.authentication_error_description'); |
112
|
|
|
} else { |
113
|
|
|
$parameters['title'] = $translator->trans('stepup.error.generic_error_title'); |
114
|
|
|
$parameters['description'] = $translator->trans('stepup.error.generic_error_description'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $parameters; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return TranslatorInterface |
122
|
|
|
*/ |
123
|
|
|
private function getTranslator() |
124
|
|
|
{ |
125
|
|
|
return $this->get('translator'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.