'MediaWiki\\Session\\BotPasswordSessionProvider' => __DIR__ . '/includes/session/BotPasswordSessionProvider.php',
'MediaWiki\\Session\\CookieSessionProvider' => __DIR__ . '/includes/session/CookieSessionProvider.php',
'MediaWiki\\Session\\ImmutableSessionProviderWithCookie' => __DIR__ . '/includes/session/ImmutableSessionProviderWithCookie.php',
+ 'MediaWiki\\Session\\MetadataMergeException' => __DIR__ . '/includes/session/MetadataMergeException.php',
'MediaWiki\\Session\\PHPSessionHandler' => __DIR__ . '/includes/session/PHPSessionHandler.php',
'MediaWiki\\Session\\Session' => __DIR__ . '/includes/session/Session.php',
'MediaWiki\\Session\\SessionBackend' => __DIR__ . '/includes/session/SessionBackend.php',
--- /dev/null
+<?php
+/**
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Session
+ */
+
+namespace MediaWiki\Session;
+
+use UnexpectedValueException;
+
+/**
+ * Subclass of UnexpectedValueException that can be annotated with additional
+ * data for debug logging.
+ *
+ * @author Bryan Davis <bd808@wikimedia.org>
+ * @copyright © 2016 Bryan Davis and Wikimedia Foundation.
+ * @since 1.27
+ */
+class MetadataMergeException extends UnexpectedValueException {
+ /** @var array $context */
+ protected $context;
+
+ /**
+ * @param string $message
+ * @param int $code
+ * @param Exception|null $previous
+ * @param array $context Additional context data
+ */
+ public function __construct(
+ $message = '',
+ $code = 0,
+ Exception $previous = null,
+ array $context = []
+ ) {
+ parent::__construct( $message, $code, $previous );
+ $this->context = $context;
+ }
+
+ /**
+ * Get context data.
+ * @return array
+ */
+ public function getContext() {
+ return $this->context;
+ }
+
+ /**
+ * Set context data.
+ * @param array $context
+ */
+ public function setContext( array $context ) {
+ $this->context = $context;
+ }
+}
if ( $newProviderMetadata !== $providerMetadata ) {
$newParams['metadata'] = $newProviderMetadata;
}
- } catch ( \UnexpectedValueException $ex ) {
+ } catch ( MetadataMergeException $ex ) {
$this->logger->warning(
'Session "{session}": Metadata merge failed: {exception}',
array(
'session' => $info,
'exception' => $ex,
- ) );
+ ) + $ex->getContext()
+ );
return false;
}
}
* @param array $savedMetadata Saved provider metadata
* @param array $providedMetadata Provided provider metadata
* @return array Resulting metadata
- * @throws \UnexpectedValueException If the metadata cannot be merged
+ * @throws MetadataMergeException If the metadata cannot be merged
*/
public function mergeMetadata( array $savedMetadata, array $providedMetadata ) {
foreach ( $providedMetadata as $k => $v ) {
if ( array_key_exists( $k, $savedMetadata ) && $savedMetadata[$k] !== $v ) {
- throw new \UnexpectedValueException( "Key \"$k\" changed" );
+ $e = new MetadataMergeException( "Key \"$k\" changed" );
+ $e->setContext( [
+ 'old_value' => $savedMetadata[$k],
+ 'new_value' => $v,
+ ] );
+ throw $e;
}
}
return $providedMetadata;
$provider->expects( $this->any() )->method( 'mergeMetadata' )
->will( $this->returnCallback( function ( $a, $b ) {
if ( $b === array( 'Throw' ) ) {
- throw new \UnexpectedValueException( 'no merge!' );
+ throw new MetadataMergeException( 'no merge!' );
}
return array( 'Merged' );
} ) );
array( 'bar' => 2, 'baz' => '3' )
);
$this->fail( 'Expected exception not thrown' );
- } catch ( \UnexpectedValueException $ex ) {
+ } catch ( MetadataMergeException $ex ) {
$this->assertSame( 'Key "baz" changed', $ex->getMessage() );
+ $this->assertSame(
+ [ 'old_value' => 3, 'new_value' => '3' ], $ex->getContext() );
}
$res = $provider->mergeMetadata(