Custom error page not showing on Laravel 5

I am trying to display a custom error page instead of the default Laravel 5 message:

"Oops ... something seems to have gone wrong."

I did a lot of searching before posting here, I tried this solution, which should work on Laravel 5, but it was out of luck: https://laracasts.com/discuss/channels/laravel/change-whoops-looks-like-something -went-wrong-page

Here is the exact code I have in the app/Exceptions/Handler.php :

 <?php namespace App\Exceptions; use Exception; use View; use Bugsnag\BugsnagLaravel\BugsnagExceptionHandler as ExceptionHandler; class Handler extends ExceptionHandler { protected $dontReport = [ 'Symfony\Component\HttpKernel\Exception\HttpException' ]; public function report(Exception $e) { return parent::report($e); } public function render($request, Exception $e) { return response()->view('errors.defaultError'); } } 

But instead of displaying my custom view, a blank page is displayed. I also tried using this code inside the render() function

 return "Hello, I am an error message"; 

But I get the same result: blank page

+6
source share
6 answers

Instead of answering, create a route for your error page in your Routes.php named "errors.defaultError". eg

 route::get('error', [ 'as' => 'errors.defaultError', 'uses' => ' ErrorController@defaultError ' ]); 

Either create a controller, or include a function in the route using

 return view('errors.defaultError'); 

and use redirection instead. for instance

 public function render($request, Exception $e) { return redirect()->route('errors.defaultError'); } 
+4
source

I totally agree with everyone who wants to tweak the bugs in Laravel so their users never see an embarrassing message such as "Oops, something seems to have gone wrong."

It took me a while to figure this out.

How to set up Oops message in Laravel 5.3

In app/Exceptions/Handler.php replace the entire prepareResponse function with this:

 protected function prepareResponse($request, Exception $e) { if ($this->isHttpException($e)) { return $this->toIlluminateResponse($this->renderHttpException($e), $e); } else { return response()->view("errors.500", ['exception' => $e]); //By overriding this function, I make Laravel display my custom 500 error page instead of the 'Whoops, looks like something went wrong.' message in Symfony\Component\Debug\ExceptionHandler } } 

Basically, it is almost identical to the original functions, but you just change the else block to render the view.

In /resources/views/errors create 500.blade.php .

You can write any text you want there, but I always recommend keeping the error pages very simple (pure HTML and CSS and nothing unusual), so there is almost a zero chance that they themselves can cause additional errors.

To check that it works

In routes/web.php you can add:

 Route::get('error500', function () { throw new \Exception('TEST PAGE. This simulated error exception allows testing of the 500 error page.'); }); 

Then I will go to mysite.com/error500 and see if you see your page with the modified error.

Then also go to mysite.com/some-nonexistent-route and see if you all got the 404 page that you customized, if you have one.

+2
source

In laravel 5.4, you can put this code inside the render function in Handler.php - found in application / exceptions / Handler.php

  //Handle TokenMismatch Error/session('csrf_error') if ($exception instanceof TokenMismatchException) { return response()->view('auth.login', ['message' => 'any custom message'] ); } if ($this->isHttpException($exception)){ if($exception instanceof NotFoundHttpException){ return response()->view("errors.404"); } return $this->renderHttpException($exception); } return response()->view("errors.500"); //return parent::render($request, $exception); 
+1
source

A typical way to do this is to simply create separate views for each type of error .

I need a dynamic user error page (so that all errors fall into the same template).

In Handler.php, I used:

 public function render($request, Exception $e) { // Get error status code. $statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 400; $data = ['customvar'=>'myval']; return response()->view('errors.index', $data, $statusCode); } 

Then I do not need to create 20 error pages for every possible http error code.

0
source

I have two error pages - 404.blade.php and generic.blade.php

I wanted:

  • 404 page to display all missing pages
  • Exception page for development exceptions
  • General Error Page for Manufacturing Exceptions

I am using .env - APP_DEBUG to solve this.

I updated the rendering method in the exception handler:

application / exceptions / handler.php

 public function render($request, Exception $e) { if ($e instanceof ModelNotFoundException) { $e = new NotFoundHttpException($e->getMessage(), $e); } if ($this->isUnauthorizedException($e)) { $e = new HttpException(403, $e->getMessage()); } if ($this->isHttpException($e)) { // Show error for status code, if it exists $status = $e->getStatusCode(); if (view()->exists("errors.{$status}")) { return response()->view("errors.{$status}", ['exception' => $e], $status); } } if (env('APP_DEBUG')) { // In development show exception return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e); } // Otherwise show generic error page return $this->toIlluminateResponse(response()->view("errors.generic"), $e); } 
0
source

on Larvel 5.2 on your app/exceptions/handler.php just extend this renderHttpException method, i.e. add this method to handler.php , configure as you wish

 /** * Render the given HttpException. * * @param \Symfony\Component\HttpKernel\Exception\HttpException $e * @return \Symfony\Component\HttpFoundation\Response */ protected function renderHttpException(HttpException $e) { // to get status code ie 404,503 $status = $e->getStatusCode(); if (view()->exists("errors.{$status}")) { return response()->view("errors.{$status}", ['exception' => $e], $status, $e->getHeaders()); } else { return $this->convertExceptionToResponse($e); } } 
0
source

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


All Articles