|
1 |
| -# Custom Errors |
| 1 | +# Errors |
2 | 2 |
|
3 | 3 | ## Introduction
|
4 | 4 |
|
5 |
| -This package allows you to return [JSON API error objects](http://jsonapi.org/format/1.0/#error-objects) which can include various information about what went wrong. |
| 5 | +The JSON API spec defines [error objects](http://jsonapi.org/format/#errors) that are returned |
| 6 | +in the top-level `errors` member of the response body. This package automatically handles converting |
| 7 | +common Laravel exceptions to these error objects, for example HTTP exceptions and validation exceptions. |
6 | 8 |
|
7 |
| -## Custom Errors In Controllers |
| 9 | +In addition, it provides the ability for you to return custom errors as needed. This chapter describes |
| 10 | +how to return your own error responses. |
8 | 11 |
|
9 |
| -This is particularly useful for errors that you have to manually present to the user through the controller. In order to magically return errors, you would need to use the `ErrorsAwareTrait`. |
| 12 | +## Creating Error Objects |
10 | 13 |
|
| 14 | +Error objects can be constructed from array key/value pairs using the static `create` method on |
| 15 | +the package's error class. All the keys described in the specification's |
| 16 | +[error objects](http://jsonapi.org/format/#error-objects) chapter are supported. |
11 | 17 |
|
12 |
| -Below is an example of a scenario which a user fails to login due to incorrect credentials. |
| 18 | +For example: |
13 | 19 |
|
14 | 20 | ```php
|
15 |
| -namespace App\Http\Controllers\Api\V1; |
| 21 | +use CloudCreativity\LaravelJsonApi\Document\Error; |
| 22 | + |
| 23 | +$error = Error::create([ |
| 24 | + 'id' => '91053382-7c00-45eb-bdcc-8359d03debbb', |
| 25 | + 'status' => '500', |
| 26 | + 'code' => 'unexpected', |
| 27 | + 'title' => 'Unexpected Error', |
| 28 | + 'detail' => 'Something went wrong.', |
| 29 | + 'meta' => ['foo' => 'bar'], |
| 30 | +]); |
| 31 | +``` |
| 32 | + |
| 33 | +## Controller Hooks |
| 34 | + |
| 35 | +All [controller hooks](../basics/controllers.md) can return a HTTP response that will be used instead |
| 36 | +of the normal response generated by the controller. This means you can use these controller hooks |
| 37 | +to return JSON API error responses. |
| 38 | + |
| 39 | +The `JsonApiController` has a responses factory that can create error responses. This can be used |
| 40 | +to return an error response in a controller hook, as demonstrated in the following example: |
16 | 41 |
|
17 |
| -use CloudCreativity\LaravelJsonApi\Contracts\Http\Requests\RequestInterface as JsonApiRequest; |
| 42 | +```php |
18 | 43 | use CloudCreativity\LaravelJsonApi\Document\Error;
|
19 | 44 |
|
20 |
| -class JsonWebTokensController extends Controller |
| 45 | +class PaymentController extends JsonApiController |
21 | 46 | {
|
22 |
| - protected function guard() |
23 |
| - { |
24 |
| - return Auth::guard('jwt'); |
25 |
| - } |
26 | 47 |
|
27 |
| - public function create(JsonApiRequest $request) |
| 48 | + protected function creating() |
28 | 49 | {
|
29 |
| - $resource_attributes = $request->getDocument()->getResource()->getAttributes(); |
30 |
| - $credentials = [ |
31 |
| - 'email' => $resource_attributes->email, |
32 |
| - 'password' => $resource_attributes->password, |
33 |
| - ]; |
34 |
| - |
35 |
| - if ($this->guard()->attempt($credentials)) { |
36 |
| - // Success! |
37 |
| - } else { |
38 |
| - // Incorrect login details |
| 50 | + if (/** some condition */) { |
39 | 51 | return $this->reply()->errors(Error::create([
|
40 |
| - 'status' => 422, |
41 |
| - 'title' => 'Login failed.', |
42 |
| - 'detail' => 'These credentials do not match our records.' |
| 52 | + 'title' => 'Payment Required', |
| 53 | + 'detail' => 'Your card has expired.', |
| 54 | + 'status' => '402', |
43 | 55 | ]));
|
44 |
| - |
45 | 56 | }
|
46 | 57 | }
|
47 | 58 | }
|
48 | 59 | ```
|
49 | 60 |
|
50 | 61 | And the response given would be the following:
|
51 | 62 |
|
52 |
| -```json |
| 63 | +```http |
| 64 | +HTTP/1.1 402 Payment Required |
| 65 | +Content-Type: application/vnd.api+json |
| 66 | +
|
53 | 67 | {
|
54 | 68 | "errors": [
|
55 | 69 | {
|
56 |
| - "status": "422", |
57 |
| - "title": "Login failed.", |
58 |
| - "detail": "These credentials do not match our records." |
| 70 | + "title": "Payment Required", |
| 71 | + "detail": "Your card has expired." |
| 72 | + "status": "402" |
59 | 73 | }
|
60 | 74 | ]
|
61 | 75 | }
|
62 |
| -``` |
| 76 | +``` |
| 77 | + |
| 78 | +You can pass either a single error object, or an array of error objects to the `errors()` reply method. |
| 79 | +The HTTP status of the response will be calculated from the status in the error objects. |
| 80 | + |
| 81 | +## Throwing Errors |
| 82 | + |
| 83 | +It is also possible to throw a `JsonApiException` from anywhere in your code. This will be converted |
| 84 | +to a JSON API response. For example: |
| 85 | + |
| 86 | +```php |
| 87 | +use Neomerx\JsonApi\Exceptions\JsonApiException; |
| 88 | +use CloudCreativity\LaravelJsonApi\Document\Error; |
| 89 | + |
| 90 | +try { |
| 91 | + dispatchNow(new ChargeCard($token)); |
| 92 | +} catch (\App\PaymentException $ex) { |
| 93 | + Error::create([ |
| 94 | + 'title' => 'Payment Required', |
| 95 | + 'detail' => $ex->getMessage(), |
| 96 | + 'status' => '402', |
| 97 | + ]); |
| 98 | + |
| 99 | + throw new JsonApiException($error, 402, $ex); |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +The JSON API exception takes three arguments: |
| 104 | + |
| 105 | +- An error object or an array of error objects. |
| 106 | +- The HTTP status code. |
| 107 | +- The previous exception. |
0 commit comments