{"record":{"id":"f743aedd0f6f4c80","repo":"passbolt/passbolt_api","slug":"could-not-save-the-secret-history","errorCode":null,"errorMessage":"Could not save the secret history.","messagePattern":"Could not save the secret history\\.","errorType":"exception","errorClass":"InternalErrorException","httpStatus":500,"severity":"error","filePath":"plugins/PassboltCe/Log/src/Model/Table/SecretsHistoryTable.php","lineNumber":150,"sourceCode":"     *\n     * @param array $data the data\n     * @return \\Passbolt\\Log\\Model\\Entity\\SecretHistory\n     * @throws \\App\\Error\\Exception\\ValidationException\n     * @throws \\Cake\\Http\\Exception\\InternalErrorException\n     */\n    public function create(array $data): SecretHistory\n    {\n        // Check validation rules.\n        $secretHistory = $this->buildEntity($data);\n        if ($secretHistory->getErrors()) {\n            throw new ValidationException(__('Could not validate secret history data.', true), $secretHistory, $this);\n        }\n\n        $secretHistory = $this->save($secretHistory);\n\n        // Check for errors while saving.\n        if (!$secretHistory) {\n            throw new InternalErrorException('Could not save the secret history.');\n        }\n\n        // Check for validation errors. (associated models too).\n        if ($secretHistory->getErrors()) {\n            throw new ValidationException(__('Could not validate secret history data.'), $secretHistory, $this);\n        }\n\n        return $secretHistory;\n    }\n}\n","sourceCodeStart":132,"sourceCodeEnd":161,"githubUrl":"https://github.com/passbolt/passbolt_api/blob/31c1bbc10f32808a607fa9bd81891e898779c0bc/plugins/PassboltCe/Log/src/Model/Table/SecretsHistoryTable.php#L132-L161","documentation":"Thrown by SecretsHistoryTable::create() when save() returns false for an entity that already passed validation — a persistence-layer failure, not a data problem. Raised as InternalErrorException (HTTP 500).","triggerScenarios":"$this->save($secretHistory) returning falsy: foreign-key constraint failure (secret or user row gone), missing secrets_history table, database lock/connection failure, or a beforeSave listener aborting the operation.","commonSituations":"Secret hard-deleted concurrently while history is being written, skipped migrations leaving secrets_history absent, DB storage/replication problems, and plugin schema drift after upgrades.","solutions":["Verify the referenced user_id/secret_id rows still exist when the save runs","Run pending migrations to ensure the secrets_history table exists and matches schema","Check database error logs for the exact SQL failure and resolve the constraint","Retry if transient; wrap multi-row history writes in a transaction for consistency"],"exampleFix":"// before\n$this->SecretsHistory->create($data); // 500 if secret row vanished\n// after\nif (!$this->Secrets->exists(['id' => $data['secret_id']])) {\n    $this->log('Skipping secret history: secret missing ' . $data['secret_id']);\n    return;\n}\n$this->SecretsHistory->create($data);","handlingStrategy":"try-catch","validationCode":"if (!$this->Secrets->exists(['id' => $data['secret_id']]) || !$this->Users->exists(['id' => $data['user_id']])) {\n    return false; // skip history write\n}","typeGuard":"if (!is_array($data) || empty($data['secret_id']) || empty($data['user_id'])) { return false; }","tryCatchPattern":"try {\n    $this->SecretsHistory->create($data);\n} catch (\\Cake\\Http\\Exception\\InternalErrorException $e) {\n    $this->log('Secret history save failed: ' . $e->getMessage());\n    // verify DB health/migrations; retry transient failures\n}","preventionTips":["Run migrations after upgrades (secrets_history schema)","Use transactions when writing secret + history together","Check DB disk/replication health if this recurs","Avoid deleting secrets while history writes are pending"],"tags":["database","persistence","orm"],"backgroundTag":"database-write-failed","analyzedSha":"31c1bbc10f32808a607fa9bd81891e898779c0bc","analyzedAt":"2026-09-17T00:04:38.960Z","contentChangedAt":"2026-09-17T00:04:38.960Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}