{"record":{"id":"1814b649516fd875","repo":"passbolt/passbolt_api","slug":"the-metadata-session-key-could-not-be-saved","errorCode":null,"errorMessage":"The metadata session key could not be saved.","messagePattern":"The metadata session key could not be saved\\.","errorType":"validation","errorClass":"CustomValidationException","httpStatus":400,"severity":"error","filePath":"plugins/PassboltCe/Metadata/src/Service/MetadataSessionKeyCreateService.php","lineNumber":54,"sourceCode":"     */\n    public function create(UserAccessControl $uac, mixed $data): MetadataSessionKey\n    {\n        $this->assertData($data);\n\n        /** @var \\Passbolt\\Metadata\\Model\\Table\\MetadataSessionKeysTable $metadataSessionKeysTable */\n        $metadataSessionKeysTable = $this->fetchTable('Passbolt/Metadata.MetadataSessionKeys');\n\n        $metadataSessionKey = $metadataSessionKeysTable->newEntity(\n            ['user_id' => $uac->getId(), 'data' => $data],\n            ['accessibleFields' => ['user_id' => true, 'data' => true]]\n        );\n        try {\n            /** @var \\Passbolt\\Metadata\\Model\\Entity\\MetadataSessionKey $result */\n            $result = $metadataSessionKeysTable->saveOrFail($metadataSessionKey);\n        } catch (PersistenceFailedException $e) { // @phpstan-ignore-line\n            $errors = $e->getEntity()->getErrors();\n\n            throw new CustomValidationException(\n                __('The metadata session key could not be saved.'),\n                $errors\n            );\n        } catch (Exception $e) {\n            throw new InternalErrorException(\n                __('Could not save the metadata session key, please try again later.'),\n                null,\n                $e\n            );\n        }\n\n        return $result;\n    }\n\n    /**\n     * Basic sanity check for the given data value.\n     *\n     * @param mixed $data Data to check.","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/passbolt/passbolt_api/blob/31c1bbc10f32808a607fa9bd81891e898779c0bc/plugins/PassboltCe/Metadata/src/Service/MetadataSessionKeyCreateService.php#L36-L72","documentation":"CustomValidationException thrown in MetadataSessionKeyCreateService::create when saveOrFail raises a PersistenceFailedException. The entity's validation errors are attached to the exception so the API can return them to the client. It covers all table-level rule failures for a new metadata session key.","triggerScenarios":"POST creating a metadata session key where the entity fails validation: missing/invalid `data`, invalid user_id, or a custom rule (e.g. user already has an active session key).","commonSituations":"Duplicate session key for the same user when a previous one wasn't deleted; payload `data` not a valid encrypted string; schema constraint differences across MySQL/MariaDB/Postgres.","solutions":["Read the `errors` attribute of the exception response for the exact failing fields.","Delete/deactivate any existing session key for the user before creating a new one if the rule forbids duplicates.","Ensure `data` is a properly encrypted string per the metadata session key specification."],"exampleFix":"// before: create without cleaning up old key\nawait api.post('/metadata/session-keys', {data});\n// after\nawait api.delete(`/metadata/session-keys/${existingId}`);\nawait api.post('/metadata/session-keys', {data});","handlingStrategy":"validation","validationCode":"const existing = await api.get('/metadata/session-keys');\nif (existing.some(k => k.userId === currentUserId)) {\n  throw new Error('session key already exists for user');\n}","typeGuard":null,"tryCatchPattern":"catch (CustomValidationException $e) {\n  $errors = $e->getErrors();\n  // handle duplicate/invalid fields accordingly\n}","preventionTips":["Clean up old session keys before creating new ones","Send `data` as a valid encrypted string","Test against MySQL, MariaDB and Postgres to catch rule differences"],"tags":["validation","metadata","session-key","php"],"backgroundTag":"schema-validation-failed","analyzedSha":"31c1bbc10f32808a607fa9bd81891e898779c0bc","analyzedAt":"2026-09-17T00:04:38.960Z","contentChangedAt":"2026-09-17T00:04:38.960Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}