Skip to content

Commit c6e2325

Browse files
committed
remove laravel class, update README.md
1 parent 2b0fde4 commit c6e2325

File tree

4 files changed

+47
-129
lines changed

4 files changed

+47
-129
lines changed

README.md

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
1715
use 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-
5950
use 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
```

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"JSON API",
66
"Error Responses"
77
],
8-
"homepage": "",
8+
"homepage": "https://github.com/SineMah/json-api-error",
99
"license": "MIT",
1010
"authors": [
1111
{
@@ -23,8 +23,5 @@
2323
},
2424
"autoload-dev": {},
2525
"scripts": {},
26-
"require-dev": {
27-
"laravel/framework": "^9.12"
28-
},
2926
"minimum-stability": "dev"
3027
}

src/Exceptions/StatusUnavailableException.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Responses/Laravel.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)