refactor: 使用 ApiMessage 统一错误响应格式
- 在多个控制器中引入 ApiMessage,替换原有的 ApiResponse 错误处理逻辑,确保错误信息的一致性与可读性。 - 更新错误返回信息,使用更具语义的键值,提升 API 的可维护性与用户体验。 - 适配相关控制器的请求参数,确保在处理错误时能够正确返回相应的错误信息。
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiMessage;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -20,7 +21,7 @@ final class AdminDrawBatchDestroyController extends Controller
|
||||
$drawIds = $request->input('draw_ids', []);
|
||||
|
||||
if (!is_array($drawIds) || empty($drawIds)) {
|
||||
return ApiResponse::error(trans('api.invalid_params'), ErrorCode::ClientHttpError->value, [], 400);
|
||||
return ApiMessage::errorResponse($request, 'invalid_params', ErrorCode::ClientHttpError->value, [], 400);
|
||||
}
|
||||
|
||||
$results = [
|
||||
@@ -36,17 +37,14 @@ final class AdminDrawBatchDestroyController extends Controller
|
||||
} catch (\RuntimeException $e) {
|
||||
$results['failed'][] = [
|
||||
'id' => $drawId,
|
||||
'reason' => match ($e->getMessage()) {
|
||||
'draw_not_deletable' => trans('api.draw_not_deletable'),
|
||||
'draw_has_bets' => trans('api.draw_has_bets'),
|
||||
'draw_result_exists' => trans('api.draw_result_exists'),
|
||||
default => trans('api.client_error'),
|
||||
},
|
||||
'reason' => ApiMessage::reason($request, $e->getMessage()),
|
||||
'reason_key' => $e->getMessage(),
|
||||
];
|
||||
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
|
||||
$results['failed'][] = [
|
||||
'id' => $drawId,
|
||||
'reason' => trans('api.draw_not_found'),
|
||||
'reason' => ApiMessage::get($request, 'draw_not_found'),
|
||||
'reason_key' => 'draw_not_found',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
use App\Models\Draw;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Support\ApiMessage;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Draw\DrawDestroyService;
|
||||
@@ -15,19 +17,12 @@ final class AdminDrawDestroyController extends Controller
|
||||
private readonly DrawDestroyService $service,
|
||||
) {}
|
||||
|
||||
public function __invoke(Draw $draw): JsonResponse
|
||||
public function __invoke(Request $request, Draw $draw): JsonResponse
|
||||
{
|
||||
try {
|
||||
$this->service->destroy($draw);
|
||||
} catch (\RuntimeException $e) {
|
||||
$message = match ($e->getMessage()) {
|
||||
'draw_not_deletable' => trans('api.draw_not_deletable'),
|
||||
'draw_has_bets' => trans('api.draw_has_bets'),
|
||||
'draw_result_exists' => trans('api.draw_result_exists'),
|
||||
default => trans('api.client_error'),
|
||||
};
|
||||
|
||||
return ApiResponse::error($message, ErrorCode::ClientHttpError->value, ['reason' => $e->getMessage()], 409);
|
||||
return ApiMessage::runtimeErrorResponse($request, $e);
|
||||
}
|
||||
|
||||
return ApiResponse::success(['deleted' => true]);
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Support\ApiMessage;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Draw\DrawManualCreateService;
|
||||
@@ -20,13 +21,7 @@ final class AdminDrawStoreController extends Controller
|
||||
try {
|
||||
$draw = $this->service->create($request->validated());
|
||||
} catch (\RuntimeException $e) {
|
||||
$message = match ($e->getMessage()) {
|
||||
'draw_no_exists' => trans('api.draw_no_exists'),
|
||||
'draw_timeline_invalid' => trans('api.draw_timeline_invalid'),
|
||||
default => trans('api.client_error'),
|
||||
};
|
||||
|
||||
return ApiResponse::error($message, ErrorCode::ClientHttpError->value, ['reason' => $e->getMessage()], 409);
|
||||
return ApiMessage::runtimeErrorResponse($request, $e);
|
||||
}
|
||||
|
||||
return ApiResponse::success([
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
use App\Models\Draw;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Support\ApiMessage;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\DrawStoreRequest;
|
||||
@@ -21,16 +22,7 @@ final class AdminDrawUpdateController extends Controller
|
||||
try {
|
||||
$updated = $this->service->update($draw, $request->validated());
|
||||
} catch (\RuntimeException $e) {
|
||||
$message = match ($e->getMessage()) {
|
||||
'draw_no_exists' => trans('api.draw_no_exists'),
|
||||
'draw_timeline_invalid' => trans('api.draw_timeline_invalid'),
|
||||
'draw_not_editable' => trans('api.draw_not_editable'),
|
||||
'draw_has_bets' => trans('api.draw_has_bets'),
|
||||
'draw_result_exists' => trans('api.draw_result_exists'),
|
||||
default => trans('api.client_error'),
|
||||
};
|
||||
|
||||
return ApiResponse::error($message, ErrorCode::ClientHttpError->value, ['reason' => $e->getMessage()], 409);
|
||||
return ApiMessage::runtimeErrorResponse($request, $e);
|
||||
}
|
||||
|
||||
return ApiResponse::success([
|
||||
|
||||
@@ -4,7 +4,9 @@ namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiMessage;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Draw\DrawAdminActionService;
|
||||
@@ -15,12 +17,12 @@ final class DrawCancelController extends Controller
|
||||
private readonly DrawAdminActionService $service,
|
||||
) {}
|
||||
|
||||
public function __invoke(Draw $draw): JsonResponse
|
||||
public function __invoke(Request $request, Draw $draw): JsonResponse
|
||||
{
|
||||
try {
|
||||
$cancelled = $this->service->cancelBeforeResult($draw);
|
||||
} catch (\RuntimeException) {
|
||||
return ApiResponse::error(trans('api.client_error'), ErrorCode::ClientHttpError->value, null, 409);
|
||||
} catch (\RuntimeException $e) {
|
||||
return ApiMessage::runtimeErrorResponse($request, $e);
|
||||
}
|
||||
|
||||
return ApiResponse::success([
|
||||
|
||||
@@ -4,7 +4,9 @@ namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiMessage;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Draw\DrawAdminActionService;
|
||||
@@ -15,12 +17,12 @@ final class DrawManualCloseController extends Controller
|
||||
private readonly DrawAdminActionService $service,
|
||||
) {}
|
||||
|
||||
public function __invoke(Draw $draw): JsonResponse
|
||||
public function __invoke(Request $request, Draw $draw): JsonResponse
|
||||
{
|
||||
try {
|
||||
$closed = $this->service->manualClose($draw);
|
||||
} catch (\RuntimeException) {
|
||||
return ApiResponse::error(trans('api.client_error'), ErrorCode::ClientHttpError->value, null, 409);
|
||||
} catch (\RuntimeException $e) {
|
||||
return ApiMessage::runtimeErrorResponse($request, $e);
|
||||
}
|
||||
|
||||
return ApiResponse::success([
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
use App\Models\Draw;
|
||||
use App\Models\AdminUser;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiMessage;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
@@ -31,13 +32,8 @@ final class DrawManualResultBatchStoreController extends Controller
|
||||
|
||||
try {
|
||||
$batch = $this->service->createPendingBatch($draw, $admin, $request->validated('items'));
|
||||
} catch (\RuntimeException) {
|
||||
return ApiResponse::error(
|
||||
trans('api.client_error', [], $request->lotteryLocale()),
|
||||
ErrorCode::ClientHttpError->value,
|
||||
null,
|
||||
409,
|
||||
);
|
||||
} catch (\RuntimeException $e) {
|
||||
return ApiMessage::runtimeErrorResponse($request, $e);
|
||||
}
|
||||
|
||||
$draw->refresh();
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Models\Draw;
|
||||
use App\Models\AdminUser;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Support\ApiMessage;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Draw\DrawReopenService;
|
||||
@@ -39,13 +40,8 @@ final class DrawReopenController extends Controller
|
||||
|
||||
try {
|
||||
$reopened = $this->service->reopenCooldownDraw($draw, $admin, $request->validated('reason') ?? null);
|
||||
} catch (\RuntimeException) {
|
||||
return ApiResponse::error(
|
||||
trans('api.client_error', [], $request->lotteryLocale()),
|
||||
ErrorCode::ClientHttpError->value,
|
||||
null,
|
||||
409,
|
||||
);
|
||||
} catch (\RuntimeException $e) {
|
||||
return ApiMessage::runtimeErrorResponse($request, $e);
|
||||
}
|
||||
|
||||
return ApiResponse::success([
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Models\Draw;
|
||||
use App\Models\AdminUser;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Support\ApiMessage;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\DrawResultBatch;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -44,13 +45,8 @@ final class DrawResultBatchPublishController extends Controller
|
||||
|
||||
try {
|
||||
$this->publishService->publishManualBatch($batch, $admin);
|
||||
} catch (\RuntimeException) {
|
||||
return ApiResponse::error(
|
||||
trans('api.client_error', [], $request->lotteryLocale()),
|
||||
ErrorCode::ClientHttpError->value,
|
||||
null,
|
||||
409,
|
||||
);
|
||||
} catch (\RuntimeException $e) {
|
||||
return ApiMessage::runtimeErrorResponse($request, $e);
|
||||
}
|
||||
|
||||
$draw->refresh();
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
use App\Models\Draw;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Support\ApiMessage;
|
||||
use App\Lottery\DrawStatus;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
@@ -32,8 +33,8 @@ final class DrawRngRunController extends Controller
|
||||
|
||||
return $this->rng->executeLocked($locked);
|
||||
});
|
||||
} catch (\RuntimeException) {
|
||||
return ApiResponse::error(trans('api.client_error'), ErrorCode::ClientHttpError->value, null, 409);
|
||||
} catch (\RuntimeException $e) {
|
||||
return ApiMessage::runtimeErrorResponse(request(), $e);
|
||||
}
|
||||
|
||||
$draw->refresh();
|
||||
|
||||
Reference in New Issue
Block a user