Jelajahi Sumber

removed HttpException base class, refactored FlattenException class

Fabien Potencier 14 tahun lalu
induk
melakukan
55bed307f1

+ 17 - 4
src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php

@@ -5,6 +5,7 @@ namespace Symfony\Bundle\FrameworkBundle\Controller;
 use Symfony\Component\DependencyInjection\ContainerAware;
 use Symfony\Component\HttpKernel\Exception\FlattenException;
 use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
+use Symfony\Component\HttpFoundation\Response;
 
 /*
  * This file is part of the Symfony framework.
@@ -40,9 +41,7 @@ class ExceptionController extends ContainerAware
             $currentContent .= ob_get_clean();
         }
 
-        if ('Symfony\Component\Security\Exception\AccessDeniedException' === $exception->getClass()) {
-            $exception->setStatusCode($exception->getCode());
-        }
+        $code = $this->getStatusCode($exception);
 
         $template = $this->container->get('kernel')->isDebug() ? 'exception' : 'error';
         if ($this->container->get('kernel')->isDebug() && 'html' == $format) {
@@ -58,14 +57,28 @@ class ExceptionController extends ContainerAware
         $response = $templating->renderResponse(
             $template,
             array(
+                'status_code'    => $code,
+                'status_text'    => Response::$statusTexts[$code],
                 'exception'      => $exception,
                 'logger'         => $logger,
                 'currentContent' => $currentContent,
             )
         );
 
-        $response->setStatusCode($exception->getStatusCode());
+        $response->setStatusCode($code);
 
         return $response;
     }
+
+    protected function getStatusCode(FlattenException $exception)
+    {
+        switch ($exception->getClass()) {
+            case 'Symfony\Component\Security\Exception\AccessDeniedException':
+                return 403;
+            case 'Symfony\Component\HttpKernel\Exception\HttpNotFoundException':
+                return 404;
+            default:
+                return 500;
+        }
+    }
 }

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.css.twig

@@ -1,4 +1,4 @@
 /*
-{{ exception.statuscode }} {{ exception.statustext }}
+{{ status_code }} {{ status_text }}
 
 */

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.js.twig

@@ -1,4 +1,4 @@
 /*
-{{ exception.statuscode }} {{ exception.statustext }}
+{{ status_code }} {{ status_text }}
 
 */

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.json.twig

@@ -1 +1 @@
-{{ ['error': ['code': exception.statuscode, 'message': exception.statustext]]|json_encode }}
+{{ ['error': ['code': status_code, 'message': status_text]]|json_encode }}

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.twig

@@ -5,7 +5,7 @@
     </head>
     <body>
         <h1>Oops! An Error Occurred</h1>
-        <h2>The server returned a "{{ exception.statuscode }} {{ exception.statustext }}".</h2>
+        <h2>The server returned a "{{ status_code }} {{ status_text }}".</h2>
 
         <div>
             Something is broken. Please e-mail us at [email] and let us know

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.txt.twig

@@ -1,7 +1,7 @@
 Oops! An Error Occurred
 =======================
 
-The server returned a "{{ exception.statuscode }} {{ exception.statustext }}".
+The server returned a "{{ status_code }} {{ status_text }}".
 
 Please e-mail us at [email] and let us know what you were doing when this
 error occurred. We will fix it as soon as possible. Sorry for any

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.xml.twig

@@ -1,3 +1,3 @@
 <?xml version="1.0" encoding="{{ _charset }}" ?>
 
-<error code="{{ exception.statuscode }}" message="{{ exception.statustext }}" />
+<error code="{{ status_code }}" message="{{ status_text }}" />

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.twig

@@ -5,7 +5,7 @@
         </div>
         <div style="float: left; width: 600px">
             <h1>{{ exception.message|replace(["\n": '<br />'])|format_file_from_text }}</h1>
-            <h2><strong>{{ exception.statuscode }}</strong> {{ exception.statustext }} - {{ exception.class|abbr_class }}</h2>
+            <h2><strong>{{ status_code }}</strong> {{ status_text }} - {{ exception.class|abbr_class }}</h2>
 
             {% set previous_count = exception.previouses|length %}
             {% if previous_count %}

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.txt.twig

