我正在使用 Slim3 开发一个相当大的 JSON API。我的控制器/动作目前充斥着以下内容:
return $response->withJson([
'status' => 'error',
'data' => null,
'message' => 'Username or password was incorrect'
]);
在应用程序的某些点上,任何事情都可能出错,因此需要做出适当的响应。但常见的一件事是错误响应总是相同的。status
总是error
,是可选的data
(在表单验证错误的情况下data
将包含这些),并message
设置为向 API 的用户或使用者指示出了什么问题。
我闻到代码重复的味道。如何减少代码重复?
从我的脑海中,我所能想到的就是创建一个自定义异常,类似这样的App\Exceptions\AppException
事情需要选项data
并且message
将获得表单$e->getMessage()
。
<?php
namespace App\Exceptions;
class AppException extends Exception
{
private $data;
public function __construct($message, $data = null, $code = 0, $previous = null)
{
$this->data = $data;
parent::__construct($message, $code, $previous);
}
public function getData()
{
return $this->data;
}
}
之后创建中间件,调用$next
包装在 try/catch 中:
$app->add(function($request, $response, $next) {
try {
return $next($request, $response);
}
catch(\App\Exceptions\AppException $e)
{
$container->Logger->addCritical('Application Error: ' . $e->getMessage());
return $response->withJson([
'status' => 'error',
'data' => $e->getData(),
'message' => $e->getMessage()
]);
}
catch(\Exception $e)
{
$container->Logger->addCritical('Unhandled Exception: ' . $e->getMessage());
$container->SMSService->send(getenv('ADMIN_MOBILE'), "Shit has hit the fan! Run to your computer and check the error logs. Beep. Boop.");
return $response->withJson([
'status' => 'error',
'data' => null,
'message' => 'It is not possible to perform this action right now'
]);
}
});
现在我在代码中所要做的就是throw new \App\Exceptions\AppException("Username or password incorrect", null)
.
我唯一的问题是感觉我使用异常是出于错误的原因,这可能会使调试变得更加困难。
关于减少重复和清理错误响应的任何建议?