diff --git a/app/Kintone/KintoneAccess.php b/app/Kintone/KintoneAccess.php index b07d721..07d010c 100644 --- a/app/Kintone/KintoneAccess.php +++ b/app/Kintone/KintoneAccess.php @@ -4,6 +4,7 @@ namespace App\Kintone; use App\Exceptions\AppCommonException; use App\Exceptions\ConfigException; +use App\Exceptions\ExclusiveException; use App\Files\TmpFile; use App\Kintone\Models\KintoneModel; use Exception; @@ -197,14 +198,6 @@ class KintoneAccess "revision" => $model->getRevision(), ]; - // logger([ - // "ACTION" => "KINTONE UPDATE", - // "APP" => $this->appName, - // "RECORD_NO" => $model->getRecordId(), - // "SEND_DATA" => $sendData, - // "JSON" => json_encode($sendData, JSON_UNESCAPED_UNICODE), - // ]); - $response = Http::withHeaders([ "X-Cybozu-API-Token" => $this->apiToken, ])->put($this->getRecordUrl(), $sendData); @@ -212,7 +205,30 @@ class KintoneAccess if ($response->failed()) { $e = $response->toException(); if ($e instanceof Exception) { - Log::error($e->getMessage()); + $context = [ + "action" => "KINTONE_UPDATE", + "appName" => $this->appName, + "appId" => $this->appId, + "recordId" => $model->getRecordId(), + "sendRevision" => $model->getRevision(), + "sendRecord" => $sendData["record"], + "status" => $response->status(), + "responseBody" => $response->body(), + ]; + + if ($response->status() === 409) { + try { + $current = $this->find((int)$model->getRecordId()); + $context["currentRevision"] = $current->getRevision(); + } catch (Exception $findException) { + $context["currentRevisionError"] = $findException->getMessage(); + } + + Log::error(sprintf("Kintone排他エラー:%s", $e->getMessage()), $context); + throw new ExclusiveException($e->getMessage(), (int)$e->getCode(), $e); + } + + Log::error($e->getMessage(), $context); throw $e; } } @@ -239,9 +255,14 @@ class KintoneAccess if ($response->failed()) { $e = $response->toException(); if ($e instanceof Exception) { - Log::error($e->getMessage()); - Log::error($response->body()); - Log::error($sendData); + Log::error($e->getMessage(), [ + "action" => "KINTONE_CREATE", + "appName" => $this->appName, + "appId" => $this->appId, + "sendRecord" => $sendData["record"], + "status" => $response->status(), + "responseBody" => $response->body(), + ]); throw $e; } }