|
|
@@ -19,7 +19,8 @@ namespace Cake\View;
|
|
|
use Cake\Event\EventManager;
|
|
|
use Cake\Http\Response;
|
|
|
use Cake\Http\ServerRequest;
|
|
|
-use RuntimeException;
|
|
|
+use Cake\View\Exception\SerializationFailureException;
|
|
|
+use Exception;
|
|
|
|
|
|
/**
|
|
|
* Parent class for view classes generating serialized outputs like JsonView and XmlView.
|
|
|
@@ -89,9 +90,9 @@ abstract class SerializedView extends View
|
|
|
*
|
|
|
* @param array|string $serialize The name(s) of the view variable(s) that
|
|
|
* need(s) to be serialized
|
|
|
- * @return string|false The serialized data or false.
|
|
|
+ * @return string The serialized data.
|
|
|
*/
|
|
|
- abstract protected function _serialize($serialize);
|
|
|
+ abstract protected function _serialize($serialize): string;
|
|
|
|
|
|
/**
|
|
|
* Render view template or return serialized data.
|
|
|
@@ -99,6 +100,7 @@ abstract class SerializedView extends View
|
|
|
* @param string|null $template The template being rendered.
|
|
|
* @param string|null|false $layout The layout being rendered.
|
|
|
* @return string The rendered view.
|
|
|
+ * @throws \Cake\View\SerializationFailureException When serialization fails.
|
|
|
*/
|
|
|
public function render(?string $template = null, $layout = null): string
|
|
|
{
|
|
|
@@ -118,12 +120,15 @@ abstract class SerializedView extends View
|
|
|
);
|
|
|
}
|
|
|
if ($serialize !== false) {
|
|
|
- $result = $this->_serialize($serialize);
|
|
|
- if ($result === false) {
|
|
|
- throw new RuntimeException('Serialization of View data failed.');
|
|
|
+ try {
|
|
|
+ return $this->_serialize($serialize);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ throw new SerializationFailureException(
|
|
|
+ 'Serialization of View data failed.',
|
|
|
+ null,
|
|
|
+ $e
|
|
|
+ );
|
|
|
}
|
|
|
-
|
|
|
- return $result;
|
|
|
}
|
|
|
|
|
|
return parent::render($template, false);
|