Skip to content

Commit

Permalink
Display the 404 and 500 custom error pages
Browse files Browse the repository at this point in the history
  • Loading branch information
MKodde committed Jan 24, 2024
1 parent 89b1cf1 commit fdf01ed
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 5 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/packages/framework.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/packages/monolog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
}
}
42 changes: 42 additions & 0 deletions templates/bundles/TwigBundle/Exception/error.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% extends 'base.html.twig' %}

{% block title %}{{ 'Error - ' ~ title }}{% endblock %}

{% block content %}
<p class="error-description">{{ description }}</p>

<table class="table table-bordered">
<tr>
<th>{{ 'stepup.error.timestamp'|trans }}</th>
<td>{{ timestamp }}</td>
</tr>
<tr>
<th>{{ 'stepup.error.hostname'|trans }}</th>
<td>{{ hostname }}</td>
</tr>
<tr>
<th>{{ 'stepup.error.request_id'|trans }}</th>
<td>{{ request_id }}</td>
</tr>
<tr>
<th>{{ 'stepup.error.error_code'|trans }}</th>
<td>{{ error_code }}</td>
</tr>
{% if user_agent %}
<tr>
<th>{{ 'stepup.error.user_agent'|trans }}</th>
<td>{{ user_agent }}</td>
</tr>
{% endif %}
<tr>
<th>{{ 'stepup.error.ip_address'|trans }}</th>
<td>{{ ip_address }}</td>
</tr>
</table>

<p class="error-description">{{ 'stepup.error.support_page.text'|trans({'%support_url%': global_view_parameters.supportUrl })|raw }}</p>

{# 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 %}
15 changes: 15 additions & 0 deletions templates/bundles/TwigBundle/Exception/error404.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'base.html.twig' %}

{% block title %}{{ 'stepup.error.page_not_found.title'|trans }}{% endblock %}

{% block content %}
<p>{{ 'stepup.error.page_not_found.text'|trans }}</p>

<hr>

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

{# 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 %}

0 comments on commit fdf01ed

Please sign in to comment.