@@ -1,4 +1,4 @@
-[exception] {{ exception.statuscode ~ ' | ' ~ exception.statustext ~ ' | ' ~ exception.class }}
+[exception] {{ status_code ~ ' | ' ~ status_text ~ ' | ' ~ exception.class }}
 [message] {{ exception.message }}
 {% for i, e in exception.toarray %}
 [{{ i + 1 }}] {{ e.class }}: {{ e.message }}

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.xml.twig

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="{{ _charset }}" ?>
 
-<error code="{{ exception.statuscode }}" message="{{ exception.statustext }}">
+<error code="{{ status_code }}" message="{{ status_text }}">
 {% for e in exception.toarray %}
     <exception class="{{ e.class }}" message="{{ e.message }}">
 {% include 'FrameworkBundle:Exception:traces.twig' with ['exception': e] only %}

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/layout.twig

@@ -2,7 +2,7 @@
 <html>
     <head>
         <meta http-equiv="Content-Type" content="text/html; charset={{ _charset }}"/>
-        <title>{{ exception.message }} ({{ exception.statuscode }} {{ exception.statustext }})</title>
+        <title>{{ exception.message }} ({{ status_code }} {{ status_text }})</title>
         <style type="text/css">
             html { background: #eee }
             body { font: 11px Verdana, Arial, sans-serif; color: #333 }

+ 0 - 21
src/Symfony/Component/HttpKernel/Exception/FlattenException.php

@@ -2,9 +2,6 @@
 
 namespace Symfony\Component\HttpKernel\Exception;
 
-use Symfony\Component\HttpFoundation\Response;
-use Symfony\Component\HttpKernel\Exception\HttpException;
-
 /*
  * This file is part of the Symfony framework.
  *
@@ -28,7 +25,6 @@ class FlattenException
     protected $previous;
     protected $trace;
     protected $class;
-    protected $status;
 
     static public function create(\Exception $exception)
     {
@@ -40,7 +36,6 @@ class FlattenException
         if ($exception->getPrevious()) {
             $e->setPrevious(static::create($exception->getPrevious()));
         }
-        $e->setStatusCode($exception instanceof HttpException ? $exception->getCode() : 500);
 
         return $e;
     }
@@ -50,7 +45,6 @@ class FlattenException
         $exceptions = array();
         foreach (array_merge(array($this), $this->getPreviouses()) as $exception) {
             $exceptions[] = array(
-                'code'     => $exception->getStatusCode(),
                 'message'  => $exception->getMessage(),
                 'class'    => $exception->getClass(),
                 'trace'    => $exception->getTrace(),
@@ -60,21 +54,6 @@ class FlattenException
         return $exceptions;
     }
 
-    public function getStatusCode()
-    {
-        return $this->status;
-    }
-
-    public function setStatusCode($status)
-    {
-        $this->status = $status;
-    }
-
-    public function getStatusText()
-    {
-        return Response::$statusTexts[$this->getStatusCode()];
-    }
-
     public function getClass()
     {
         return $this->class;

+ 0 - 32
src/Symfony/Component/HttpKernel/Exception/HttpException.php

@@ -1,32 +0,0 @@
-<?php
-
-namespace Symfony\Component\HttpKernel\Exception;
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-/**
- * HttpException.
- *
- * By convention, exception code == response status code.
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
-class HttpException extends \Exception
-{
-    /**
-     * Constructor overridden to require the code, which is the status code.
-     *
-     * @see \Exception
-     */
-    public function __construct($message, $code, \Exception $previous = null)
-    {
-        parent::__construct($message, $code, $previous);
-    }
-}

+ 1 - 1
src/Symfony/Component/HttpKernel/Exception/NotFoundHttpException.php

@@ -16,7 +16,7 @@ namespace Symfony\Component\HttpKernel\Exception;
  *
  * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  */
-class NotFoundHttpException extends HttpException
+class NotFoundHttpException extends \RuntimeException
 {
     public function __construct($message = 'Not Found', \Exception $previous = null)
     {