Skip to content

Commit 3d67aba

Browse files
committed
feature #21421 Use proper error message when session write fails #20807 (digilist)
This PR was submitted for the 2.8 branch but it was merged into the 3.3-dev branch instead (closes #21421). Discussion ---------- Use proper error message when session write fails #20807 This improves the error message that is thrown if a session write fails (see #20807) As there was no way to get the actual handler, I introduced a method on the SessionHandlerProxy. I hope that's okay, otherwise please give me a hint on what to do. | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20807 | License | MIT Commits ------- c7a44be Use proper error message when session write fails #20807
2 parents c6e1a49 + c7a44be commit 3d67aba

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpFoundation\Session\Storage;
1313

14+
use Symfony\Component\Debug\Exception\ContextErrorException;
1415
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
1516
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
1617
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
@@ -208,7 +209,28 @@ public function regenerate($destroy = false, $lifetime = null)
208209
*/
209210
public function save()
210211
{
211-
session_write_close();
212+
// Register custom error handler to catch a possible failure warning during session write
213+
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
214+
throw new ContextErrorException($errstr, $errno, E_WARNING, $errfile, $errline, $errcontext);
215+
}, E_WARNING);
216+
217+
try {
218+
session_write_close();
219+
restore_error_handler();
220+
} catch (ContextErrorException $e) {
221+
// The default PHP error message is not very helpful, as it does not give any information on the current save handler.
222+
// Therefore, we catch this error and trigger a warning with a better error message
223+
$handler = $this->getSaveHandler();
224+
if ($handler instanceof SessionHandlerProxy) {
225+
$handler = $handler->getHandler();
226+
}
227+
228+
restore_error_handler();
229+
trigger_error(sprintf(
230+
'session_write_close(): Failed to write session data with %s handler',
231+
get_class($handler)
232+
), E_USER_WARNING);
233+
}
212234

213235
$this->closed = true;
214236
$this->started = false;

src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public function __construct(\SessionHandlerInterface $handler)
3535
$this->saveHandlerName = $this->wrapper ? ini_get('session.save_handler') : 'user';
3636
}
3737

38+
/**
39+
* @return \SessionHandlerInterface
40+
*/
41+
public function getHandler()
42+
{
43+
return $this->handler;
44+
}
45+
3846
// \SessionHandlerInterface
3947

4048
/**

0 commit comments

Comments
 (0)