@@ -11,16 +11,16 @@ how to return your own error responses.
1111
1212## Creating Error Objects
1313
14- Error objects can be constructed from array key/value pairs using the static ` create ` method on
14+ Error objects can be constructed from array key/value pairs using the static ` fromArray ` method on
1515the package's error class. All the keys described in the specification's
1616[ error objects] ( http://jsonapi.org/format/#error-objects ) chapter are supported.
1717
1818For example:
1919
2020``` php
21- use CloudCreativity\LaravelJsonApi\Document\Error;
21+ use CloudCreativity\LaravelJsonApi\Document\Error\Error ;
2222
23- $error = Error::create ([
23+ $error = Error::fromArray ([
2424 'id' => '91053382-7c00-45eb-bdcc-8359d03debbb',
2525 'status' => '500',
2626 'code' => 'unexpected',
@@ -40,15 +40,15 @@ The `JsonApiController` has a responses factory that can create error responses.
4040to return an error response in a controller hook, as demonstrated in the following example:
4141
4242``` php
43- use CloudCreativity\LaravelJsonApi\Document\Error;
43+ use CloudCreativity\LaravelJsonApi\Document\Error\Error ;
4444
4545class PaymentController extends JsonApiController
4646{
4747
4848 protected function creating()
4949 {
5050 if (/** some condition */) {
51- return $this->reply()->errors(Error::create ([
51+ return $this->reply()->errors(Error::fromArray ([
5252 'title' => 'Payment Required',
5353 'detail' => 'Your card has expired.',
5454 'status' => '402',
@@ -84,24 +84,36 @@ It is also possible to throw a `JsonApiException` from anywhere in your code. Th
8484to a JSON API response. For example:
8585
8686``` php
87- use Neomerx\JsonApi\Exceptions\JsonApiException ;
88- use CloudCreativity\LaravelJsonApi\Document\Error ;
87+ use CloudCreativity\LaravelJsonApi\Document\Error\Error ;
88+ use CloudCreativity\LaravelJsonApi\Exceptions\JsonApiException ;
8989
9090try {
9191 dispatchNow(new ChargeCard($token));
9292} catch (\App\PaymentException $ex) {
93- $error = Error::create ([
93+ $error = Error::fromArray ([
9494 'title' => 'Payment Required',
9595 'detail' => $ex->getMessage(),
9696 'status' => '402',
9797 ]);
9898
99- throw new JsonApiException($error, 402, $ex);
99+ throw new JsonApiException($error, $ex);
100100}
101101```
102102
103103The JSON API exception takes three arguments:
104104
105105- An error object or an array of error objects.
106- - The HTTP status code.
107- - The previous exception.
106+ - The previous exception (optional)
107+ - Additional headers for the response (optional).
108+
109+ You can also fluently construct a JSON API exception with headers:
110+
111+ ``` php
112+ use CloudCreativity\LaravelJsonApi\Document\Error\Error;
113+ use CloudCreativity\LaravelJsonApi\Exceptions\JsonApiException;
114+
115+ throw JsonApiException::make(Error::fromArray([
116+ 'status' => '418',
117+ 'title' => "I'm a Teapot"
118+ ]))->withHeaders(['X-Foo' => 'Bar']);
119+ ```
0 commit comments