php - how to show 500 internal Server error page in laravel 5.2?

Solution:
You need to create handler to catchingFatalErrorExceptions
in your handler like below code:
Handler
Inapp/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
Seeresources/views/errors/500.blade.php
. If not exist then create it.
You can get more detailed OR other ways from Laravel 5 custom error view for 500
Answer
Solution:
In yourresources/views/errors
folder create a file named500.blade.php
.
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 500 HTTP status codes, create a
resources/views/errors/500.blade.php
. This file will be served on all 500 errors generated by your application.
The problem is that Laravel will only do this automatic rendering of error pages for exceptions that are instances ofHttpException
. Unfortunately when your server throws an error (method does not exist, variable undefined, etc) it actually throws aFatalErrorException
. As such, it is uncaught, and trickles down to theSymfonyDisplayer()
which either gives you the trace (debug true) or ugly one-liner 'Whoops, looks like something went wrong' (debug false).
To solve this you have add this to yourrender
method toapp/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);
}
Answer
Solution:
My solution is simple, just replace your render() method in Exceptions\Handler.php file with:
/**
* 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 page if app in production environment. You will need to have 500.blade.php view in your resources/views/errors folder.
Answer
Solution:
// 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 do this automatic rendering of error pages for exceptions that are instances of HttpException.
So my solution is to replace whatever exception that is not HttpException by a HttpException.
Answer
Solution:
in app\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 one in the parent class (Illuminate\Foundation\Exceptions\Handler) that displays the whoops page.
Answer
Solution:
In Laravel 5.4, you could overrideprepareException
function in yourapp\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;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: invalid argument supplied for foreach() laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.