Add a custom page with 500 production-only errors in Laravel

I want to create a custom page with 500 errors. This can be done simply by creating a view in errors/500.blade.php .

This is normal for production mode, but I no longer get the default exception / debug page in debug mode (the one that looks gray and says “Something went wrong”).

So my question is: how can I create a 500 error page for production, but is the original page with 500 errors in debug mode correct?

+5
source share
4 answers

I found a better way to solve my problem: add the following function to App\Exceptions\Handler.php

 protected function renderHttpException(HttpException $e) { if ($e->getStatusCode() === 500 && env('APP_DEBUG') === true) { // Display Laravel default error message with appropriate error information return $this->convertExceptionToResponse($e); } return parent::renderHttpException($e); // Continue as normal } 

Best solutions are welcome!

+2
source

Just add this code to \ App \ Exceptinons \ Handler.php:

 public function render($request, Exception $exception) { // Render well-known exceptions here // Otherwise display internal error message if(!env('APP_DEBUG', false)){ return view('errors.500'); } else { return parent::render($request, $exception); } } 

or

 public function render($request, Exception $exception) { // Render well-known exceptions here // Otherwise display internal error message if(app()->environment() === 'production') { return view('errors.500'); } else { return parent::render($request, $exception); } } 
+1
source

Add the code to the app/Exceptions/Handler.php inside the Handler class:

 protected function convertExceptionToResponse(Exception $e) { if (config('app.debug')) { return parent::convertExceptionToResponse($e); } return response()->view('errors.500', [ 'exception' => $e ], 500); } 

The convertExceptionToResponse method gets the correct errors that cause state 500.

0
source

Add this code to the app/Exceptions/Handler.php , render method. I think it is clean and simple. Assuming you have a 500 error page.

 public function render($request, Exception $e) { if ($this->isHttpException($e)) { return $this->toIlluminateResponse($this->renderHttpException($e), $e); } elseif (!config('app.debug')) { return response()->view('errors.500', [], 500); } else { // return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e); return response()->view('errors.500', [], 500); } } 

use a commented line if you need a default whoops error page for debugging. use another for custom 500 error pages.

0
source

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


All Articles