How to get a raw exception message without HTML in Laravel?

I am making ajax requests for the Laravel backend.

In the backend, I check the request data and throw some exceptions. Laravel, by default, generates html pages with exception messages.

I want to respond only to the original exception message, not the html.

->getMessage() does not work. Laravel, as always, generates html.

What am I doing?

+5
source share
1 answer

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); } 
+8
source

All Articles