|
1
|
|
|
<?php namespace Pz\Doctrine\Rest\Exceptions; |
|
2
|
|
|
|
|
3
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
|
4
|
|
|
use Exception; |
|
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
6
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface; |
|
7
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
|
8
|
|
|
|
|
9
|
|
|
class RestException extends Exception |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $errors = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param \Exception $exception |
|
18
|
|
|
* @param bool $debug |
|
19
|
|
|
* |
|
20
|
|
|
* @return $this |
|
21
|
|
|
*/ |
|
22
|
1 |
|
public static function createFromException(\Exception $exception, $debug = false) |
|
23
|
|
|
{ |
|
24
|
1 |
|
$extra = $debug ? ['trace' => $exception->getTrace()] : []; |
|
25
|
|
|
|
|
26
|
1 |
|
return static::create(Response::HTTP_INTERNAL_SERVER_ERROR, 'Internal Server Error', $exception) |
|
27
|
1 |
|
->error('internal-error', [], $exception->getMessage(), $extra); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ConstraintViolationListInterface $errors |
|
32
|
|
|
* |
|
33
|
|
|
* @return RestException |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public static function createFromConstraintViolationList(ConstraintViolationListInterface $errors) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$exception = static::createUnprocessable('Input validation errors.'); |
|
38
|
|
|
|
|
39
|
|
|
/** @var ConstraintViolationInterface $error */ |
|
40
|
1 |
|
foreach ($errors as $error) { |
|
41
|
1 |
|
$exception->errorValidation($error->getPropertyPath(), $error->getMessage()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
return $exception; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $message |
|
49
|
|
|
* |
|
50
|
|
|
* @return RestException |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public static function createForbidden($message = 'Forbidden.') |
|
53
|
|
|
{ |
|
54
|
1 |
|
return static::create(Response::HTTP_FORBIDDEN, $message); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $message |
|
59
|
|
|
* |
|
60
|
|
|
* @return RestException |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public static function createUnprocessable($message = '') |
|
63
|
|
|
{ |
|
64
|
2 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY, $message); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param int|array $id |
|
69
|
|
|
* @param string $resourceKey |
|
70
|
|
|
* @param string $message |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public static function createNotFound($id, $resourceKey, $message = '') |
|
75
|
|
|
{ |
|
76
|
1 |
|
return static::create(Response::HTTP_NOT_FOUND, 'Entity not found.') |
|
77
|
1 |
|
->error('entity-not-found', ['type' => $resourceKey, 'id' => $id], $message); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array $source |
|
82
|
|
|
* @param $message |
|
83
|
|
|
* |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public static function createFilterError(array $source, $message) |
|
87
|
|
|
{ |
|
88
|
1 |
|
return static::create(Response::HTTP_BAD_REQUEST, 'Wrong filter input.') |
|
89
|
1 |
|
->error('filter-input', $source, $message); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return static |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public static function missingRootData() |
|
96
|
|
|
{ |
|
97
|
1 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
|
98
|
1 |
|
->error('missing-root-data', ['pointer' => ''], "Missing `data` member at document top level."); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $pointer |
|
103
|
|
|
* |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public static function missingData($pointer) |
|
107
|
|
|
{ |
|
108
|
1 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
|
109
|
1 |
|
->error('missing-data', ['pointer' => $pointer], "Missing `data` member at pointer level."); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $pointer |
|
114
|
|
|
* |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public static function missingDataMembers($pointer) |
|
118
|
|
|
{ |
|
119
|
1 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
|
120
|
1 |
|
->error('missing-data-members', ['pointer' => $pointer], |
|
121
|
1 |
|
'Missing or not array `data.attributes` or `data.relationships` at pointer level.' |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $pointer |
|
127
|
|
|
* |
|
128
|
|
|
* @return $this |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public static function unknownAttribute($pointer) |
|
131
|
|
|
{ |
|
132
|
1 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
|
133
|
1 |
|
->error('unknown-attribute', ['pointer' => $pointer], 'Unknown attribute.'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $pointer |
|
138
|
|
|
* |
|
139
|
|
|
* @return static |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public static function unknownRelation($pointer) |
|
142
|
|
|
{ |
|
143
|
1 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
|
144
|
1 |
|
->error('unknown-relation', ['pointer' => $pointer], 'Unknown relation.'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return static |
|
149
|
|
|
*/ |
|
150
|
11 |
|
public static function invalidInclude() |
|
151
|
|
|
{ |
|
152
|
11 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
|
153
|
11 |
|
->error('invalid-include', ['pointer' => 'include'], 'Invalid `include` param.'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param int $httpStatus |
|
158
|
|
|
* @param string $message |
|
159
|
|
|
* @param \Exception|null $previous |
|
160
|
|
|
* |
|
161
|
|
|
* @return static |
|
162
|
|
|
*/ |
|
163
|
16 |
|
static public function create( |
|
164
|
|
|
$httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR, |
|
165
|
|
|
$message = '', |
|
166
|
|
|
\Exception $previous = null |
|
167
|
|
|
) { |
|
168
|
16 |
|
return new static($httpStatus, $message, $previous); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* RestException constructor. |
|
173
|
|
|
* |
|
174
|
|
|
* @param int $httpStatus |
|
175
|
|
|
* @param string $message |
|
176
|
|
|
* @param \Exception|null $previous |
|
177
|
|
|
*/ |
|
178
|
16 |
|
public function __construct( |
|
179
|
|
|
$httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR, |
|
180
|
|
|
$message = '', |
|
181
|
|
|
\Exception $previous = null |
|
182
|
|
|
) { |
|
183
|
16 |
|
parent::__construct($message, $httpStatus, $previous); |
|
184
|
16 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param string $pointer |
|
188
|
|
|
* @param string $detail |
|
189
|
|
|
* @param array $extra |
|
190
|
|
|
* |
|
191
|
|
|
* @return RestException |
|
192
|
|
|
*/ |
|
193
|
1 |
|
public function errorValidation($pointer, $detail, array $extra = []) |
|
194
|
|
|
{ |
|
195
|
1 |
|
return $this->error('validation', ['pointer' => $pointer], $detail, $extra); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param string $applicationCode |
|
200
|
|
|
* @param array $source |
|
201
|
|
|
* @param string $detail |
|
202
|
|
|
* @param array $extra |
|
203
|
|
|
* |
|
204
|
|
|
* @return $this |
|
205
|
|
|
*/ |
|
206
|
15 |
|
public function error($applicationCode, $source, $detail, array $extra = []) |
|
207
|
|
|
{ |
|
208
|
15 |
|
$this->errors[] = array_merge(['code' => $applicationCode, 'source' => $source, 'detail' => $detail] + $extra); |
|
209
|
|
|
|
|
210
|
15 |
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @return array |
|
215
|
|
|
*/ |
|
216
|
5 |
|
public function errors() |
|
217
|
|
|
{ |
|
218
|
5 |
|
return $this->errors; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|