如何捕获错误并打印给用户

I'm trying to print the errors that usually come in the gray screen , but capturing them in a html and displaying the user to use the system. For this I am using the render method and a view on the resources / views / errors / error.php folder and I'm doing the following:

public function render($request, Exception $e)
 {
   return response()->view('errors.error', [], 500);
 }

This effectively is showing me what I have in error.php html file but want to show the user the actual error captured.

As I do ? Help !!!

First of all, you have to think if you really want to show the user the error detail. (Probably not). In fact, the detail you see in the "gray screen" varies according to the value of APP_DEBUG in your .env file.

Typically you want to hide from users the reason behind the failing, and display a very generic error message. For HTTP exceptions, the exception handler will do it for you: just create a blade file for each http code you want to handle (resources/views/errors/404.blade.php for instance).

If you want this for debugging purposes, there are better tools. Checking the error log is the most basic one. Or use the dd() function here and there to look at the state of some variable at any point.

If you really really want to do this, you can modify the app/Exceptions/Handler.phpfile, render method as you suggest

public function render($request, Exception $e)
{
   return response()->view('errors.error', [
       'error_message' => $e->getMessage()
   ], $code);
}