From e7eff640b7f75ae57eb0cc737809dd1ea89653d0 Mon Sep 17 00:00:00 2001 From: "sosuke.iwabuchi" Date: Thu, 20 Jul 2023 15:21:48 +0900 Subject: [PATCH] =?UTF-8?q?WebController=E5=86=85=E3=81=A7=E3=83=88?= =?UTF-8?q?=E3=83=A9=E3=83=B3=E3=82=B6=E3=82=AF=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E3=82=92=E7=AE=A1=E7=90=86=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Web/Auth/LoginController.php | 1 + app/Http/Controllers/Web/Auth/LogoutController.php | 1 + app/Http/Controllers/Web/Auth/MeController.php | 4 +--- app/Http/Controllers/Web/WebController.php | 12 +++++++++++- app/Util/DBUtil.php | 9 ++++++--- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Web/Auth/LoginController.php b/app/Http/Controllers/Web/Auth/LoginController.php index d007fa1..2381c5e 100644 --- a/app/Http/Controllers/Web/Auth/LoginController.php +++ b/app/Http/Controllers/Web/Auth/LoginController.php @@ -25,6 +25,7 @@ class LoginController extends WebController public function __construct(protected LoginParam $param) { + parent::__construct(); } protected function run(Request $request): JsonResponse diff --git a/app/Http/Controllers/Web/Auth/LogoutController.php b/app/Http/Controllers/Web/Auth/LogoutController.php index daf7bc4..17642aa 100644 --- a/app/Http/Controllers/Web/Auth/LogoutController.php +++ b/app/Http/Controllers/Web/Auth/LogoutController.php @@ -25,6 +25,7 @@ class LogoutController extends WebController public function __construct(protected LogoutParam $param) { + parent::__construct(); } protected function run(Request $request): JsonResponse diff --git a/app/Http/Controllers/Web/Auth/MeController.php b/app/Http/Controllers/Web/Auth/MeController.php index a2c6aa2..17d7b78 100644 --- a/app/Http/Controllers/Web/Auth/MeController.php +++ b/app/Http/Controllers/Web/Auth/MeController.php @@ -2,12 +2,9 @@ namespace App\Http\Controllers\Web\Auth; -use App\Http\Controllers\Web\IParam; use App\Http\Controllers\Web\WebController; -use App\Models\User; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; class MeController extends WebController { @@ -26,6 +23,7 @@ class MeController extends WebController public function __construct(protected MeParam $param) { + parent::__construct(); } diff --git a/app/Http/Controllers/Web/WebController.php b/app/Http/Controllers/Web/WebController.php index eb46b7b..1071dc5 100644 --- a/app/Http/Controllers/Web/WebController.php +++ b/app/Http/Controllers/Web/WebController.php @@ -167,16 +167,23 @@ abstract class WebController extends BaseController $this->authorize(); - return $this->run($request); + $this->transaction->beginTransaction(); + $ret = $this->run($request); + $this->transaction->commit(); + return $ret; } catch (GeneralErrorMessageException $e) { + $this->transaction->rollBack(); return $this->failedResponse([], $e->getMessage()); } catch (AppCommonException $e) { + $this->transaction->rollBack(); logs()->error(sprintf("Appエラー:%s", $e->getMessage())); return $this->failedResponse(); } catch (ExclusiveException $e) { + $this->transaction->rollBack(); logs()->error(sprintf("排他エラー:%s", $e->getMessage())); return $this->exclusiveErrorResponse(); } catch (LogicException $e) { + $this->transaction->rollBack(); logs()->error([ sprintf("実装エラー:%s", $e->getMessage()), get_class($e), @@ -189,13 +196,16 @@ abstract class WebController extends BaseController }, ARRAY_FILTER_USE_BOTH)); return $this->failedResponse(); } catch (ValidationException $e) { + $this->transaction->rollBack(); return $this->validateErrorResponse($e); } catch (HttpException $e) { + $this->transaction->rollBack(); if ($e->getStatusCode() === 401) { return $this->unAuthorizedResponse(); } throw e; } catch (Exception $e) { + $this->transaction->rollBack(); logs()->error([ sprintf("例外エラー:%s", $e->getMessage()), get_class($e), diff --git a/app/Util/DBUtil.php b/app/Util/DBUtil.php index 22f676e..47a5fb4 100644 --- a/app/Util/DBUtil.php +++ b/app/Util/DBUtil.php @@ -22,7 +22,8 @@ class DBUtil public function beginTransaction(): void { if ($this->isBeginning) { - throw new LogicException("2重トランザクション開始検知"); + // throw new LogicException("2重トランザクション開始検知"); + return; } DB::beginTransaction(); @@ -32,7 +33,8 @@ class DBUtil public function commit(): void { if (!$this->isBeginning) { - throw new LogicException("無効なコミット検知"); + // throw new LogicException("無効なコミット検知"); + return; } DB::commit(); $this->isBeginning = false; @@ -41,7 +43,8 @@ class DBUtil public function rollBack(): void { if (!$this->isBeginning) { - throw new LogicException("無効なロールバック検知"); + // throw new LogicException("無効なロールバック検知"); + return; } DB::rollBack();