Skip to content

Commit a33f2ad

Browse files
committed
2 parents f8805f4 + cc866e3 commit a33f2ad

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

README.md

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Laravel Faulty - RESTful Exceptions
22

3-
A Laravel/Lumen package that lets you handle the API problems with ease. All the thrown exceptions HTTP or non-HTTP are properly rendered as JSON.
3+
> Automatically turns your thrown exceptions (HTTP/non-HTTP) to the JSON response while conforming to API problem specification.
4+
5+
A Laravel/Lumen package that lets you handle the API problems with ease.
46

57
Faulty provides a straightforward implementation of [IETF Problem Specification](https://tools.ietf.org/html/draft-nottingham-http-problem-07) and turns your exceptions to be returned in the below format with the content type of `application/problem+json`
68

@@ -20,44 +22,51 @@ Where
2022
- `detail` is human readable explanation specific to problem
2123
- `instance` is the absolute URI that identifies the specific occurrence of the problem
2224

23-
> Faulty automatically turns your thrown exceptions to the specified response.
24-
2525
## Installation
2626

2727
Run the below command
28+
2829
```
2930
composer require kamranahmedse/laravel-faulty
3031
```
3132

3233
Make your exception handler i.e. `App\Exceptions\Handler` that can be found at `app\Exceptions\Handler.php` extend from the Faulty's handler i.e.
3334

34-
```
35-
\KamranAhmed\Faulty\Handler
36-
```
37-
38-
And that's it. You are all set to use Faulty.
35+
```php
36+
37+
use KamranAhmed\Faulty\Handler as FaultyHandler;
38+
39+
class Handler extends FaultyHandler {
40+
// ...
41+
}
42+
```
3943

40-
## Configuration
41-
Faulty relies on the following environment configurations
44+
And that's it. You are all set to use Faulty.
4245

43-
- `APP_DEBUG` : If `true`, exceptions will be rendered with whoops, if false JSON will be returned. **Defaults to `false`**
44-
- `APP_DEBUG_TRACE` : If true, stack trace will be included in the application errors. **Defaults to `true`**
46+
##Configuration
47+
Faulty relies on the following environment configurations
48+
49+
- `APP_DEBUG` : If `true`, exceptions will be rendered with whoops, if false JSON will be returned. **Defaults to `false`**
50+
- `APP_DEBUG_TRACE` : If true, stack trace will be included in the application errors. **Defaults to `true`**
4551

46-
## Usage
52+
## Usage
4753

48-
For HTTP exceptions to be rendered properly with the proper status codes, you should use the exception classes provided by faulty i.e. the ones available in `Faulty\Exceptions` namespace or use the relevant ones provided by the Symfony's HTTP component i.e. the ones available under `Symfony\Component\HttpKernel\Exception`
54+
For HTTP exceptions to be rendered properly with the proper status codes, you should use the exception classes provided by faulty i.e. the ones available in `Faulty\Exceptions` namespace or use the relevant ones provided by the Symfony's HTTP component i.e. the ones available under `Symfony\Component\HttpKernel\Exception`
4955

50-
#### Throwing Exceptions
51-
All the exception classes have the below signature
52-
```php
53-
\KamranAhmed\Faulty\Exceptions\[ProblemType]Exception($detail, $title = '', $instance = '', $type = '')
54-
```
56+
####Throwing Exceptions
57+
58+
All the exception classes have the below signature
59+
60+
```php
61+
use KamranAhmed\Faulty\Exceptions\[ProblemType]Exception;
62+
[ProblemType]Exception($detail, $title = '', $instance = '', $type = '')
63+
```
64+
5565
Here are some of the provided exception classes
5666

5767
```php
5868
// Include the exception classes from the given namespace
5969

60-
6170
throw new BadRequestException('Invalid request data');
6271
throw new ConflictException('Same request is already pending');
6372
throw new ForbiddenException('You are not allowed to perform this action');
@@ -77,12 +86,16 @@ throw new UnprocessableEntityException('..');
7786
Also, if you would like to return any response for which the exception class isn't available, you can use the `HttpException` class i.e.
7887

7988
```php
80-
throw new \KamranAhmed\Faulty\Exceptions\HttpException($title = '', $status = 500, $detail = '', $instance = '', $type = '');
89+
use KamranAhmed\Faulty\Exceptions\HttpException;
90+
91+
throw new HttpException($title = '', $status = 500, $detail = '', $instance = '', $type = '');
8192
```
8293
For example
8394

8495
```php
85-
throw new \KamranAhmed\Faulty\Exceptions\HttpException('Unsupported Media Type', 415);
96+
use KamranAhmed\Faulty\Exceptions\HttpException;
97+
98+
throw new HttpException('Unsupported Media Type', 415);
8699
```
87100

88101
#### Syntactic Sugar
@@ -98,6 +111,7 @@ $occurence = route('account.error', ['account_id' => 'A837332A', 'log_id' => 34]
98111
->setInstance($occurence)
99112
->toss();
100113
```
114+
101115
Also, if you would like to send additional data in response, call the method `setAdditional([])` on the error object while passing the additional detail i.e.
102116

103117
```php

0 commit comments

Comments
 (0)