1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Paysera\Bundle\ApiBundle\Service; |
5
|
|
|
|
6
|
|
|
use Paysera\Component\ObjectWrapper\Exception\InvalidItemException; |
7
|
|
|
use Paysera\Component\Normalization\Exception\InvalidDataException; |
8
|
|
|
use Paysera\Pagination\Exception\InvalidCursorException; |
9
|
|
|
use Paysera\Pagination\Exception\TooLargeOffsetException; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
12
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
13
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException; |
14
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
15
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; |
16
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
17
|
|
|
use Paysera\Bundle\ApiBundle\Entity\Error; |
18
|
|
|
use Paysera\Bundle\ApiBundle\Exception\ApiException; |
19
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
20
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
21
|
|
|
use Exception; |
22
|
|
|
|
23
|
|
|
class ErrorBuilder implements ErrorBuilderInterface |
24
|
|
|
{ |
25
|
|
|
private $errorConfiguration; |
26
|
|
|
|
27
|
96 |
|
public function __construct() |
28
|
|
|
{ |
29
|
96 |
|
$this->errorConfiguration = []; |
30
|
96 |
|
} |
31
|
|
|
|
32
|
96 |
|
public function configureError(string $errorCode, int $statusCode, string $message) |
33
|
|
|
{ |
34
|
96 |
|
$this->errorConfiguration[$errorCode] = [ |
35
|
96 |
|
'statusCode' => $statusCode, |
36
|
96 |
|
'message' => $message, |
37
|
|
|
]; |
38
|
96 |
|
} |
39
|
|
|
|
40
|
36 |
|
public function createErrorFromException(Exception $exception): Error |
41
|
|
|
{ |
42
|
36 |
|
return $this->fillErrorFields($this->buildErrorFromException($exception)); |
43
|
|
|
} |
44
|
|
|
|
45
|
36 |
|
private function buildErrorFromException(Exception $exception): Error |
46
|
|
|
{ |
47
|
36 |
|
if ($exception instanceof ApiException) { |
48
|
30 |
|
return (new Error()) |
49
|
30 |
|
->setCode($exception->getErrorCode()) |
50
|
30 |
|
->setMessage($exception->getMessage()) |
51
|
30 |
|
->setStatusCode($exception->getStatusCode()) |
52
|
30 |
|
->setProperties($exception->getProperties()) |
53
|
30 |
|
->setData($exception->getData()) |
54
|
30 |
|
->setViolations($exception->getViolations()) |
55
|
|
|
; |
56
|
6 |
|
} elseif ($exception instanceof InvalidDataException) { |
57
|
|
|
return (new Error()) |
58
|
|
|
->setCode(ApiException::INVALID_PARAMETERS) |
59
|
|
|
->setMessage($exception->getMessage()) |
60
|
|
|
; |
61
|
6 |
|
} elseif ($exception instanceof InvalidItemException) { |
62
|
4 |
|
return (new Error()) |
63
|
4 |
|
->setCode(ApiException::INVALID_PARAMETERS) |
64
|
4 |
|
->setMessage($exception->getMessage()) |
65
|
|
|
; |
66
|
2 |
|
} elseif ($exception instanceof TooLargeOffsetException) { |
67
|
1 |
|
return (new Error()) |
68
|
1 |
|
->setCode(ApiException::OFFSET_TOO_LARGE) |
69
|
1 |
|
->setMessage($exception->getMessage()) |
70
|
|
|
; |
71
|
1 |
|
} elseif ($exception instanceof InvalidCursorException) { |
72
|
1 |
|
return (new Error()) |
73
|
1 |
|
->setCode(ApiException::INVALID_CURSOR) |
74
|
1 |
|
->setMessage($exception->getMessage() ?: 'Provided cursor is invalid') |
75
|
|
|
; |
76
|
|
|
} elseif ($exception instanceof AuthenticationCredentialsNotFoundException) { |
77
|
|
|
return (new Error()) |
78
|
|
|
->setCode(ApiException::UNAUTHORIZED) |
79
|
|
|
->setMessage('No authorization data found') |
80
|
|
|
; |
81
|
|
|
} elseif ($exception instanceof AuthenticationException) { |
82
|
|
|
$error = (new Error())->setCode(ApiException::UNAUTHORIZED); |
83
|
|
|
if ($exception->getCode() === 999) { |
84
|
|
|
$error->setMessage($exception->getMessage()); |
85
|
|
|
} |
86
|
|
|
return $error; |
87
|
|
|
} elseif ($exception instanceof AccessDeniedException) { |
88
|
|
|
return (new Error()) |
89
|
|
|
->setCode(ApiException::FORBIDDEN) |
90
|
|
|
->setMessage($exception->getMessage()) |
91
|
|
|
; |
92
|
|
|
} elseif ($exception instanceof AccessDeniedHttpException) { |
93
|
|
|
return (new Error()) |
94
|
|
|
->setCode(ApiException::FORBIDDEN) |
95
|
|
|
->setMessage($exception->getMessage()) |
96
|
|
|
; |
97
|
|
|
} elseif ($exception instanceof ResourceNotFoundException || $exception instanceof NotFoundHttpException) { |
98
|
|
|
return (new Error()) |
99
|
|
|
->setCode(ApiException::NOT_FOUND) |
100
|
|
|
->setMessage('Provided url not found') |
101
|
|
|
; |
102
|
|
|
} elseif ($exception instanceof MethodNotAllowedException) { |
103
|
|
|
return (new Error()) |
104
|
|
|
->setCode(ApiException::NOT_FOUND) |
105
|
|
|
->setMessage('Provided method not allowed for this url') |
106
|
|
|
->setStatusCode(Response::HTTP_METHOD_NOT_ALLOWED) |
107
|
|
|
; |
108
|
|
|
} elseif ($exception instanceof HttpExceptionInterface && $exception->getStatusCode() < 500) { |
109
|
|
|
if ($exception->getStatusCode() === Response::HTTP_NOT_FOUND) { |
110
|
|
|
return (new Error()) |
111
|
|
|
->setCode(ApiException::NOT_FOUND) |
112
|
|
|
->setStatusCode($exception->getStatusCode()) |
113
|
|
|
; |
114
|
|
|
} elseif ($exception->getStatusCode() === Response::HTTP_METHOD_NOT_ALLOWED) { |
115
|
|
|
return (new Error()) |
116
|
|
|
->setCode(ApiException::NOT_FOUND) |
117
|
|
|
->setStatusCode($exception->getStatusCode()) |
118
|
|
|
->setMessage('Provided method not allowed for this url') |
119
|
|
|
; |
120
|
|
|
} elseif ($exception->getStatusCode() === 401) { |
121
|
|
|
return (new Error())->setCode(ApiException::UNAUTHORIZED); |
122
|
|
|
} elseif ($exception->getStatusCode() === 403) { |
123
|
|
|
return (new Error())->setCode(ApiException::FORBIDDEN); |
124
|
|
|
} elseif ($exception->getStatusCode() === 400) { |
125
|
|
|
return (new Error())->setCode(ApiException::INVALID_REQUEST); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return (new Error())->setCode(ApiException::INTERNAL_SERVER_ERROR)->setStatusCode(500); |
130
|
|
|
} |
131
|
|
|
|
132
|
36 |
|
private function fillErrorFields(Error $error): Error |
133
|
|
|
{ |
134
|
36 |
|
$configuration = $this->errorConfiguration[$error->getCode()] ?? []; |
135
|
|
|
|
136
|
36 |
|
if ($error->getStatusCode() === null) { |
137
|
36 |
|
$error->setStatusCode($configuration['statusCode'] ?? Response::HTTP_BAD_REQUEST); |
138
|
|
|
} |
139
|
|
|
|
140
|
36 |
|
if ($error->getMessage() === null || $error->getMessage() === '') { |
141
|
12 |
|
$error->setMessage($configuration['message'] ?? null); |
142
|
|
|
} |
143
|
|
|
|
144
|
36 |
|
return $error; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|