From f3262ed390f15427baad7e1470fc4ea6783ec198 Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Sat, 7 Sep 2019 16:21:09 +0200 Subject: [PATCH] Add a dedicated Exception for SessionOverflow Instead of setting a custom property and checking via isset + count. Change-Id: I087eeb2eee414218bbd6023ad1703fde22292281 --- includes/Setup.php | 25 ++++++-------- includes/session/SessionManager.php | 5 ++- includes/session/SessionOverflowException.php | 34 +++++++++++++++++++ .../includes/session/SessionManagerTest.php | 8 ++--- 4 files changed, 50 insertions(+), 22 deletions(-) create mode 100644 includes/session/SessionOverflowException.php diff --git a/includes/Setup.php b/includes/Setup.php index 39f0c8116f..518531aacf 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -808,22 +808,17 @@ if ( !defined( 'MW_NO_SESSION' ) && !$wgCommandLineMode ) { // Initialize the session try { $session = MediaWiki\Session\SessionManager::getGlobalSession(); - } catch ( OverflowException $ex ) { - if ( isset( $ex->sessionInfos ) && count( $ex->sessionInfos ) >= 2 ) { - // The exception is because the request had multiple possible - // sessions tied for top priority. Report this to the user. - $list = []; - foreach ( $ex->sessionInfos as $info ) { - $list[] = $info->getProvider()->describe( $wgContLang ); - } - $list = $wgContLang->listToText( $list ); - throw new HttpError( 400, - Message::newFromKey( 'sessionmanager-tie', $list )->inLanguage( $wgContLang )->plain() - ); + } catch ( MediaWiki\Session\SessionOverflowException $ex ) { + // The exception is because the request had multiple possible + // sessions tied for top priority. Report this to the user. + $list = []; + foreach ( $ex->getSessionInfos() as $info ) { + $list[] = $info->getProvider()->describe( $wgContLang ); } - - // Not the one we want, rethrow - throw $ex; + $list = $wgContLang->listToText( $list ); + throw new HttpError( 400, + Message::newFromKey( 'sessionmanager-tie', $list )->inLanguage( $wgContLang )->plain() + ); } if ( $session->isPersistent() ) { diff --git a/includes/session/SessionManager.php b/includes/session/SessionManager.php index 09cdf72f46..a3380ff82f 100644 --- a/includes/session/SessionManager.php +++ b/includes/session/SessionManager.php @@ -505,11 +505,10 @@ final class SessionManager implements SessionManagerInterface { } if ( count( $retInfos ) > 1 ) { - $ex = new \OverflowException( + throw new SessionOverflowException( + $retInfos, 'Multiple sessions for this request tied for top priority: ' . implode( ', ', $retInfos ) ); - $ex->sessionInfos = $retInfos; - throw $ex; } return $retInfos ? $retInfos[0] : null; diff --git a/includes/session/SessionOverflowException.php b/includes/session/SessionOverflowException.php new file mode 100644 index 0000000000..2a5ed2b80a --- /dev/null +++ b/includes/session/SessionOverflowException.php @@ -0,0 +1,34 @@ +sessionInfos = $sessionInfos; + } + + /** + * @return SessionInfo[] + */ + public function getSessionInfos() : array { + return $this->sessionInfos; + } +} diff --git a/tests/phpunit/includes/session/SessionManagerTest.php b/tests/phpunit/includes/session/SessionManagerTest.php index cd0867d2ef..b2f525d741 100644 --- a/tests/phpunit/includes/session/SessionManagerTest.php +++ b/tests/phpunit/includes/session/SessionManagerTest.php @@ -259,14 +259,14 @@ class SessionManagerTest extends MediaWikiTestCase { try { $manager->getSessionForRequest( $request ); $this->fail( 'Expcected exception not thrown' ); - } catch ( \OverflowException $ex ) { + } catch ( SessionOverflowException $ex ) { $this->assertStringStartsWith( 'Multiple sessions for this request tied for top priority: ', $ex->getMessage() ); - $this->assertCount( 2, $ex->sessionInfos ); - $this->assertContains( $request->info1, $ex->sessionInfos ); - $this->assertContains( $request->info2, $ex->sessionInfos ); + $this->assertCount( 2, $ex->getSessionInfos() ); + $this->assertContains( $request->info1, $ex->getSessionInfos() ); + $this->assertContains( $request->info2, $ex->getSessionInfos() ); } $this->assertFalse( $request->unpersist1 ); $this->assertFalse( $request->unpersist2 ); -- 2.20.1