|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Controllers\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
6
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Trait ErrorHandling |
|
10
|
|
|
* @package Nip\Controllers\Traits |
|
11
|
|
|
*/ |
|
12
|
|
|
trait ErrorHandling |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
protected function dispatchAccessDeniedResponse() |
|
16
|
|
|
{ |
|
17
|
|
|
throw $this->createAccessDeniedException(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
protected function dispatchNotFoundResponse() |
|
21
|
|
|
{ |
|
22
|
|
|
throw $this->createNotFoundException(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Returns a NotFoundHttpException. |
|
27
|
|
|
* |
|
28
|
|
|
* This will result in a 404 response code. Usage example: |
|
29
|
|
|
* |
|
30
|
|
|
* throw $this->createNotFoundException('Page not found!'); |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function createNotFoundException( |
|
33
|
|
|
string $message = 'Not Found', |
|
34
|
|
|
\Throwable $previous = null |
|
35
|
|
|
): NotFoundHttpException { |
|
36
|
|
|
return new NotFoundHttpException($message, $previous); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Returns an AccessDeniedException. |
|
41
|
|
|
* |
|
42
|
|
|
* This will result in a 403 response code. Usage example: |
|
43
|
|
|
* |
|
44
|
|
|
* throw $this->createAccessDeniedException('Unable to access this page!'); |
|
45
|
|
|
* |
|
46
|
|
|
* @throws \LogicException If the Security component is not available |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function createAccessDeniedException( |
|
49
|
|
|
string $message = 'Access Denied.', |
|
50
|
|
|
\Throwable $previous = null |
|
51
|
|
|
): AccessDeniedException { |
|
52
|
|
|
if (!class_exists(AccessDeniedException::class)) { |
|
53
|
|
|
throw new \LogicException('You can not use the "createAccessDeniedException" method if the Security component is not available. Try running "composer require symfony/security-bundle".'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return new AccessDeniedException($message, $previous); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|