异常的序列化视图变量在异常渲染器中进行了硬编码,您必须创建一个自定义/扩展变量来处理您的自定义异常,以便它可以获取它提供的额外数据。
这是一个快速而肮脏的示例,使用名为 ValidationErrorException 的自定义异常(CakePHP 核心已使用 InternalErrorException),它扩展了 \Cake\Http\Exception\HttpException,并实现了一个返回验证错误的 getValidationErrors() 方法:
// in src/Error/Exception/ValidationErrorException.php
namespace App\Error\Exception;
use Cake\Datasource\EntityInterface;
use Cake\Http\Exception\HttpException;
class ValidationErrorException extends HttpException
{
protected $_validationErrors;
public function __construct(EntityInterface $entity, $message = null, $code = 422)
{
$this->_validationErrors = $entity->getErrors();
if ($message === null) {
$message = 'A validation error occurred.';
}
parent::__construct($message, $code);
}
public function getValidationErrors()
{
return $this->_validationErrors;
}
}
这样的 HTTP 异常将映射到具有匹配名称的异常渲染器类方法:
// in src/Error/AppExceptionRenderer.php
namespace App\Error;
use App\Error\Exception\ValidationErrorException;
use Cake\Error\ExceptionRenderer;
class AppExceptionRenderer extends ExceptionRenderer
{
// HttpExceptions automatically map to methods matching the inflected variable name
public function validationError(ValidationErrorException $exception)
{
$code = $this->_code($exception);
$method = $this->_method($exception);
$template = $this->_template($exception, $method, $code);
$message = $this->_message($exception, $code);
$url = $this->controller->request->getRequestTarget();
$response = $this->controller->getResponse();
foreach ((array)$exception->responseHeader() as $key => $value) {
$response = $response->withHeader($key, $value);
}
$this->controller->setResponse($response->withStatus($code));
$viewVars = [
'message' => $message,
'url' => h($url),
'error' => $exception,
'code' => $code,
// set the errors as a view variable
'errors' => $exception->getValidationErrors(),
'_serialize' => [
'message',
'url',
'code',
'errors' // mark the variable as to be serialized
]
];
$this->controller->set($viewVars);
return $this->_outputMessage($template);
}
}
在你的控制器中,你可以像这样抛出它,将验证失败的实体提供给它:
if (!$this->Categories->save($categoryEntity)) {
throw new \App\Error\Exception\ValidationErrorException($categoryEntity);
}
另见