Skip to content

Commit

Permalink
Add error handling for HttpExceptions
Browse files Browse the repository at this point in the history
This adds support for all abort($code) errors that can be thrown.

404 will always display the 404 page (either provided by the theme or the default CMS 404 view)

Any other status code will either display the exception itself when debug mode is enabled or the nice error page (provided by the theme /error page or the default CMS error view) when debug mode is disabled.

Fixes #1236
  • Loading branch information
LukeTowers authored Oct 23, 2024
1 parent 963984a commit f5b34cc
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions modules/system/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
use Backend\Models\UserRole;
use BackendAuth;
use BackendMenu;
use Config;
use Cms\Classes\CmsController;
use Cms\Classes\Router;
use Cms\Classes\Theme;
use DateInterval;
use Event;
use Illuminate\Foundation\Vite as LaravelVite;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
use Markdown;
use Request;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use System\Classes\CombineAssets;
use System\Classes\ErrorHandler;
use System\Classes\MailManager;
Expand All @@ -24,11 +30,10 @@
use System\Models\EventLog;
use System\Models\MailSetting;
use System\Twig\Engine as TwigEngine;
use SystemException;
use Twig\Environment;
use Twig\Extension\CoreExtension;
use Validator;
use View;
use Winter\Storm\Exception\SystemException;
use Winter\Storm\Router\Helper as RouterHelper;
use Winter\Storm\Support\ClassLoader;
use Winter\Storm\Support\ModuleServiceProvider;
Expand Down Expand Up @@ -342,6 +347,30 @@ protected function registerConsole()
*/
protected function registerErrorHandler()
{
$this->app->error(function (HttpExceptionInterface $exception, $code, $fromConsole) {
if (class_exists(Theme::class)) {
$theme = Theme::getActiveTheme();
$controller = new CmsController($theme);
if ($code === 404) {
return Response::make($controller->run('/404')->original, 404, []);
}

if (!Config::get('app.debug', false)) {
$router = new Router($theme);
// Use the default view if no "/error" URL is found.
if (!$router->findByUrl('/error')) {
$result = View::make('cms::error');
} else {
// Route to the CMS error page.
$controller = new CmsController($theme);
$result = $controller->run('/error')->original;
}

return Response::make($result, $code, []);
}
}
});

Event::listen('exception.beforeRender', function ($exception, $httpCode, $request) {
$handler = new ErrorHandler;
return $handler->handleException($exception);
Expand Down

2 comments on commit f5b34cc

@LukeTowers
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bennothommo @jaxwilko thinking about this further, this should probably be in the CMS module instead, what do you guys think?

@bennothommo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it would make more sense in there.

Please sign in to comment.