Skip to content

Commit

Permalink
Improved 404 handling and fixed editor error
Browse files Browse the repository at this point in the history
404 handling now not a hack-around and uses Laravel 'fallback' routes
instead. Prevents errors with the session when you have mulitple errors
on a page where a post/put/delete is made.
  • Loading branch information
ssddanbrown committed May 19, 2018
1 parent b0d027a commit d3d8ddb
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 152 deletions.
23 changes: 2 additions & 21 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Validation\ValidationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
Expand Down Expand Up @@ -33,6 +31,7 @@ class Handler extends ExceptionHandler
*
* @param \Exception $e
* @return mixed
* @throws Exception
*/
public function report(Exception $e)
{
Expand Down Expand Up @@ -65,30 +64,12 @@ public function render($request, Exception $e)

// Handle 404 errors with a loaded session to enable showing user-specific information
if ($this->isExceptionType($e, NotFoundHttpException::class)) {
return $this->loadErrorMiddleware($request, function ($request) use ($e) {
$message = $e->getMessage() ?: trans('errors.404_page_not_found');
return response()->view('errors/404', ['message' => $message], 404);
});
return \Route::respondWithRoute('fallback');
}

return parent::render($request, $e);
}

/**
* Load the middleware required to show state/session-enabled error pages.
* @param Request $request
* @param $callback
* @return mixed
*/
protected function loadErrorMiddleware(Request $request, $callback)
{
$middleware = (\Route::getMiddlewareGroups()['web_errors']);
return (new Pipeline($this->container))
->send($request)
->through($middleware)
->then($callback);
}

/**
* Check the exception chain to compare against the original exception type.
* @param Exception $e
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,12 @@ public function getRobots()
->view('robots', ['allowRobots' => $allowRobots])
->header('Content-Type', 'text/plain');
}

/**
* Show the route for 404 responses.
*/
public function getNotFound()
{
return response()->view('errors/404', [], 404);
}
}
8 changes: 0 additions & 8 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ class Kernel extends HttpKernel
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\BookStack\Http\Middleware\Localization::class
],
'web_errors' => [
\BookStack\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\BookStack\Http\Middleware\VerifyCsrfToken::class,
\BookStack\Http\Middleware\Localization::class
],
'api' => [
'throttle:60,1',
'bindings',
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "project",
"require": {
"php": ">=7.0.0",
"laravel/framework": "5.5.*",
"laravel/framework": "~5.5.22",
"fideloper/proxy": "~3.3",
"ext-tidy": "*",
"intervention/image": "^2.4",
Expand Down
Loading

0 comments on commit d3d8ddb

Please sign in to comment.