Skip to content

Commit 2fe425b

Browse files
committed
bug symfony#28562 [HttpFoundation] fix hidding warnings from session handlers (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpFoundation] fix hidding warnings from session handlers | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#28467 | License | MIT | Doc PR | - The current logic is too greedy. Commits ------- f405b4d [HttpFoundation] fix hidding warnings from session handlers
2 parents 9bc774c + f405b4d commit 2fe425b

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -230,29 +230,22 @@ public function save()
230230
unset($_SESSION[$key]);
231231
}
232232

233-
// Register custom error handler to catch a possible failure warning during session write
234-
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
235-
throw new \ErrorException($errstr, $errno, E_WARNING, $errfile, $errline);
236-
}, E_WARNING);
233+
// Register error handler to add information about the current save handler
234+
$previousHandler = set_error_handler(function ($type, $msg, $file, $line) use (&$previousHandler) {
235+
if (E_WARNING === $type && 0 === strpos($msg, 'session_write_close():')) {
236+
$handler = $this->saveHandler instanceof SessionHandlerProxy ? $this->saveHandler->getHandler() : $this->saveHandler;
237+
$msg = sprintf('session_write_close(): Failed to write session data with "%s" handler', \get_class($handler));
238+
}
239+
240+
return $previousHandler ? $previousHandler($type, $msg, $file, $line) : false;
241+
});
237242

238243
try {
239-
$e = null;
240244
session_write_close();
241-
} catch (\ErrorException $e) {
242245
} finally {
243246
restore_error_handler();
244247
$_SESSION = $session;
245248
}
246-
if (null !== $e) {
247-
// The default PHP error message is not very helpful, as it does not give any information on the current save handler.
248-
// Therefore, we catch this error and trigger a warning with a better error message
249-
$handler = $this->getSaveHandler();
250-
if ($handler instanceof SessionHandlerProxy) {
251-
$handler = $handler->getHandler();
252-
}
253-
254-
trigger_error(sprintf('session_write_close(): Failed to write session data with %s handler', \get_class($handler)), E_USER_WARNING);
255-
}
256249

257250
$this->closed = true;
258251
$this->started = false;

0 commit comments

Comments
 (0)