From fdf01ed561875c4bba9f7d7a091fe86e56166cb1 Mon Sep 17 00:00:00 2001 From: Michiel Kodde Date: Wed, 24 Jan 2024 08:29:14 +0100 Subject: [PATCH] Display the 404 and 500 custom error pages --- composer.lock | 10 ++--- config/packages/framework.yaml | 1 + config/packages/monolog.yaml | 2 + .../Controller/ExceptionController.php | 37 ++++++++++++++++ .../TwigBundle/Exception/error.html.twig | 42 +++++++++++++++++++ .../TwigBundle/Exception/error404.html.twig | 15 +++++++ 6 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 templates/bundles/TwigBundle/Exception/error.html.twig create mode 100644 templates/bundles/TwigBundle/Exception/error404.html.twig diff --git a/composer.lock b/composer.lock index 8de6a3d77..7977bec0d 100644 --- a/composer.lock +++ b/composer.lock @@ -3082,12 +3082,12 @@ "source": { "type": "git", "url": "https://github.com/OpenConext/Stepup-bundle.git", - "reference": "1d33328810c7c17ecf574a233cd0239acefd589c" + "reference": "f42f7de4fe444a713fbe3a74ff019a29a06c5adb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/OpenConext/Stepup-bundle/zipball/1d33328810c7c17ecf574a233cd0239acefd589c", - "reference": "1d33328810c7c17ecf574a233cd0239acefd589c", + "url": "https://api.github.com/repos/OpenConext/Stepup-bundle/zipball/f42f7de4fe444a713fbe3a74ff019a29a06c5adb", + "reference": "f42f7de4fe444a713fbe3a74ff019a29a06c5adb", "shasum": "" }, "require": { @@ -3179,10 +3179,10 @@ "surf secure id" ], "support": { - "source": "https://github.com/OpenConext/Stepup-bundle/tree/6.0.10", + "source": "https://github.com/OpenConext/Stepup-bundle/tree/6.0.11", "issues": "https://github.com/OpenConext/Stepup-bundle/issues" }, - "time": "2024-01-09T10:11:10+00:00" + "time": "2024-01-23T17:33:14+00:00" }, { "name": "surfnet/stepup-middleware-client-bundle", diff --git a/config/packages/framework.yaml b/config/packages/framework.yaml index 1e65c1238..e62cefa2d 100644 --- a/config/packages/framework.yaml +++ b/config/packages/framework.yaml @@ -19,6 +19,7 @@ framework: cookie_samesite: lax fragments: false + error_controller: Surfnet\StepupSelfService\SelfServiceBundle\Controller\ExceptionController::show http_method_override: true php_errors: log: true diff --git a/config/packages/monolog.yaml b/config/packages/monolog.yaml index 1fd3a0b6f..a2b792913 100644 --- a/config/packages/monolog.yaml +++ b/config/packages/monolog.yaml @@ -7,6 +7,7 @@ monolog: action_level: ERROR passthru_level: NOTICE # this means that all message of level NOTICE or higher are always logged handler: main_stdout + channels: [ "!event" ] bubble: false # if we handle it, nothing else should main_stdout: type: stream @@ -20,6 +21,7 @@ when@dev: prod-signaler: action_level: ERROR passthru_level: DEBUG # DEV setting: this means that all message of level DEBUG or higher are always logged + channels: [ "!event" ] bubble: true main_logfile: type: stream diff --git a/src/Surfnet/StepupSelfService/SelfServiceBundle/Controller/ExceptionController.php b/src/Surfnet/StepupSelfService/SelfServiceBundle/Controller/ExceptionController.php index 9bf9a2896..30ae4070b 100644 --- a/src/Surfnet/StepupSelfService/SelfServiceBundle/Controller/ExceptionController.php +++ b/src/Surfnet/StepupSelfService/SelfServiceBundle/Controller/ExceptionController.php @@ -20,6 +20,11 @@ namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller; +use DateTime; +use DateTimeInterface; +use Surfnet\StepupBundle\Exception\Art; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Throwable; use Surfnet\StepupBundle\Controller\ExceptionController as BaseExceptionController; use Surfnet\StepupSelfService\SelfServiceBundle\Exception\MissingRequiredAttributeException; @@ -48,4 +53,36 @@ protected function getPageTitleAndDescription(Throwable $exception): array return parent::getPageTitleAndDescription($exception); } + + public function show(Request $request, Throwable $exception): Response + { + $statusCode = $this->getStatusCode($exception); + + $template = '@default/bundles/TwigBundle/Exception/error.html.twig'; + if ($statusCode == 404) { + $template = '@default/bundles/TwigBundle/Exception/error404.html.twig'; + } + + $response = new Response('', $statusCode); + + $timestamp = (new DateTime)->format(DateTimeInterface::ATOM); + $hostname = $request->getHost(); + $requestId = $this->requestId; + $errorCode = Art::forException($exception); + $userAgent = $request->headers->get('User-Agent'); + $ipAddress = $request->getClientIp(); + + return $this->render( + $template, + [ + 'timestamp' => $timestamp, + 'hostname' => $hostname, + 'request_id' => $requestId->get(), + 'error_code' => $errorCode, + 'user_agent' => $userAgent, + 'ip_address' => $ipAddress, + ] + $this->getPageTitleAndDescription($exception), + $response + ); + } } diff --git a/templates/bundles/TwigBundle/Exception/error.html.twig b/templates/bundles/TwigBundle/Exception/error.html.twig new file mode 100644 index 000000000..7a25dcb5a --- /dev/null +++ b/templates/bundles/TwigBundle/Exception/error.html.twig @@ -0,0 +1,42 @@ +{% extends 'base.html.twig' %} + +{% block title %}{{ 'Error - ' ~ title }}{% endblock %} + +{% block content %} +

{{ description }}

+ + + + + + + + + + + + + + + + + + + {% if user_agent %} + + + + + {% endif %} + + + + +
{{ 'stepup.error.timestamp'|trans }}{{ timestamp }}
{{ 'stepup.error.hostname'|trans }}{{ hostname }}
{{ 'stepup.error.request_id'|trans }}{{ request_id }}
{{ 'stepup.error.error_code'|trans }}{{ error_code }}
{{ 'stepup.error.user_agent'|trans }}{{ user_agent }}
{{ 'stepup.error.ip_address'|trans }}{{ ip_address }}
+ +

{{ 'stepup.error.support_page.text'|trans({'%support_url%': global_view_parameters.supportUrl })|raw }}

+ + {# For now the back button is not shown on the error page until a more intelligent solution is thought of #} + {% block back_button %}{% endblock %} + +{% endblock %} diff --git a/templates/bundles/TwigBundle/Exception/error404.html.twig b/templates/bundles/TwigBundle/Exception/error404.html.twig new file mode 100644 index 000000000..185692a65 --- /dev/null +++ b/templates/bundles/TwigBundle/Exception/error404.html.twig @@ -0,0 +1,15 @@ +{% extends 'base.html.twig' %} + +{% block title %}{{ 'stepup.error.page_not_found.title'|trans }}{% endblock %} + +{% block content %} +

{{ 'stepup.error.page_not_found.text'|trans }}

+ +
+ +

{{ 'stepup.error.support_page.text'|trans({'%support_url%': global_view_parameters.supportUrl })|raw }}

+ + {# For now the back button is not shown on the error page until a more intelligent solution is thought of #} + {% block back_button %}{% endblock %} + +{% endblock %}