{"record":{"id":"906b0698a16ecea1","repo":"symfony/http-foundation","slug":"s-requires-pdo-error-mode-attribute-be-set-to-throw","errorCode":null,"errorMessage":"\"%s\" requires PDO error mode attribute be set to throw Exceptions (i.e. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)).","messagePattern":"\"(.+?)\" requires PDO error mode attribute be set to throw Exceptions \\(i\\.e\\. \\$pdo->setAttribute\\(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION\\)\\)\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"Session/Storage/Handler/PdoSessionHandler.php","lineNumber":164,"sourceCode":"     *  * db_id_col: The column where to store the session id [default: sess_id]\n     *  * db_data_col: The column where to store the session data [default: sess_data]\n     *  * db_lifetime_col: The column where to store the lifetime [default: sess_lifetime]\n     *  * db_time_col: The column where to store the timestamp [default: sess_time]\n     *  * db_username: The username when lazy-connect [default: '']\n     *  * db_password: The password when lazy-connect [default: '']\n     *  * db_connection_options: An array of driver-specific connection options [default: []]\n     *  * lock_mode: The strategy for locking, see constants [default: LOCK_TRANSACTIONAL]\n     *  * ttl: The time to live in seconds.\n     *\n     * @param \\PDO|string|null $pdoOrDsn A \\PDO instance or DSN string or URL string or null\n     *\n     * @throws \\InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION\n     */\n    public function __construct(#[\\SensitiveParameter] \\PDO|string|null $pdoOrDsn = null, #[\\SensitiveParameter] array $options = [])\n    {\n        if ($pdoOrDsn instanceof \\PDO) {\n            if (\\PDO::ERRMODE_EXCEPTION !== $pdoOrDsn->getAttribute(\\PDO::ATTR_ERRMODE)) {\n                throw new \\InvalidArgumentException(\\sprintf('\"%s\" requires PDO error mode attribute be set to throw Exceptions (i.e. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)).', __CLASS__));\n            }\n\n            $this->pdo = $pdoOrDsn;\n            $this->driver = $this->pdo->getAttribute(\\PDO::ATTR_DRIVER_NAME);\n        } elseif (\\is_string($pdoOrDsn) && str_contains($pdoOrDsn, '://')) {\n            $this->dsn = $this->buildDsnFromUrl($pdoOrDsn);\n        } else {\n            $this->dsn = $pdoOrDsn;\n        }\n\n        $this->table = $options['db_table'] ?? $this->table;\n        $this->idCol = $options['db_id_col'] ?? $this->idCol;\n        $this->dataCol = $options['db_data_col'] ?? $this->dataCol;\n        $this->lifetimeCol = $options['db_lifetime_col'] ?? $this->lifetimeCol;\n        $this->timeCol = $options['db_time_col'] ?? $this->timeCol;\n        $this->username = $options['db_username'] ?? $this->username;\n        $this->password = $options['db_password'] ?? $this->password;\n        $this->connectionOptions = $options['db_connection_options'] ?? $this->connectionOptions;","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/symfony/http-foundation/blob/5aea19cd678fa4140f6108406f1096de5e9ed6e4/Session/Storage/Handler/PdoSessionHandler.php#L146-L182","documentation":"PdoSessionHandler relies on PDO throwing exceptions so SQL errors during session read/write surface as exceptions rather than silent failures. When a \\PDO instance is passed with ATTR_ERRMODE set to anything other than ERRMODE_EXCEPTION, the constructor throws an InvalidArgumentException.","triggerScenarios":"Passing an existing \\PDO object whose error mode is ERRMODE_SILENT or ERRMODE_WARNING to new PdoSessionHandler($pdo).","commonSituations":"Reusing an application-wide PDO connection configured with legacy error modes; legacy codebases that set ERRMODE_SILENT globally; framework bootstrapping that resets PDO attributes after construction.","solutions":["Call $pdo->setAttribute(\\PDO::ATTR_ERRMODE, \\PDO::ERRMODE_EXCEPTION) before passing the PDO to PdoSessionHandler.","Configure the PDO connection factory used by the app to always set ERRMODE_EXCEPTION.","Alternatively pass a DSN string (or URL) plus options instead of a pre-built PDO instance, letting PdoSessionHandler configure error mode itself."],"exampleFix":"// before\n$pdo = new \\PDO($dsn, $user, $pass);\n$pdo->setAttribute(\\PDO::ATTR_ERRMODE, \\PDO::ERRMODE_SILENT);\n$handler = new PdoSessionHandler($pdo); // throws\n\n// after\n$pdo = new \\PDO($dsn, $user, $pass);\n$pdo->setAttribute(\\PDO::ATTR_ERRMODE, \\PDO::ERRMODE_EXCEPTION);\n$handler = new PdoSessionHandler($pdo);","handlingStrategy":"validation","validationCode":"if ($pdo instanceof \\PDO && \\PDO::ERRMODE_EXCEPTION !== $pdo->getAttribute(\\PDO::ATTR_ERRMODE)) {\n    $pdo->setAttribute(\\PDO::ATTR_ERRMODE, \\PDO::ERRMODE_EXCEPTION);\n}\n$handler = new PdoSessionHandler($pdo);","typeGuard":null,"tryCatchPattern":"try {\n    $handler = new PdoSessionHandler($pdo);\n} catch (\\InvalidArgumentException $e) {\n    // set ERRMODE_EXCEPTION on the PDO and retry construction\n}","preventionTips":["Always set PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION at connection creation.","Wrap PDO creation in a factory that enforces exception mode.","Pass a DSN string to PdoSessionHandler when you cannot control the PDO instance."],"tags":["pdo","database","sessions","configuration"],"backgroundTag":"invalid-config-value","analyzedSha":"5aea19cd678fa4140f6108406f1096de5e9ed6e4","analyzedAt":"2026-09-13T01:52:22.855Z","contentChangedAt":"2026-09-13T01:52:22.855Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}