@@ -8,96 +8,73 @@ https://jsonapi.org/examples/#error-objects-basics
88
99## Usage
1010
11- In your Laravel controller:
11+ ### Basic Usage
1212``` php
1313<?php
1414
15- namespace App\Http\Controllers;
16-
1715use Sinemah\JsonApi\Error\Error;
18- use Sinemah\JsonApi\Error\Responses\Laravel;
19- use Illuminate\Http\JsonResponse;
16+ use Sinemah\JsonApi\Error\ErrorBag;
2017
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()
3832```
3933
40- Response
34+ Result as JSON representation
4135``` 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+ ]
5143```
5244
53- Build an error stack.
45+ ### Response Usage
46+
5447``` php
5548<?php
5649
57- namespace App\Http\Controllers;
58-
5950use Sinemah\JsonApi\Error\Error;
60- use Sinemah\JsonApi\Error\Responses\Laravel;
61- use Illuminate\Http\JsonResponse;
51+ use Sinemah\JsonApi\Error\Response;
6252
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()
7467```
75- Response
68+
69+ Result as JSON representation
7670``` json
7771{
7872 "errors" : [
7973 {
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"
9077 }
9178 ]
9279}
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)
10380```
0 commit comments