From a9e71d55657c0d294b5ca962484ffa912d31a30d Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 23 Aug 2013 07:05:33 -0400 Subject: [PATCH] better handling with exceptions in __toString() --- framework/yii/web/Response.php | 2 +- framework/yii/widgets/ActiveField.php | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/framework/yii/web/Response.php b/framework/yii/web/Response.php index ac5fc77..bdb9178 100644 --- a/framework/yii/web/Response.php +++ b/framework/yii/web/Response.php @@ -781,7 +781,7 @@ class Response extends \yii\base\Response if (is_array($this->content)) { $this->content = 'array()'; } elseif (is_object($this->content)) { - $this->content = method_exists($this->content, '__toString') ? (string)$this->content : get_class($this->content); + $this->content = method_exists($this->content, '__toString') ? $this->content->__toString() : get_class($this->content); } } } diff --git a/framework/yii/widgets/ActiveField.php b/framework/yii/widgets/ActiveField.php index 00a28df..bba1ead 100644 --- a/framework/yii/widgets/ActiveField.php +++ b/framework/yii/widgets/ActiveField.php @@ -128,7 +128,14 @@ class ActiveField extends Component */ public function __toString() { - return $this->render(); + // __toString cannot throw exception + // use trigger_error to bypass this limitation + try { + return $this->render(); + } catch (\Exception $e) { + trigger_error($e->getMessage()); + return ''; + } } /**