Thursday, March 2, 2017

Laravel ajax catch 500 Internal server error

I am using 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 am using this to show the error message in a div on the page.

This is working, but not if the error is a 500 - internal server error (example: tokenmismatchexception).

Then the error is not caught by the catch-block, and essentially the user is not notified of any error (other than in the console).

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




via Bergkamp10

Advertisement