@@ -8,96 +8,73 @@ https://jsonapi.org/examples/#error-objects-basics
8
8
9
9
## Usage
10
10
11
- In your Laravel controller:
11
+ ### Basic Usage
12
12
``` php
13
13
<?php
14
14
15
- namespace App\Http\Controllers;
16
-
17
15
use Sinemah\JsonApi\Error\Error;
18
- use Sinemah\JsonApi\Error\Responses\Laravel;
19
- use Illuminate\Http\JsonResponse;
16
+ use Sinemah\JsonApi\Error\ErrorBag;
20
17
21
- class AnyController extends Controller
22
- {
23
- public function show(): JsonResponse
24
- {
25
- return Laravel::get()->json(
26
- Error::fromArray(
27
- [
28
- 'status' => 404,
29
- 'source' => null,
30
- 'title' => 'Item not found',
31
- 'detail' => sprintf('Item %s not found', request('item_uuid')),
32
- ]
33
- ),
34
- 404
35
- );
36
- }
37
- }
18
+ $errors = new ErrorBag();
19
+
20
+ $errors->add(
21
+ Error::fromArray(
22
+ [
23
+ 'status' => 404,
24
+ 'source' => null,
25
+ 'title' => 'Item not found',
26
+ 'detail' => sprintf('Item %s not found', 'some-id'),
27
+ ]
28
+ )
29
+ );
30
+
31
+ $errors->toArray()
38
32
```
39
33
40
- Response
34
+ Result as JSON representation
41
35
``` json
42
- {
43
- "errors" : [
44
- {
45
- "status" : 404 ,
46
- "title" : " Bike not found" ,
47
- "detail" : " Bike bd11f048-8663-4d95-8c7a-02a5579b0682 not found in customer data"
48
- }
49
- ]
50
- }
36
+ [
37
+ {
38
+ "status" : 404 ,
39
+ "title" : " Item not found" ,
40
+ "detail" : " Item some-id not found"
41
+ }
42
+ ]
51
43
```
52
44
53
- Build an error stack.
45
+ ### Response Usage
46
+
54
47
``` php
55
48
<?php
56
49
57
- namespace App\Http\Controllers;
58
-
59
50
use Sinemah\JsonApi\Error\Error;
60
- use Sinemah\JsonApi\Error\Responses\Laravel;
61
- use Illuminate\Http\JsonResponse;
51
+ use Sinemah\JsonApi\Error\Response;
62
52
63
- class AnyController extends Controller
64
- {
65
- public function show(): JsonResponse
66
- {
67
- return Laravel::get()
68
- ->add(Error::fromArray(['status' => 500, 'code' => 'first_error']))
69
- ->add(Error::fromArray(['status' => 500, 'code' => 'second_error']))
70
- ->add(Error::fromArray(['status' => 500, 'code' => 'third_error']))
71
- ->json();
72
- }
73
- }
53
+ $response = Response::get();
54
+
55
+ $response->add(
56
+ Error::fromArray(
57
+ [
58
+ 'status' => 404,
59
+ 'source' => null,
60
+ 'title' => 'Item not found',
61
+ 'detail' => sprintf('Item %s not found', 'some-id'),
62
+ ]
63
+ )
64
+ );
65
+
66
+ $response->toArray()
74
67
```
75
- Response
68
+
69
+ Result as JSON representation
76
70
``` json
77
71
{
78
72
"errors" : [
79
73
{
80
- "status" : 500 ,
81
- "code" : " first_error"
82
- },
83
- {
84
- "status" : 500 ,
85
- "code" : " second_error"
86
- },
87
- {
88
- "status" : 500 ,
89
- "code" : " third_error"
74
+ "status" : 404 ,
75
+ "title" : " Item not found" ,
76
+ "detail" : " Item some-id not found"
90
77
}
91
78
]
92
79
}
93
- ```
94
-
95
- ## Laravel Response
96
- You do not need to pass a status code via the json method. The status code will be fetched from the first error you pushed in the bag.
97
- ``` php
98
- ->json()
99
- ```
100
- You can also overwrite the status code with the json method.
101
- ``` php
102
- ->json(null, 401)
103
80
```
0 commit comments