1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Middleware; |
4
|
|
|
|
5
|
|
|
use BrainExe\Core\Annotations\Middleware; |
6
|
|
|
use BrainExe\Core\Application\UserException; |
7
|
|
|
use BrainExe\Core\Traits\LoggerTrait; |
8
|
|
|
use BrainExe\Core\Translation\TranslationTrait; |
9
|
|
|
use Throwable; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException; |
13
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @Middleware("Middleware.CatchUserException") |
17
|
|
|
*/ |
18
|
|
|
class CatchUserException extends AbstractMiddleware |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
const ERROR_NOT_AUTHORIZED = 'NotAuthorized'; |
22
|
|
|
use LoggerTrait; |
23
|
|
|
use TranslationTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
13 |
|
public function processException(Request $request, Throwable $exception) |
29
|
|
|
{ |
30
|
13 |
|
if ($exception instanceof ResourceNotFoundException) { |
31
|
3 |
|
$exception = new UserException( |
32
|
3 |
|
$this->translate( |
33
|
3 |
|
'Page not found: %s', |
34
|
3 |
|
htmlspecialchars($request->getRequestUri()) |
35
|
|
|
), |
36
|
3 |
|
0, |
37
|
|
|
$exception |
38
|
|
|
); |
39
|
3 |
|
$response = new Response('', 404); |
40
|
|
|
} elseif ($exception instanceof MethodNotAllowedException) { |
41
|
2 |
|
$exception = new UserException( |
42
|
2 |
|
$this->translate( |
43
|
2 |
|
'You are not allowed to access the page. Allowed methods: %s', |
44
|
2 |
|
implode(',', $exception->getAllowedMethods()) |
45
|
|
|
), |
46
|
2 |
|
0, |
47
|
|
|
$exception |
48
|
|
|
); |
49
|
2 |
|
$response = new Response('', 405); |
50
|
2 |
|
$response->headers->set('X-Error', self::ERROR_NOT_AUTHORIZED); |
51
|
|
|
} elseif ($exception instanceof UserException) { |
52
|
|
|
// just pass a UserException to Frontend |
53
|
2 |
|
$response = new Response('', 200); |
54
|
|
|
} else { |
55
|
6 |
|
$exception = new UserException($exception->getMessage(), 0, $exception); |
56
|
6 |
|
$response = new Response('', 500); |
57
|
|
|
} |
58
|
|
|
|
59
|
13 |
|
$this->error($exception->getMessage()); |
60
|
13 |
|
$this->error($exception->getTraceAsString()); |
61
|
|
|
|
62
|
13 |
|
$this->setMessage($exception, $request, $response); |
63
|
|
|
|
64
|
13 |
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Throwable $exception |
69
|
|
|
* @param Request $request |
70
|
|
|
* @param Response $response |
71
|
|
|
*/ |
72
|
13 |
|
private function setMessage(Throwable $exception, Request $request, Response $response) |
73
|
|
|
{ |
74
|
13 |
|
$message = $exception->getMessage() ?: $this->translate('An error occurred'); |
75
|
|
|
|
76
|
13 |
|
if ($request->isXmlHttpRequest()) { |
77
|
6 |
|
$response->headers->set('X-Flash-Type', 'danger'); |
78
|
6 |
|
$response->headers->set('X-Flash-Message', $message); |
79
|
|
|
} else { |
80
|
7 |
|
$response->setContent($message); |
81
|
|
|
} |
82
|
13 |
|
} |
83
|
|
|
} |
84
|
|
|
|