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