我正在使用Slim3製作一個相當大的JSON API。我的控制器/行動目前充斥着如下:Slim3 /幹 - 如何正確處理錯誤/異常,無需重複代碼?
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)
。
我唯一的問題就是我覺得我使用異常的原因是錯誤的,它可能會使調試更加困難。
有關減少重複項和清除錯誤響應的任何建議?
你正在使用例外,就像你應該在Slim中一樣。我用Slim構建了很多API,並且使用了完全相同的方法 - 我想要停止請求處理時拋出的特定AppException,並讓它在Slim的exceptionHandler中被捕獲。這是我找到的最乾淨,必須脫鉤的方式。只需使用@Mika Tuupola方法。 –