Skip to content

Instantly share code, notes, and snippets.

@lucagrandicelli
Last active March 10, 2021 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucagrandicelli/0570fa8d6292c171f4e3c2796d2bb7bd to your computer and use it in GitHub Desktop.
Save lucagrandicelli/0570fa8d6292c171f4e3c2796d2bb7bd to your computer and use it in GitHub Desktop.
Laravel 8 API/Web Exception Handler
<?php
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {});
// reportable
$this->renderable(function (NotFoundHttpException $e, Request $request) {
if ($request->wantsJson()) {
return response(['errorjson' => $e->getMessage()], $e->getCode() ?: 404);
} else {
return response(['errorweb' => $e->getMessage()], $e->getCode() ?: 404);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment