1
votes

I am using latest version laravel(5.6)

Now in my code whenever an exception occurs laravel treating it as a fatal error, stop executing instantly and displaying the error message in some template.

But I don't want that, I want to handle exceptions and display some custom error messages

I found some ways like

  1. changing the APP_DEBUG value in the .env file to false. But this also displays another page with the message "whoops!some this want wrong";

  2. In Handler.php which is in app/Exceptions, I had put some exceptions in not report zone. But the app is still reporting them

3
The docs are a good place to start: laravel.com/docs/5.6/errors#custom-http-error-pagesDevon

3 Answers

0
votes

Custom HTTP Error Pages

Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a resources/views/errors/404.blade.php. This file will be served on all 404 errors generated by your application. The views within this directory should be named to match the HTTP status code they correspond to. The HttpException instance raised by the abort function will be passed to the view as an $exception variable.

https://laravel.com/docs/5.6/errors#custom-http-error-pages

0
votes

Really you want to be handling your exceptions. Wrap the code in a try catch and you can do all manner of things (e.g. email / slack / log). Once you have handled the exception you can still use custom http error pages inside the catch so the end user get's a friendly message on a nicely designed page. There is even a report helper built in to allow you to externally log and continue on processing the code.

@Devon's above answer re: Custom HTTP Error Pages gets you exactly what you want also.

0
votes

Please note few important points :

  1. The App\Exceptions\Handler class is where all exceptions triggered by your application are logged and then rendered back to the user. This class has two method report() and render(), both has their own responsibility.
  2. The report method is used to log exceptions. By default, the report method passes the exception to the base class where the exception is logged. However, you are free to log exceptions however you wish. For example, if you need to report different types of exceptions in different ways, you may use the PHP instanceof comparison operator
  3. The render method is responsible for converting a given exception into an HTTP response that should be sent back to the browser. By default, the exception is passed to the base class which generates a response for you. However, you are free to check the exception type or return your own custom response.

As in your case you want to return custom message for exception, inside render() you may use the PHP instanceof comparison operator and return you own logic.

Example :

if($exception instanceof  PostTooLargeException || $exception instanceof  FileException){
     return response()->json([
        'error' => true,
        'error_message' => "The file you are trying to upload exceeds the maximum limit. Please try to upload a smaller file."
     ],200); 
}

Go through https://laravel.com/docs/5.6/errors for more datails