In Laravel 5, you can catch exceptions by editing the render method in app/Exceptions/Handler.php .
If you want to catch exceptions for all AJAX requests, you can do this:
public function render($request, Exception $e) { if ($request->ajax()) { return response()->json(['message' => $e->getMessage()]); } return parent::render($request, $e); }
This will apply to any exception in AJAX requests. If your application App\Exceptions\MyOwnException exception from App\Exceptions\MyOwnException , you check that instance instead.
public function render($request, Exception $e) { if ($e instanceof \App\Exceptions\MyOwnException) { return response()->json(['message' => $e->getMessage()]); } return parent::render($request, $e); }
source share