我正在使用CakePHP3构建JSON RESTful API,但我不确定处理错误的最佳方法是什么,并向客户端提供有关错误的信息。到目前为止,我的方法是抛出一个HttpException,如果(例如)由于验证错误而导致实体保存失败。
在我的控制器中,我有以下内容:
if (!$this->Categories->save($categoryEntity)) {
throw new InternalErrorException('Saving failed');
}
$this->set('status', 'Everything fine!');
$this->set('_serialize', true);
如果保存失败,则将异常序列化为json,响应如下所示:
{
"message": "Saving failed",
"url": "\/categories\/edit",
"code": 500,
}
现在我想要包含一些有关错误的更详细信息。例如:
{
"message": "Saving failed",
"errors": "Validation error: Field id has to be numeric"
"url": "\/categories\/edit",
"code": 500,
}
我已尝试使用扩展的HttpException,它将错误作为额外参数,但该额外参数未被序列化。如何在异常中包含一些额外信息,或者如何在CakePHP3中更改异常的序列化行为?
答案 0 :(得分:3)
异常的序列化视图变量在异常渲染器中进行了硬编码,您必须创建一个处理自定义异常的自定义/扩展变量,以便它可以获取它提供的其他数据。 / p>
这是一个快速的&脏示例,使用名为ValidationErrorException
的自定义异常(InternalErrorException
已被CakePHP核心使用),它扩展了\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);
}
另见