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 Symfony\Bundle\FrameworkBundle\Controller\Controller as FrameworkController; |
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
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(Request $request, 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
|
|
|
$response = new Response('', $statusCode); |
46
|
|
|
|
47
|
|
|
$timestamp = (new DateTime)->format(DateTime::ISO8601); |
48
|
|
|
$hostname = $request->getHost(); |
49
|
|
|
$requestId = $request->headers->get(RequestIdRequestResponseListener::HEADER_NAME, '-'); |
50
|
|
|
$errorCode = Art::forException($exception); |
51
|
|
|
$userAgent = $request->headers->get('User-Agent'); |
52
|
|
|
$ipAddress = $request->getClientIp(); |
53
|
|
|
|
54
|
|
|
return $this->render( |
55
|
|
|
$template, |
56
|
|
|
[ |
57
|
|
|
'timestamp' => $timestamp, |
58
|
|
|
'hostname' => $hostname, |
59
|
|
|
'request_id' => $requestId, |
60
|
|
|
'error_code' => $errorCode, |
61
|
|
|
'user_agent' => $userAgent, |
62
|
|
|
'ip_address' => $ipAddress, |
63
|
|
|
] + $this->getPageTitleAndDescription($exception), |
64
|
|
|
$response |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Exception $exception |
70
|
|
|
* @return int HTTP status code |
71
|
|
|
*/ |
72
|
|
|
protected function getStatusCode(Exception $exception) |
73
|
|
|
{ |
74
|
|
|
if ($exception instanceof AuthenticationException) { |
75
|
|
|
return Response::HTTP_UNAUTHORIZED; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($exception instanceof AccessDeniedException) { |
79
|
|
|
return Response::HTTP_FORBIDDEN; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($exception instanceof HttpExceptionInterface) { |
83
|
|
|
return $exception->getStatusCode(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Unknown exceptions are server errors! |
87
|
|
|
return 500; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Exception $exception |
92
|
|
|
* @return array View parameters 'title' and 'description' |
93
|
|
|
*/ |
94
|
|
|
protected function getPageTitleAndDescription(Exception $exception) |
95
|
|
|
{ |
96
|
|
|
$translator = $this->getTranslator(); |
97
|
|
|
$parameters = []; |
98
|
|
|
|
99
|
|
|
if ($exception instanceof AuthenticationException) { |
100
|
|
|
$parameters['title'] = $translator->trans('stepup.error.authentication_error.title'); |
101
|
|
|
$parameters['description'] = $translator->trans('stepup.error.authentication_error.description'); |
102
|
|
|
} else { |
103
|
|
|
$parameters['title'] = $translator->trans('stepup.error.generic_error.title'); |
104
|
|
|
$parameters['description'] = $translator->trans('stepup.error.generic_error.description'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $parameters; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return TranslatorInterface |
112
|
|
|
*/ |
113
|
|
|
protected function getTranslator() |
114
|
|
|
{ |
115
|
|
|
return $this->get('translator'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|