removed HttpException base class, refactored FlattenException class

This commit is contained in:
Fabien Potencier 2010-12-10 09:16:42 +01:00
parent 23fbd87164
commit 55bed307f1
14 changed files with 28 additions and 68 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -5,7 +5,7 @@
</head> </head>
<body> <body>
<h1>Oops! An Error Occurred</h1> <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> <div>
Something is broken. Please e-mail us at [email] and let us know Something is broken. Please e-mail us at [email] and let us know

View File

@ -1,7 +1,7 @@
Oops! An Error Occurred 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 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 error occurred. We will fix it as soon as possible. Sorry for any

View File

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

View File

@ -5,7 +5,7 @@
</div> </div>
<div style="float: left; width: 600px"> <div style="float: left; width: 600px">
<h1>{{ exception.message|replace(["\n": '<br />'])|format_file_from_text }}</h1> <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 %} {% set previous_count = exception.previouses|length %}
{% if previous_count %} {% if previous_count %}

View File

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

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="{{ _charset }}" ?> <?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 %} {% for e in exception.toarray %}
<exception class="{{ e.class }}" message="{{ e.message }}"> <exception class="{{ e.class }}" message="{{ e.message }}">
{% include 'FrameworkBundle:Exception:traces.twig' with ['exception': e] only %} {% include 'FrameworkBundle:Exception:traces.twig' with ['exception': e] only %}

View File

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset={{ _charset }}"/> <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"> <style type="text/css">
html { background: #eee } html { background: #eee }
body { font: 11px Verdana, Arial, sans-serif; color: #333 } body { font: 11px Verdana, Arial, sans-serif; color: #333 }

View File

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

View File

@ -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);
}
}

View File

@ -16,7 +16,7 @@ namespace Symfony\Component\HttpKernel\Exception;
* *
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @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) public function __construct($message = 'Not Found', \Exception $previous = null)
{ {