2
2
[ ![ Installs] ( https://img.shields.io/packagist/dt/phpro/api-problem.svg )] ( https://packagist.org/packages/phpro/api-problem/stats )
3
3
[ ![ Packagist] ( https://img.shields.io/packagist/v/phpro/api-problem.svg )] ( https://packagist.org/packages/phpro/api-problem )
4
4
5
-
6
5
# Api Problem
7
6
8
7
This package provides a [ RFC7807] ( https://tools.ietf.org/html/rfc7807 ) Problem details implementation.
@@ -13,7 +12,6 @@ Since handling the exceptions is up to the framework, here is a list of known fr
13
12
14
13
- ** Symfony** ` ^4.1 ` : [ ApiProblemBundle] ( https://www.github.com/phpro/api-problem-bundle )
15
14
16
-
17
15
## Installation
18
16
19
17
``` sh
@@ -34,14 +32,26 @@ throw new ApiProblemException(
34
32
35
33
### Built-in problems
36
34
37
- - [ ExceptionApiProblem] ( #exceptionapiproblem )
38
- - [ ForbiddenProblem] ( #forbiddenproblem )
39
- - [ HttpApiProblem] ( #httpapiproblem )
40
- - [ NotFoundProblem] ( #notfoundproblem )
41
- - [ UnauthorizedProblem] ( #unauthorizedproblem )
42
- - [ ValidationApiProblem] ( #validationapiproblem )
43
- - [ BadRequestProblem] ( #badrequestproblem )
44
- - [ ConflictProblem] ( #conflictproblem )
35
+ - General problems
36
+ - [ ExceptionApiProblem] ( #exceptionapiproblem )
37
+ - [ HttpApiProblem] ( #httpapiproblem )
38
+
39
+ - Symfony integration problems
40
+ - [ ValidationApiProblem] ( #validationapiproblem )
41
+
42
+ - Http problems
43
+ - 400 [ BadRequestProblem] ( #badrequestproblem )
44
+ - 401 [ UnauthorizedProblem] ( #unauthorizedproblem )
45
+ - 403 [ ForbiddenProblem] ( #forbiddenproblem )
46
+ - 404 [ NotFoundProblem] ( #notfoundproblem )
47
+ - 405 [ MethodNotAllowedProblem] ( #methodnotallowedproblem )
48
+ - 409 [ ConflictProblem] ( #conflictproblem )
49
+ - 412 [ PreconditionFailedProblem] ( #preconditionfailedproblem )
50
+ - 415 [ UnsupportedMediaTypeProblem] ( #unsupportedmediatypeproblem )
51
+ - 418 [ IAmATeapotProblem] ( #iamateapotproblem )
52
+ - 422 [ UnprocessableEntityProblem] ( #unprocessableentityproblem )
53
+ - 423 [ LockedProblem] ( #lockedproblem )
54
+ - 428 [ PreconditionRequiredProblem] ( #preconditionrequiredproblem )
45
55
46
56
#### ExceptionApiProblem
47
57
@@ -90,54 +100,69 @@ new ExceptionApiProblem(new \Exception('message', 500));
90
100
}
91
101
````
92
102
93
- #### ForbiddenProblem
103
+ #### HttpApiProblem
94
104
95
105
```php
96
- use Phpro\ApiProblem\Http\ForbiddenProblem ;
106
+ use Phpro\ApiProblem\Http\HttpApiProblem ;
97
107
98
- new ForbiddenProblem('Not authorized to access gold.' );
108
+ new HttpApiProblem(404, ['detail' => 'The book could not be found.'] );
99
109
```
100
110
101
111
``` json
102
112
{
103
- "status" : 403 ,
104
- "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
105
- "title" : " Forbidden " ,
106
- "detail" : " Not authorized to access gold ."
113
+ "status" : 404 ,
114
+ "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
115
+ "title" : " Not found " ,
116
+ "detail" : " The book could not be found ."
107
117
}
108
118
````
109
119
110
- #### HttpApiProblem
120
+ #### ValidationApiProblem
121
+
122
+ ```sh
123
+ composer require symfony/validator:^4.1
124
+ ```
111
125
112
126
``` php
113
- use Phpro\ApiProblem\Http\HttpApiProblem;
127
+ use Phpro\ApiProblem\Http\ValidationApiProblem;
128
+ use Symfony\Component\Validator\ConstraintViolation;
129
+ use Symfony\Component\Validator\ConstraintViolationList;
114
130
115
- new HttpApiProblem(404, ['detail' => 'The book could not be found.']);
131
+ new ValidationApiProblem(new ConstraintViolationList([
132
+ new ConstraintViolation('Invalid email', '', [], '', 'email', '', null, '8615ecd9-afcb-479a-9c78-8bcfe260cf2a'),
133
+ ]));
116
134
```
117
135
118
136
``` json
119
137
{
120
- "status" : 404 ,
121
- "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
122
- "title" : " Not found" ,
123
- "detail" : " The book could not be found."
138
+ "status" : 400 ,
139
+ "type" : " https:\/\/ symfony.com\/ errors\/ validation" ,
140
+ "title" : " Validation Failed" ,
141
+ "detail" : " email: Invalid Email" ,
142
+ "violations" : [
143
+ {
144
+ "propertyPath" : " email" ,
145
+ "title" : " Invalid email" ,
146
+ "type" : " urn:uuid:8615ecd9-afcb-479a-9c78-8bcfe260cf2a"
147
+ }
148
+ ]
124
149
}
125
150
````
126
151
127
- #### NotFoundProblem
152
+ #### BadRequestProblem
128
153
129
154
```php
130
- use Phpro\ApiProblem\Http\NotFoundProblem ;
155
+ use Phpro\ApiProblem\Http\BadRequestProblem ;
131
156
132
- new NotFoundProblem('The book with ID 20 could not be found .');
157
+ new BadRequestProblem('Bad request. Bad! .');
133
158
```
134
159
135
160
``` json
136
161
{
137
- "status" : 404 ,
162
+ "status" : 400 ,
138
163
"type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
139
- "title" : " Not found " ,
140
- "detail" : " The book with ID 20 could not be found. "
164
+ "title" : " Bad Request " ,
165
+ "detail" : " Bad request. Bad! "
141
166
}
142
167
````
143
168
@@ -158,52 +183,54 @@ new UnauthorizedProblem('You are not authorized to access X.');
158
183
}
159
184
````
160
185
161
- #### ValidationApiProblem
186
+ #### ForbiddenProblem
162
187
163
- ```sh
164
- composer require symfony/validator:^4.1
188
+ ```php
189
+ use Phpro\ApiProblem\Http\ForbiddenProblem;
190
+
191
+ new ForbiddenProblem('Not authorized to access gold.');
165
192
```
166
193
194
+ ``` json
195
+ {
196
+ "status" : 403 ,
197
+ "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
198
+ "title" : " Forbidden" ,
199
+ "detail" : " Not authorized to access gold."
200
+ }
201
+ ````
202
+
203
+ #### NotFoundProblem
204
+
167
205
```php
168
- use Phpro\ApiProblem\Http\ValidationApiProblem;
169
- use Symfony\Component\Validator\ConstraintViolation;
170
- use Symfony\Component\Validator\ConstraintViolationList;
206
+ use Phpro\ApiProblem\Http\NotFoundProblem;
171
207
172
- new ValidationApiProblem(new ConstraintViolationList([
173
- new ConstraintViolation('Invalid email', '', [], '', 'email', '', null, '8615ecd9-afcb-479a-9c78-8bcfe260cf2a'),
174
- ]));
208
+ new NotFoundProblem('The book with ID 20 could not be found.');
175
209
```
176
210
177
211
``` json
178
212
{
179
- "status" : 400 ,
180
- "type" : " https:\/\/ symfony.com\/ errors\/ validation" ,
181
- "title" : " Validation Failed" ,
182
- "detail" : " email: Invalid Email" ,
183
- "violations" : [
184
- {
185
- "propertyPath" : " email" ,
186
- "title" : " Invalid email" ,
187
- "type" : " urn:uuid:8615ecd9-afcb-479a-9c78-8bcfe260cf2a"
188
- }
189
- ]
213
+ "status" : 404 ,
214
+ "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
215
+ "title" : " Not found" ,
216
+ "detail" : " The book with ID 20 could not be found."
190
217
}
191
218
````
192
219
193
- #### BadRequestProblem
220
+ #### MethodNotAllowedProblem
194
221
195
222
```php
196
- use Phpro\ApiProblem\Http\BadRequestProblem ;
223
+ use Phpro\ApiProblem\Http\MethodNotAllowedProblem ;
197
224
198
- new BadRequestProblem('Bad request. Bad! .');
225
+ new MethodNotAllowedProblem('Only POST and GET allowed .');
199
226
```
200
227
201
228
``` json
202
229
{
203
- "status" : 400 ,
230
+ "status" : 405 ,
204
231
"type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
205
- "title" : " Bad Request " ,
206
- "detail" : " Bad request. Bad! "
232
+ "title" : " Method Not Allowed " ,
233
+ "detail" : " Only POST and GET allowed. "
207
234
}
208
235
````
209
236
@@ -223,6 +250,108 @@ new ConflictProblem('Duplicated key for book with ID 20.');
223
250
}
224
251
````
225
252
253
+ #### PreconditionFailedProblem
254
+
255
+ ```php
256
+ use Phpro\ApiProblem\Http\PreconditionFailedProblem;
257
+
258
+ new PreconditionFailedProblem('Incorrect entity tag provided.');
259
+ ```
260
+
261
+ ``` json
262
+ {
263
+ "status" : 412 ,
264
+ "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
265
+ "title" : " Precondition Failed" ,
266
+ "detail" : " Incorrect entity tag provided."
267
+ }
268
+ ````
269
+
270
+ #### UnsupportedMediaTypeProblem
271
+
272
+ ```php
273
+ use Phpro\ApiProblem\Http\UnsupportedMediaTypeProblem;
274
+
275
+ new UnsupportedMediaTypeProblem('Please provide valid JSON.');
276
+ ```
277
+
278
+ ``` json
279
+ {
280
+ "status" : 415 ,
281
+ "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
282
+ "title" : " Unsupported Media Type" ,
283
+ "detail" : " Please provide valid JSON."
284
+ }
285
+ ````
286
+
287
+ #### IAmATeapotProblem
288
+
289
+ ```php
290
+ use Phpro\ApiProblem\Http\IAmATeapotProblem;
291
+
292
+ new IAmATeapotProblem('More tea please.');
293
+ ```
294
+
295
+ ``` json
296
+ {
297
+ "status" : 418 ,
298
+ "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
299
+ "title" : " I'm a teapot" ,
300
+ "detail" : " More tea please."
301
+ }
302
+ ````
303
+
304
+ #### UnprocessableEntityProblem
305
+
306
+ ```php
307
+ use Phpro\ApiProblem\Http\UnprocessableEntityProblem;
308
+
309
+ new UnprocessableEntityProblem('Unable to process the contained instructions.');
310
+ ```
311
+
312
+ ``` json
313
+ {
314
+ "status" : 422 ,
315
+ "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
316
+ "title" : " Unprocessable Entity" ,
317
+ "detail" : " Unable to process the contained instructions."
318
+ }
319
+ ````
320
+
321
+ #### LockedProblem
322
+
323
+ ```php
324
+ use Phpro\ApiProblem\Http\LockedProblem;
325
+
326
+ new LockedProblem('This door is locked.');
327
+ ```
328
+
329
+ ``` json
330
+ {
331
+ "status" : 423 ,
332
+ "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
333
+ "title" : " Locked" ,
334
+ "detail" : " This door is locked."
335
+ }
336
+ ````
337
+
338
+ #### PreconditionRequiredProblem
339
+
340
+ ```php
341
+ use Phpro\ApiProblem\Http\PreconditionRequiredProblem;
342
+
343
+ new PreconditionRequiredProblem('If-match header is required.');
344
+ ```
345
+
346
+ ``` json
347
+ {
348
+ "status" : 428 ,
349
+ "type" : " http:\/\/ www.w3.org\/ Protocols\/ rfc2616\/ rfc2616-sec10.html" ,
350
+ "title" : " Precondition Required" ,
351
+ "detail" : " If-match header is required."
352
+ }
353
+ ````
354
+
226
355
### Creating your own problem
227
356
228
357
Creating problem sounds scary right!?
0 commit comments