diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index 3fd2313ce..618dc111f 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -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; @@ -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; @@ -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);