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