Browse Source

Remove Exception coercion.

Just change error message and template instead.
ADmad 12 years ago
parent
commit
dc0da6e015
2 changed files with 48 additions and 15 deletions
  1. 36 11
      src/Error/ExceptionRenderer.php
  2. 12 4
      tests/TestCase/Error/ExceptionRendererTest.php

+ 36 - 11
src/Error/ExceptionRenderer.php

@@ -79,15 +79,6 @@ class ExceptionRenderer {
  * @param \Exception $exception Exception
  */
 	public function __construct(\Exception $exception) {
-		if (!Configure::read('debug') && !($exception instanceof Error\HttpException)) {
-			$code = $this->_code($exception);
-			if ($code < 500) {
-				$exception = new Error\NotFoundException();
-			} else {
-				$exception = new Error\InternalErrorException();
-			}
-		}
-
 		$this->controller = $this->_getController($exception);
 		$this->error = $exception;
 	}
@@ -136,7 +127,7 @@ class ExceptionRenderer {
 		$exception = $this->error;
 		$code = $this->_code($exception);
 		$template = $this->_template($exception, $code);
-		$message = $this->error->getMessage();
+		$message = $this->_message($exception, $code);
 		$url = $this->controller->request->here();
 
 		$this->controller->response->statusCode($code);
@@ -156,12 +147,46 @@ class ExceptionRenderer {
 	}
 
 /**
- * Get template for rendering exception info
+ * Get error message.
+ *
+ * @param Exception $exception Exception
+ * @param int $code Error code
+ * @return string Error message
+ */
+	protected function _message(\Exception $exception, $code) {
+		$message = $this->error->getMessage();
+
+		if (!Configure::read('debug') &&
+			!($exception instanceof Error\HttpException)
+		) {
+			if ($code < 500) {
+				$message = __d('cake', 'Not Found');
+			} else {
+				$message = __d('cake', 'An Internal Error Has Occurred.');
+			}
+		}
+
+		return $message;
+	}
+
+/**
+ * Get template for rendering exception info.
+ *
  * @param \Exception $exception
  * @param int $code Error code
  * @return string Template name
  */
 	protected function _template(\Exception $exception, $code) {
+		if (!Configure::read('debug') &&
+			!($exception instanceof Error\HttpException)
+		) {
+			$template = 'error500';
+			if ($code < 500) {
+				$template = 'error400';
+			}
+			return $this->template = $template;
+		}
+
 		list(, $baseClass) = namespaceSplit(get_class($exception));
 		$baseClass = substr($baseClass, 0, -9);
 		$template = Inflector::variable($baseClass);

+ 12 - 4
tests/TestCase/Error/ExceptionRendererTest.php

@@ -197,17 +197,25 @@ class ExceptionRendererTest extends TestCase {
 	}
 
 /**
- * test that exception gets coerced when debug = 0
+ * test that exception message gets coerced when debug = 0
  *
  * @return void
  */
-	public function testExceptionCoercion() {
+	public function testExceptionMessageCoercion() {
 		Configure::write('debug', false);
-		$exception = new MissingActionException('Page not found');
+		$exception = new MissingActionException('Secret info not to be leaked');
 		$ExceptionRenderer = new ExceptionRenderer($exception);
 
 		$this->assertInstanceOf('Cake\Controller\ErrorController', $ExceptionRenderer->controller);
-		$this->assertTrue($ExceptionRenderer->error instanceof Error\NotFoundException);
+		$this->assertEquals($exception, $ExceptionRenderer->error);
+
+		ob_start();
+		$ExceptionRenderer->render();
+		$result = ob_get_clean();
+
+		$this->assertEquals('error400', $ExceptionRenderer->template);
+		$this->assertContains('Not Found', $result);
+		$this->assertNotContains('Secret info not to be leaked', $result);
 	}
 
 /**