Instead of setting a custom property and checking via isset + count.
Change-Id: I087eeb2eee414218bbd6023ad1703fde22292281
// 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() ) {
}
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;
--- /dev/null
+<?php
+
+namespace MediaWiki\Session;
+
+/**
+ * OverflowException specific to the SessionManager, used when the request had multiple possible
+ * sessions tied for top priority.
+ *
+ * @since 1.34
+ */
+class SessionOverflowException extends \OverflowException {
+ /** @var SessionInfo[] */
+ private $sessionInfos;
+
+ /**
+ * @param SessionInfo[] $sessionInfos Must have at least two elements
+ * @param string $msg
+ * @throws \InvalidArgumentException If $sessionInfos has less than 2 elements
+ */
+ function __construct( array $sessionInfos, $msg ) {
+ if ( count( $sessionInfos ) < 2 ) {
+ throw new \InvalidArgumentException( 'Expected at least two SessionInfo objects.' );
+ }
+ parent::__construct( $msg );
+ $this->sessionInfos = $sessionInfos;
+ }
+
+ /**
+ * @return SessionInfo[]
+ */
+ public function getSessionInfos() : array {
+ return $this->sessionInfos;
+ }
+}
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 );