Laravel ajax catch 500 Internal Server Error

I use Laravel with ajax, and in my controller I have a try-catch block:

try { something... } catch (\Exception $e) { return response()->json([ 'status' => 'error', 'message'=> 'error message' ]); } 

I use this to show an error message in a div on the page.

This works, but not if the error is an internal server 500 error (example: tokenmismatchexception).

Then the error does not fall into the catch block, and, in fact, the user is not notified of any error (except for the console).

Is there a way I can catch such errors and display the error in the same div that I usually do?

+5
source share
1 answer

This is because you may not have included the error section. Create jQuery Ajax code, for example:

  $.ajax({ url : '/yoururl', method : 'POST', data : { name:"test" }, success : function(response) { //USE THIS SECTION WHEN ITS SUCCESS }, error : function(e) { //SHOW ERROR TO USER HERE THIS SECTION IS CALLED IN CASE OF ERRORS TO SEE THE OBJECT OF the error use console.log(e); } }); 
+4
source

Source: https://habr.com/ru/post/1264935/


All Articles