Skip to content

Commit

Permalink
Throw exceptions rather than returning false to avoid PHP Warning: 's…
Browse files Browse the repository at this point in the history
…ession_start(): Failed to read session data: user (path: )'
  • Loading branch information
colinmollenhour committed Mar 21, 2023
1 parent 3e6ff3b commit 731f636
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions app/code/community/Cm/RedisSession/Model/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,13 @@ class Cm_RedisSession_Model_Session implements \Zend_Session_SaveHandler_Interfa

public function __construct($config = array())
{
try {
$this->sessionHandler = new \Cm\RedisSession\Handler(
new Cm_RedisSession_Model_Session_Config($config['session_name'] ?? 'default'),
new Cm_RedisSession_Model_Session_Logger(),
Mage::registry('controller')
&& Mage::app()->getFrontController()->getAction()
&& Mage::app()->getFrontController()->getAction()->getFlag('', self::FLAG_READ_ONLY)
);
} catch (\Cm\RedisSession\ConnectionFailedException $e) {
$this->handleException($e);
}
$this->sessionHandler = new \Cm\RedisSession\Handler(
new Cm_RedisSession_Model_Session_Config($config['session_name'] ?? 'default'),
new Cm_RedisSession_Model_Session_Logger(),
Mage::registry('controller')
&& Mage::app()->getFrontController()->getAction()
&& Mage::app()->getFrontController()->getAction()->getFlag('', self::FLAG_READ_ONLY)
);
}

public function setDieOnError(bool $flag): void
Expand Down Expand Up @@ -106,7 +102,6 @@ public function open($savePath, $sessionName)
return $this->sessionHandler->open($savePath, $sessionName);
} catch (Throwable $e) {
$this->handleException($e);
return false;
}
}

Expand All @@ -122,10 +117,9 @@ public function read($sessionId)
$data = $this->sessionHandler->read($sessionId);
self::$failedLockAttempts = $this->sessionHandler->getFailedLockAttempts();
return $data;
} catch (\Cm\RedisSession\ConcurrentConnectionsExceededException $e) {
} catch (Throwable $e) {
self::$failedLockAttempts = $this->sessionHandler->getFailedLockAttempts();
$this->handleException($e);
return false;
}
}

Expand Down Expand Up @@ -174,22 +168,23 @@ public function gc($maxLifeTime)
}

/**
* @param \Exception $e
* @param Throwable $e
* @return void
*/
protected function handleException(\Exception $e)
protected function handleException(Throwable $e)
{
if ($e instanceof \Cm\RedisSession\ConcurrentConnectionsExceededException) {
Mage::register('concurrent_connections_exceeded', true);
if (Mage::getConfig()->getNode('global/redis_session')->is('log_exceptions')) {
Mage::logException($e);
}
if ($this->dieOnError) {
if (Mage::getConfig()->getNode('global/redis_session')->is('log_exceptions')) {
Mage::logException($e);
}
require_once Mage::getBaseDir() . DS . 'errors' . DS . '503.php';
die();
}
} else if ($this->dieOnError) {
Mage::printException($e);
}
throw $e;
}
}

0 comments on commit 731f636

Please sign in to comment.