How to show 500 internal pages of server error in laravel 5.2?

I want to display the Internal Server Error page. when the user had a syntax error in the project, can anyone help me? if I am wrong in the syntax, I want to show this particular blade.

+5
source share
6 answers

You need to create a handler to catch FatalErrorExceptions in your handler, as shown below:

Handler In app/Exceptions/Handler.php

 public function render($request, Exception $e) { // 404 page when a model is not found if ($e instanceof ModelNotFoundException) { return response()->view('errors.404', [], 404); } // custom error message if ($e instanceof \ErrorException) { return response()->view('errors.500', [], 500); } else { return parent::render($request, $e); } return parent::render($request, $e); } 

View See resources/views/errors/500.blade.php . If this does not exist, create it.

You can get more detailed or other ways from Laravel 5 custom error view for 500

+8
source

In your resources/views/errors folder, create a file called 500.blade.php .

Laravel makes it easy to display custom error pages for various HTTP protocol status codes. For example, if you want to set up an error page for 500 HTTP status codes, create resources/views/errors/500.blade.php . This file will be serviced on all 500 errors created by your application.

The problem is that Laravel will only automatically render error pages for exceptions that are HttpException instances. Unfortunately, when your server throws an error (the method does not exist, the variable is undefined, etc.), it actually throws a FatalErrorException . This way it doesn't open and flows to SymfonyDisplayer() , which either gives you a trace (debug true) or the ugly single-line "Whoops, something seems to go wrong" (debug false).

To solve this problem, add this to your render method in app/Exceptions/Handler

 # /app/Exceptions/Handler.php # use Symfony\Component\Debug\Exception\FlattenException; # public function render($request, Exception $e) $exception = FlattenException::create($e); $statusCode = $exception->getStatusCode($exception); if ($statusCode === 404 or $statusCode === 500) { return response()->view('errors.' . $statusCode, [], $statusCode); } 

Docs

+3
source

My solution is simple, just replace the render () method in the Exceptions \ Handler.php file as follows:

 /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { if ($request->expectsJson()) { return $this->renderJson($request, $exception); } if ($this->shouldReport($exception) && app()->environment('production')) { $exception = new HttpException(500, $exception->getMessage(), $exception); } return parent::render($request, $exception); } 

It will show 500 pages if the application is in production. You will need to have the form 500.blade.php in the resources / views / errors folder.

+1
source
  // app/Exceptions/Handler.php protected function prepareResponse($request, Exception $e) { if($this->isHttpException($e) === false && config('app.debug') === false) { $e = new HttpException(500); } return parent::prepareResponse($request, $e); } 

Like @Amit said

The problem is that Laravel will only perform this automatic rendering of the error page for exceptions that are instances of HttpException.

So my solution is to replace any exception that is not an HttpException with an HttpException.

0
source

in the application \ Exceptions \ Handler, create the following method:

 protected function convertExceptionToResponse(Exception $e) { $e = FlattenException::create($e); return response()->view('errors.500', ['exception' => $e], $e->getStatusCode(), $e->getHeaders()); } 

it will override the value in the parent class (Illuminate \ Foundation \ Exceptions \ Handler), which displays the whoops page.

0
source

In Laravel 5.4, you can override the prepareException function in app\Exception\Handler.php :

 /** * @inheridoc */ protected function prepareException(Exception $e) { $exception = parent::prepareException($e); if(!config('app.debug')) { if(!$exception instanceof HttpException && $this->shouldReport($exception)) { $exception = new HttpException(500); } } return $exception; } 
0
source

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


All Articles