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
|
1 |
|
public static function createFromException(\Exception $exception, $debug = false) |
22
|
|
|
{ |
23
|
1 |
|
$extra = $debug ? ['trace' => $exception->getTrace()] : []; |
24
|
|
|
|
25
|
1 |
|
return static::create(Response::HTTP_INTERNAL_SERVER_ERROR, 'Internal Server Error', $exception) |
26
|
1 |
|
->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
|
1 |
|
public static function createForbidden($message = 'Forbidden.') |
52
|
|
|
{ |
53
|
1 |
|
return static::create(Response::HTTP_FORBIDDEN, $message); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $message |
58
|
|
|
* |
59
|
|
|
* @return RestException |
60
|
|
|
*/ |
61
|
2 |
|
public static function createUnprocessable($message = '') |
62
|
|
|
{ |
63
|
2 |
|
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
|
1 |
|
public static function missingRootData() |
95
|
|
|
{ |
96
|
1 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
97
|
1 |
|
->error('missing-root-data', ['pointer' => ''], "Missing `data` member at document top level."); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $pointer |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
1 |
|
public static function missingData($pointer) |
106
|
|
|
{ |
107
|
1 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
108
|
1 |
|
->error('missing-data', ['pointer' => $pointer], "Missing `data` member at pointer level."); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $pointer |
113
|
|
|
* |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
1 |
|
public static function missingDataMembers($pointer) |
117
|
|
|
{ |
118
|
1 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
119
|
1 |
|
->error('missing-data-members', ['pointer' => $pointer], |
120
|
1 |
|
'Missing or not array `data.attributes` or `data.relationships` at pointer level.' |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $pointer |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
1 |
|
public static function unknownAttribute($pointer) |
130
|
|
|
{ |
131
|
1 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
132
|
1 |
|
->error('unknown-attribute', ['pointer' => $pointer], 'Unknown attribute.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $pointer |
137
|
|
|
* |
138
|
|
|
* @return static |
139
|
|
|
*/ |
140
|
1 |
|
public static function unknownRelation($pointer) |
141
|
|
|
{ |
142
|
1 |
|
return static::create(Response::HTTP_UNPROCESSABLE_ENTITY) |
143
|
1 |
|
->error('unknown-relation', ['pointer' => $pointer], 'Unknown relation.'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param int $httpStatus |
148
|
|
|
* @param string $message |
149
|
|
|
* @param \Exception|null $previous |
150
|
|
|
* |
151
|
|
|
* @return static |
152
|
|
|
*/ |
153
|
6 |
|
static public function create( |
154
|
|
|
$httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR, |
155
|
|
|
$message = '', |
156
|
|
|
\Exception $previous = null |
157
|
|
|
) { |
158
|
6 |
|
return new static($httpStatus, $message, $previous); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* RestException constructor. |
163
|
|
|
* |
164
|
|
|
* @param int $httpStatus |
165
|
|
|
* @param string $message |
166
|
|
|
* @param \Exception|null $previous |
167
|
|
|
*/ |
168
|
6 |
|
public function __construct( |
169
|
|
|
$httpStatus = Response::HTTP_INTERNAL_SERVER_ERROR, |
170
|
|
|
$message = '', |
171
|
|
|
\Exception $previous = null |
172
|
|
|
) { |
173
|
6 |
|
parent::__construct($message, $httpStatus, $previous); |
174
|
6 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $pointer |
178
|
|
|
* @param string $detail |
179
|
|
|
* @param array $extra |
180
|
|
|
* |
181
|
|
|
* @return RestException |
182
|
|
|
*/ |
183
|
1 |
|
public function errorValidation($pointer, $detail, array $extra = []) |
184
|
|
|
{ |
185
|
1 |
|
return $this->error('validation', ['pointer' => $pointer], $detail, $extra); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $applicationCode |
190
|
|
|
* @param array $source |
191
|
|
|
* @param string $detail |
192
|
|
|
* @param array $extra |
193
|
|
|
* |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
5 |
|
public function error($applicationCode, $source, $detail, array $extra = []) |
197
|
|
|
{ |
198
|
5 |
|
$this->errors[] = array_merge(['code' => $applicationCode, 'source' => $source, 'detail' => $detail] + $extra); |
199
|
|
|
|
200
|
5 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
5 |
|
public function errors() |
207
|
|
|
{ |
208
|
5 |
|
return $this->errors; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|