@@ -284,27 +284,18 @@ $errors = $factory
284284
285285## Error Rendering
286286
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:
290289
291290``` 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+ })
308299```
309300
310301The 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
334325our exception parser:
335326
336327``` php
337- $this->renderable(
338- ExceptionParser::make()->acceptsJson()->renderable()
328+ $exceptions->render(
329+ \LaravelJsonApi\Exceptions\ExceptionParser::make()
330+ ->acceptsJson()
331+ ->renderable(),
339332);
340333```
341334
@@ -350,8 +343,10 @@ In this scenario, use the `acceptsMiddleware()` method when registering our
350343exception parser. For example:
351344
352345``` php
353- $this->renderable(
354- ExceptionParser::make()->acceptsMiddleware('api')->renderable()
346+ $exceptions->render(
347+ \LaravelJsonApi\Exceptions\ExceptionParser::make()
348+ ->acceptsMiddleware('api')
349+ ->renderable(),
355350);
356351```
357352
@@ -367,8 +362,10 @@ If you want our exception parser to always convert exceptions to JSON:API
367362errors, use the ` acceptsAll() ` helper method:
368363
369364``` php
370- $this->renderable(
371- ExceptionParser::make()->acceptsAll()->renderable()
365+ $exceptions->render(
366+ \LaravelJsonApi\Exceptions\ExceptionParser::make()
367+ ->acceptsAll()
368+ ->renderable(),
372369);
373370```
374371
@@ -382,9 +379,10 @@ responses, regardless of what `Accept` header the client sent. We would use
382379the request ` is() ` method to check if the path is our API:
383380
384381``` 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(),
388386);
389387```
390388
@@ -442,36 +440,23 @@ We can then add it to the JSON:API exception parser using either the
442440` prepend ` or ` append ` method:
443441
444442``` 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(),
448447);
449448```
450449
451450## Error Reporting
452451
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 :
456455
457456``` 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+ );
475460```
476461
477462This 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,
483468an alternative is to use the helper methods on the ` JsonApiException ` class
484469to determine whether or not the exception should be reported.
485470
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:
491475
492476``` php
493477use LaravelJsonApi\Core\Exceptions\JsonApiException;
494478
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+ );
507483
508- // ...
509- }
484+ $exceptions->render(
485+ \LaravelJsonApi\Exceptions\ExceptionParser::renderer(),
486+ );
487+ })
510488```
511489
512490::: 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.
515495:::
516496
517497In the following example, we log 4xx statuses as debug information, while
518498letting all other JSON: API exceptions propagate to the default logging stack:
519499
520500``` php
521- $this->reportable( function (JsonApiException $ex) {
501+ $exceptions->report(static function (JsonApiException $ex) {
522502 if ($ex->is4xx()) {
523503 logger('JSON:API client exception.', $ex->getErrors()->toArray());
524504 return false;
525505 }
506+
507+ return null;
526508});
527509```
0 commit comments