@@ -284,27 +284,18 @@ $errors = $factory
284
284
285
285
## Error Rendering
286
286
287
- As described in the [ installation instructions] ( ../getting-started/#exception-handler ) ,
288
- the following should have been added to the ` register ` method on your
289
- application's exception handler:
287
+ As described in the [ installation instructions] ( ../getting-started/#exception-handler ) , the following should have been
288
+ added to the ` withExceptions() ` call in your ` bootstrap/app.php ` file:
290
289
291
290
``` php
292
- class Handler extends ExceptionHandler
293
- {
294
- // ...
295
-
296
- /**
297
- * Register the exception handling callbacks for the application.
298
- *
299
- * @return void
300
- */
301
- public function register()
302
- {
303
- $this->renderable(
304
- \LaravelJsonApi\Exceptions\ExceptionParser::make()->renderable()
305
- );
306
- }
307
- }
291
+ ->withExceptions(function (Exceptions $exceptions) {
292
+ $exceptions->dontReport(
293
+ \LaravelJsonApi\Core\Exceptions\JsonApiException::class,
294
+ );
295
+ $exceptions->render(
296
+ \LaravelJsonApi\Exceptions\ExceptionParser::renderer(),
297
+ );
298
+ })
308
299
```
309
300
310
301
The Laravel exception handler already takes care of converting exceptions to
@@ -334,8 +325,10 @@ of the default Laravel response, use the `acceptsJson()` method when registering
334
325
our exception parser:
335
326
336
327
``` php
337
- $this->renderable(
338
- ExceptionParser::make()->acceptsJson()->renderable()
328
+ $exceptions->render(
329
+ \LaravelJsonApi\Exceptions\ExceptionParser::make()
330
+ ->acceptsJson()
331
+ ->renderable(),
339
332
);
340
333
```
341
334
@@ -350,8 +343,10 @@ In this scenario, use the `acceptsMiddleware()` method when registering our
350
343
exception parser. For example:
351
344
352
345
``` php
353
- $this->renderable(
354
- ExceptionParser::make()->acceptsMiddleware('api')->renderable()
346
+ $exceptions->render(
347
+ \LaravelJsonApi\Exceptions\ExceptionParser::make()
348
+ ->acceptsMiddleware('api')
349
+ ->renderable(),
355
350
);
356
351
```
357
352
@@ -367,8 +362,10 @@ If you want our exception parser to always convert exceptions to JSON:API
367
362
errors, use the ` acceptsAll() ` helper method:
368
363
369
364
``` php
370
- $this->renderable(
371
- ExceptionParser::make()->acceptsAll()->renderable()
365
+ $exceptions->render(
366
+ \LaravelJsonApi\Exceptions\ExceptionParser::make()
367
+ ->acceptsAll()
368
+ ->renderable(),
372
369
);
373
370
```
374
371
@@ -382,9 +379,10 @@ responses, regardless of what `Accept` header the client sent. We would use
382
379
the request ` is() ` method to check if the path is our API:
383
380
384
381
``` php
385
- $this->renderable(ExceptionParser::make()
386
- ->accept(fn(\Throwable $ex, $request) => $request->is('api/*'))
387
- ->renderable()
382
+ $exceptions->render(
383
+ \LaravelJsonApi\Exceptions\ExceptionParser::make()
384
+ ->accept(static fn(\Throwable $ex, $request) => $request->is('api/*'))
385
+ ->renderable(),
388
386
);
389
387
```
390
388
@@ -442,36 +440,23 @@ We can then add it to the JSON:API exception parser using either the
442
440
` prepend ` or ` append ` method:
443
441
444
442
``` php
445
- $this->renderable(ExceptionParser::make()
446
- ->append(\App\JsonApi\Exceptions\PaymentFailedHandler::class)
447
- ->renderable()
443
+ $exceptions->render(
444
+ \LaravelJsonApi\Exceptions\ExceptionParser::make()
445
+ ->append(\App\JsonApi\Exceptions\PaymentFailedHandler::class)
446
+ ->renderable(),
448
447
);
449
448
```
450
449
451
450
## Error Reporting
452
451
453
- As described in the [ installation instructions] ( ../getting-started/#exception-handler ) ,
454
- the following should have been added to the ` $ dontReport` property on your
455
- application's exception handler :
452
+ As described in the [ installation instructions] ( ../getting-started/#exception-handler ) , when configuring your
453
+ application's exception handler in the ` bootstrap/app.php ` file, you should have called ` dontReport() ` with the JSON : API
454
+ exception class :
456
455
457
456
``` php
458
- use LaravelJsonApi\Core\Exceptions\JsonApiException;
459
-
460
- class Handler extends ExceptionHandler
461
- {
462
- // ...
463
-
464
- /**
465
- * A list of the exception types that should not be reported.
466
- *
467
- * @var array
468
- */
469
- protected $dontReport = [
470
- JsonApiException::class,
471
- ];
472
-
473
- // ...
474
- }
457
+ $exceptions->dontReport(
458
+ \LaravelJsonApi\Core\Exceptions\JsonApiException::class,
459
+ );
475
460
```
476
461
477
462
This prevents our ` JsonApiException ` from being reported in your application's
@@ -483,45 +468,42 @@ code (server-side error) will not be reported in your error log. Therefore,
483
468
an alternative is to use the helper methods on the ` JsonApiException ` class
484
469
to determine whether or not the exception should be reported.
485
470
486
- To do this, we will use the ` reportable() ` method to register a callback
487
- for the JSON: API exception class. (At the same time, we remove the exception
488
- class from the ` $dontReport ` property.) For example, the following will stop
489
- the propagation of JSON: API exceptions to the default logging stack if the
490
- exception does not have a 5xx status:
471
+ To do this, we will use the ` report() ` method when configuring our application exception handler in the
472
+ ` bootstrap/app.php ` file to register a callback for the JSON: API exception class. (At the same time, we remove
473
+ ` dontReport() ` call.) For example, the following will stop the propagation of JSON: API exceptions to the default logging
474
+ stack if the exception does not have a 5xx status:
491
475
492
476
``` php
493
477
use LaravelJsonApi\Core\Exceptions\JsonApiException;
494
478
495
- /**
496
- * Register the exception handling callbacks for the application.
497
- *
498
- * @return void
499
- */
500
- public function register()
501
- {
502
- $this->reportable(function (JsonApiException $ex) {
503
- if (!$ex->is5xx()) {
504
- return false;
505
- }
506
- });
479
+ ->withExceptions(function (Exceptions $exceptions) {
480
+ $exceptions->report(
481
+ static fn(JsonApiException $ex) => !$ex->is5xx() ? false : null,
482
+ );
507
483
508
- // ...
509
- }
484
+ $exceptions->render(
485
+ \LaravelJsonApi\Exceptions\ExceptionParser::renderer(),
486
+ );
487
+ })
510
488
```
511
489
512
490
::: tip
513
- As described in the [ Laravel documentation on reporting exceptions] ( https://laravel.com/docs/8.x/errors#reporting-exceptions ) , returning ` false ` from the reportable callback prevents the
514
- exception from propagating to the default logging stack.
491
+ As described in
492
+ the [ Laravel documentation on reporting exceptions] ( https://laravel.com/docs/8.x/errors#reporting-exceptions ) , returning
493
+ ` false ` from the reportable callback prevents the exception from propagating to the default logging stack. Returning
494
+ ` null ` indicates the Laravel should determine whether to report the exception.
515
495
:::
516
496
517
497
In the following example, we log 4xx statuses as debug information, while
518
498
letting all other JSON: API exceptions propagate to the default logging stack:
519
499
520
500
``` php
521
- $this->reportable( function (JsonApiException $ex) {
501
+ $exceptions->report(static function (JsonApiException $ex) {
522
502
if ($ex->is4xx()) {
523
503
logger('JSON:API client exception.', $ex->getErrors()->toArray());
524
504
return false;
525
505
}
506
+
507
+ return null;
526
508
});
527
509
```
0 commit comments