]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #2322 from wallabag/v2-404
authorNicolas LÅ“uillet <nicolas@loeuillet.org>
Sat, 1 Oct 2016 14:35:48 +0000 (16:35 +0200)
committerGitHub <noreply@github.com>
Sat, 1 Oct 2016 14:35:48 +0000 (16:35 +0200)
Customize errors templates

app/config/config.yml
src/Wallabag/CoreBundle/Controller/ExceptionController.php [new file with mode: 0644]
src/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverter.php
src/Wallabag/CoreBundle/Resources/config/services.yml
src/Wallabag/CoreBundle/Resources/views/themes/baggy/Exception/error.html.twig [new file with mode: 0644]
src/Wallabag/CoreBundle/Resources/views/themes/material/Exception/error.html.twig [new file with mode: 0644]
tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php

index b5d82ed9a0e183793f6c6074427f3c4d12fd5bf9..fbebfee7f1f2420f53e7568c51d237f9c931be01 100644 (file)
@@ -64,6 +64,7 @@ twig:
     strict_variables: "%kernel.debug%"
     form_themes:
         - "LexikFormFilterBundle:Form:form_div_layout.html.twig"
+    exception_controller: wallabag_core.exception_controller:showAction
 
 # Doctrine Configuration
 doctrine:
diff --git a/src/Wallabag/CoreBundle/Controller/ExceptionController.php b/src/Wallabag/CoreBundle/Controller/ExceptionController.php
new file mode 100644 (file)
index 0000000..abfa9c2
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+namespace Wallabag\CoreBundle\Controller;
+
+use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
+use Symfony\Component\HttpFoundation\Request;
+
+/**
+ * This controller allow us to customize the error template.
+ * The only modified line from the parent template is for "WallabagCoreBundle".
+ */
+class ExceptionController extends BaseExceptionController
+{
+    protected function findTemplate(Request $request, $format, $code, $showException)
+    {
+        $name = $showException ? 'exception' : 'error';
+        if ($showException && 'html' == $format) {
+            $name = 'exception_full';
+        }
+
+        // For error pages, try to find a template for the specific HTTP status code and format
+        if (!$showException) {
+            $template = sprintf('WallabagCoreBundle:Exception:%s.%s.twig', $name, $format);
+            if ($this->templateExists($template)) {
+                return $template;
+            }
+        }
+
+        // try to find a template for the given format
+        $template = sprintf('@Twig/Exception/%s.%s.twig', $name, $format);
+        if ($this->templateExists($template)) {
+            return $template;
+        }
+
+        // default to a generic HTML exception
+        $request->setRequestFormat('html');
+
+        return sprintf('@Twig/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
+    }
+}
index 6ea2a4f3af29edbb1872291e61d4ac7fa684959f..40b5673ddf62310d61b9484a148b71bf1bb55281 100644 (file)
@@ -49,7 +49,7 @@ class UsernameRssTokenConverter implements ParamConverterInterface
         $em = $this->registry->getManagerForClass($configuration->getClass());
 
         // Check, if class name is what we need
-        if ('Wallabag\UserBundle\Entity\User' !== $em->getClassMetadata($configuration->getClass())->getName()) {
+        if (null !== $em && 'Wallabag\UserBundle\Entity\User' !== $em->getClassMetadata($configuration->getClass())->getName()) {
             return false;
         }
 
@@ -69,9 +69,8 @@ class UsernameRssTokenConverter implements ParamConverterInterface
         $username = $request->attributes->get('username');
         $rssToken = $request->attributes->get('token');
 
-        // Check, if route attributes exists
-        if (null === $username || null === $rssToken) {
-            throw new \InvalidArgumentException('Route attribute is missing');
+        if (!$request->attributes->has('username') || !$request->attributes->has('token')) {
+            return false;
         }
 
         // Get actual entity manager for class
index 5a727ec530c57c8dcb3f6ddbae3e9c461006d830..d11398469757c4a60957a2a37c98adeca5b0dfd6 100644 (file)
@@ -122,3 +122,9 @@ services:
                 host: '%redis_host%'
                 port: '%redis_port%'
                 schema: tcp
+
+    wallabag_core.exception_controller:
+        class: Wallabag\CoreBundle\Controller\ExceptionController
+        arguments:
+            - '@twig'
+            - '%kernel.debug%'
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Exception/error.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Exception/error.html.twig
new file mode 100644 (file)
index 0000000..b52634f
--- /dev/null
@@ -0,0 +1,24 @@
+{% extends "WallabagCoreBundle::layout.html.twig" %}
+
+{% block title %}{{ 'error.page_title'|trans }}{% endblock %}
+
+{% block body_class %}login{% endblock %}
+
+{% block menu %}{% endblock %}
+{% block messages %}{% endblock %}
+{% block header %}{% endblock %}
+
+{% block content %}
+<main class="valign-wrapper">
+    <div class="valign row">
+        <div class="card sw">
+            <div class="center"><img src="{{ asset('bundles/wallabagcore/themes/_global/img/logo-w.png') }}" alt="wallabag logo" /></div>
+            <h2>{{ status_code }}: {{ status_text }}</h2>
+            <p>{{ exception.message }}</p>
+        </div>
+    </div>
+</main>
+{% endblock %}
+
+{% block footer %}
+{% endblock %}
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Exception/error.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Exception/error.html.twig
new file mode 100644 (file)
index 0000000..6be78ed
--- /dev/null
@@ -0,0 +1,30 @@
+{% extends "WallabagCoreBundle::layout.html.twig" %}
+
+{% block title %}{{ 'error.page_title'|trans }}{% endblock %}
+
+{% block body_class %}login{% endblock %}
+
+{% block menu %}{% endblock %}
+{% block messages %}{% endblock %}
+
+{% block content %}
+<main class="valign-wrapper">
+    <div class="valign row">
+        <div class="card sw">
+            <div class="center"><img src="{{ asset('bundles/wallabagcore/themes/_global/img/logo-other_themes.png') }}" alt="wallabag logo" /></div>
+            <div class="card-content">
+                <div class="row">
+                    <h5>{{ status_code }}: {{ status_text }}</h5>
+                    <p>{{ exception.message }}</p>
+                    {# {% for trace in exception.trace %}
+                        <p>{{ trace.class }} - {{ trace.type }} - {{ trace.file }} - {{ trace.line }}</p>
+                    {% endfor %} #}
+                </div>
+            </div>
+        </div>
+    </div>
+</main>
+{% endblock %}
+
+{% block footer %}
+{% endblock %}
index e29b58b5795c35f3e29d4c56cf854f00f2af541e..2e6fccfb152fe8830efddad032b8b15e9e95bb17 100644 (file)
@@ -125,16 +125,14 @@ class UsernameRssTokenConverterTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($converter->supports($params));
     }
 
-    /**
-     * @expectedException InvalidArgumentException
-     * @expectedExceptionMessage Route attribute is missing
-     */
     public function testApplyEmptyRequest()
     {
         $params = new ParamConverter([]);
         $converter = new UsernameRssTokenConverter();
 
-        $converter->apply(new Request(), $params);
+        $res = $converter->apply(new Request(), $params);
+
+        $this->assertFalse($res);
     }
 
     /**