BUGFIX: added error pages

This commit is contained in:
Jeroen De Meerleer 2021-08-06 12:30:00 +02:00
parent 9c7be573e0
commit 9e06a69f28
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 45 additions and 17 deletions

View File

@ -1,3 +1,9 @@
error:
path: '/error/{status}'
defaults:
_controller: JeroenED\Website\Controller\DefaultController::ErrorAction
status: '404'
default: default:
path: '/{page}' path: '/{page}'
defaults: defaults:

View File

@ -6,8 +6,10 @@ namespace JeroenED\Framework;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
use Symfony\Component\Filesystem\Exception\InvalidArgumentException; use Symfony\Component\Filesystem\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Generator\UrlGenerator; use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Loader\YamlFileLoader; use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\Matcher\UrlMatcher;
@ -21,6 +23,9 @@ class Router
public function route(Request $request, Kernel $kernel): Response public function route(Request $request, Kernel $kernel): Response
{ {
try {
// Here be dragons
$requestContext = new RequestContext(); $requestContext = new RequestContext();
$this->requestContext = $requestContext->fromRequest($request); $this->requestContext = $requestContext->fromRequest($request);
$matcher = new UrlMatcher($this->routes, $this->requestContext); $matcher = new UrlMatcher($this->routes, $this->requestContext);
@ -30,6 +35,7 @@ class Router
$action = $controller[1]; $action = $controller[1];
unset($method['_controller']); unset($method['_controller']);
unset($method['_route']); unset($method['_route']);
$response = $controllerObj->$action(...$method); $response = $controllerObj->$action(...$method);
if ($response instanceof Response) { if ($response instanceof Response) {
@ -37,6 +43,11 @@ class Router
} else { } else {
throw new InvalidArgumentException(); throw new InvalidArgumentException();
} }
} catch(ResourceNotFoundException $e) {
return new RedirectResponse($this->getUrlForRoute('error', ['status' => '404']));
} catch (\Throwable $e) {
return new RedirectResponse($this->getUrlForRoute('error', ['status' => (method_exists($e,'getStatusCode')) ? $e->getStatusCode() : '500']));
}
} }
public function parseRoutes(string $dir, string $file): void public function parseRoutes(string $dir, string $file): void

View File

@ -22,4 +22,16 @@ class DefaultController extends Controller
'nineties' => (isset($_COOKIE['nineties'])) 'nineties' => (isset($_COOKIE['nineties']))
], $page['status']); ], $page['status']);
} }
public function ErrorAction($status)
{
$pageRepo = new Page();
$page = $pageRepo->getPage('error/' . $status);
return $this->render('/page.html.twig', [
'header' => $page['header'],
'content' => $page['content'],
'title' => $page['title'],
'nineties' => (isset($_COOKIE['nineties']))
], $status);
}
} }

View File

@ -9,6 +9,7 @@ use GuzzleHttp\Client;
use JeroenED\Framework\Repository; use JeroenED\Framework\Repository;
use phpseclib3\Crypt\PublicKeyLoader; use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Net\SSH2; use phpseclib3\Net\SSH2;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Page class Page
{ {
@ -28,9 +29,7 @@ class Page
$return['content'] = file_get_contents(strtolower($this->root . '/' . $page . '.md')); $return['content'] = file_get_contents(strtolower($this->root . '/' . $page . '.md'));
$return['status'] = '200'; $return['status'] = '200';
} else { } else {
$return['title'] = $titles['404'] ?? ''; throw new NotFoundHttpException();
$return['content'] = file_get_contents(strtolower($this->root . '/404.md'));
$return['status'] = '404';
} }
return $return; return $return;
} }