Replace deprecated MWException

Introduce new classes for checked exceptions, and use SPL exceptions
for the unchecked ones.

Bug: T328220
Change-Id: Ic44463e910911afe6395c38fd2f59b9bfa02a4f0
This commit is contained in:
Daimona Eaytoy 2023-06-09 02:01:46 +02:00
parent 7e54e8d7fd
commit d06bd18532
11 changed files with 53 additions and 40 deletions

View file

@ -2,7 +2,6 @@
namespace MediaWiki\Extension\Math\HookHandlers;
use FatalError;
use MediaWiki\Extension\Math\Hooks\HookRunner;
use MediaWiki\Extension\Math\MathConfig;
use MediaWiki\Extension\Math\MathMathML;
@ -15,7 +14,6 @@ use MediaWiki\Hook\ParserOptionsRegisterHook;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\User\UserOptionsLookup;
use MWException;
use Parser;
use ParserOptions;
@ -115,8 +113,6 @@ class ParserHooksHandler implements
* @param MathRenderer $renderer
* @param Parser $parser
* @return string
* @throws FatalError
* @throws MWException
*/
private function mathPostTagHook( MathRenderer $renderer, Parser $parser ) {
$checkResult = $renderer->checkTeX();

View file

@ -4,8 +4,8 @@ namespace MediaWiki\Extension\Math\InputCheck;
use MediaWiki\Http\HttpRequestFactory;
use Message;
use MWException;
use Psr\Log\LoggerInterface;
use RuntimeException;
use WANObjectCache;
class MathoidChecker extends BaseChecker {
@ -83,7 +83,6 @@ class MathoidChecker extends BaseChecker {
/**
* @return array
* @throws MWException
*/
public function runCheck(): array {
$url = "{$this->url}/texvcinfo";
@ -100,7 +99,7 @@ class MathoidChecker extends BaseChecker {
if ( in_array( $statusCode, self::EXPECTED_RETURN_CODES, true ) ) {
return [ $statusCode, $req->getContent() ];
}
$e = new MWException( 'Mathoid check returned unexpected error code.' );
$e = new RuntimeException( 'Mathoid check returned unexpected error code.' );
$this->logger->error( 'Mathoid check endpoint "{url}" returned ' .
'HTTP status code "{statusCode}" for post data "{postData}": {exception}.',
[

View file

@ -0,0 +1,8 @@
<?php
namespace MediaWiki\Extension\Math;
use Exception;
class InvalidTeXException extends Exception {
}

View file

@ -466,9 +466,7 @@ class MathMathML extends MathRenderer {
$hyperlink = null;
if ( isset( $this->params['qid'] ) && preg_match( '/Q\d+/', $this->params['qid'] ) ) {
$attribs['data-qid'] = $this->params['qid'];
// TODO: SpecialPage::getTitleFor uses the depcrated method Title::newFromTitleValue and
// declares a never thrown MWException. Once this SpecialPage is updated, update the next line.
$titleObj = Title::newFromLinkTarget( SpecialPage::getTitleValueFor( 'MathWikibase' ) );
$titleObj = SpecialPage::getTitleFor( 'MathWikibase' );
$hyperlink = $titleObj->getLocalURL( [ 'qid' => $this->params['qid'] ] );
}
$output = Html::openElement( $element, $attribs );

View file

@ -5,7 +5,7 @@ namespace MediaWiki\Extension\Math;
use Exception;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MWException;
use RuntimeException;
use stdClass;
/**
@ -16,7 +16,6 @@ class MathMathMLCli extends MathMathML {
/**
* @param MathRenderer[] $renderers
* @return bool
* @throws MWException
*/
public static function batchEvaluate( array $renderers ) {
$req = [];
@ -118,7 +117,6 @@ class MathMathMLCli extends MathMathML {
* @param mixed $req request
* @param int|null &$exitCode
* @return mixed
* @throws MWException
*/
private static function evaluateWithCli( $req, &$exitCode = null ) {
global $wgMathoidCli;
@ -135,11 +133,11 @@ class MathMathMLCli extends MathMathML {
'conf' => var_export( $wgMathoidCli, true ),
'res' => var_export( $result, true ),
] );
throw new MWException( "Failed to execute Mathoid cli '$wgMathoidCli[0]', reason: $errorMsg" );
throw new RuntimeException( "Failed to execute Mathoid cli '$wgMathoidCli[0]', reason: $errorMsg" );
}
$res = json_decode( $result->getStdout() );
if ( !$res ) {
throw new MWException( "Mathoid cli response '$res' is no valid JSON file." );
throw new RuntimeException( "Mathoid cli response '$res' is no valid JSON file." );
}
return $res;

View file

@ -0,0 +1,8 @@
<?php
namespace MediaWiki\Extension\Math;
use Exception;
class MathRestbaseException extends Exception {
}

View file

@ -11,7 +11,6 @@ namespace MediaWiki\Extension\Math;
use Exception;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MWException;
use Psr\Log\LoggerInterface;
use stdClass;
@ -103,7 +102,7 @@ class MathRestbaseInterface {
/**
* @return string MathML code
* @throws MWException
* @throws MathRestbaseException
*/
public function getMathML() {
if ( !$this->mml ) {
@ -112,6 +111,11 @@ class MathRestbaseInterface {
return $this->mml;
}
/**
* @param string $type
* @return string
* @throws MathRestbaseException
*/
private function getContent( $type ) {
$request = $this->getContentRequest( $type );
$multiHttpClient = $this->getMultiHttpClient();
@ -119,10 +123,13 @@ class MathRestbaseInterface {
return $this->evaluateContentResponse( $type, $response, $request );
}
/**
* @throws InvalidTeXException
*/
private function calculateHash() {
if ( !$this->hash ) {
if ( !$this->checkTeX() ) {
throw new MWException( "TeX input is invalid." );
throw new InvalidTeXException( "TeX input is invalid." );
}
}
}
@ -212,7 +219,6 @@ class MathRestbaseInterface {
* @param string $path
* @param bool|true $internal
* @return string
* @throws MWException
*/
public function getUrl( $path, $internal = true ) {
global $wgMathInternalRestbaseURL, $wgMathFullRestbaseURL;
@ -230,6 +236,10 @@ class MathRestbaseInterface {
return $this->logger;
}
/**
* @return string
* @throws MathRestbaseException
*/
public function getSvg() {
return $this->getContent( 'svg' );
}
@ -270,7 +280,7 @@ class MathRestbaseInterface {
/**
* Gets a publicly accessible link to the generated SVG image.
* @return string
* @throws MWException
* @throws InvalidTeXException
*/
public function getFullSvgUrl() {
$this->calculateHash();
@ -280,7 +290,7 @@ class MathRestbaseInterface {
/**
* Gets a publicly accessible link to the generated SVG image.
* @return string
* @throws MWException
* @throws InvalidTeXException
*/
public function getFullPngUrl() {
$this->calculateHash();
@ -345,7 +355,6 @@ class MathRestbaseInterface {
/**
* @return array
* @throws MWException
*/
public function getCheckRequest() {
return [
@ -396,7 +405,7 @@ class MathRestbaseInterface {
/**
* @param string $type
* @return array
* @throws MWException
* @throws InvalidTeXException
*/
private function getContentRequest( $type ) {
$this->calculateHash();
@ -418,7 +427,7 @@ class MathRestbaseInterface {
* @param array $response
* @param array $request
* @return string
* @throws MWException
* @throws MathRestbaseException
*/
private function evaluateContentResponse( $type, array $response, array $request ) {
if ( $response['code'] === 200 ) {
@ -441,7 +450,7 @@ class MathRestbaseInterface {
/**
* @param string $type
* @param string $body
* @throws MWException
* @throws MathRestbaseException
* @return never
*/
public static function throwContentError( $type, $body ) {
@ -454,6 +463,6 @@ class MathRestbaseInterface {
$detail = $json->detail;
}
}
throw new MWException( "Cannot get $type. $detail" );
throw new MathRestbaseException( "Cannot get $type. $detail" );
}
}

View file

@ -5,8 +5,6 @@ namespace MediaWiki\Extension\Math;
use ExtensionRegistry;
use MediaWiki\Extension\Math\Render\RendererFactory;
use MediaWiki\Logger\LoggerFactory;
use MWException;
use PermissionsError;
use Psr\Log\LoggerInterface;
use SpecialPage;
@ -41,9 +39,6 @@ class SpecialMathStatus extends SpecialPage {
/**
* @param null|string $query
*
* @throws MWException
* @throws PermissionsError
*/
public function execute( $query ) {
$this->setHeaders();

View file

@ -4,7 +4,7 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC;
use MWException;
use InvalidArgumentException;
class TexUtil {
private static $instance = null;
@ -82,7 +82,6 @@ class TexUtil {
* @param mixed $func
* @param mixed $params
* @return false|mixed
* @throws MWException
*/
public function __call( $func, $params ) {
if ( array_key_exists( $func, $this->baseElements ) ) {
@ -93,7 +92,7 @@ class TexUtil {
return false;
}
} else {
throw new MWException( "Function not defined in json " . $func );
throw new InvalidArgumentException( "Function not defined in json " . $func );
}
}

View file

@ -8,6 +8,7 @@ use MediaWiki\Extension\Math\MathMathML;
use MediaWiki\Http\HttpRequestFactory;
use MediaWikiIntegrationTestCase;
use MockHttpTrait;
use RuntimeException;
use WANObjectCache;
class MathoidCheckerTest extends MediaWikiIntegrationTestCase {
@ -74,7 +75,7 @@ class MathoidCheckerTest extends MediaWikiIntegrationTestCase {
$this->setService( 'MainWANObjectCache', $fakeWAN );
$this->setFakeRequest( 401, false );
$checker = $this->getMathoidChecker();
$this->expectException( 'MWException' );
$this->expectException( RuntimeException::class );
$checker->getCheckResponse();
}

View file

@ -1,5 +1,7 @@
<?php
use MediaWiki\Extension\Math\InvalidTeXException;
use MediaWiki\Extension\Math\MathRestbaseException;
use MediaWiki\Extension\Math\MathRestbaseInterface;
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
@ -117,7 +119,7 @@ class MathRestbaseInterfaceTest extends MediaWikiIntegrationTestCase {
$input = '\\sin\\newcommand';
$rbi = new MathRestbaseInterface( $input );
$this->expectException( MWException::class );
$this->expectException( InvalidTeXException::class );
$this->expectExceptionMessage( 'TeX input is invalid.' );
$rbi->getMathML();
}
@ -127,7 +129,7 @@ class MathRestbaseInterfaceTest extends MediaWikiIntegrationTestCase {
$input = '\\sin\\newcommand';
$rbi = new MathRestbaseInterface( $input );
$this->expectException( MWException::class );
$this->expectException( InvalidTeXException::class );
$this->expectExceptionMessage( 'TeX input is invalid.' );
$rbi->getFullSvgUrl();
}
@ -139,7 +141,7 @@ class MathRestbaseInterfaceTest extends MediaWikiIntegrationTestCase {
public function testLateError() {
// phpcs:ignore Generic.Files.LineLength.TooLong
$input = '{"type":"https://mediawiki.org/wiki/HyperSwitch/errors/bad_request","title":"Bad Request","method":"POST","detail":["TeX parse error: Missing close brace"],"uri":"/complete"}';
$this->expectException( MWException::class );
$this->expectException( MathRestbaseException::class );
$this->expectExceptionMessage( 'Cannot get mml. TeX parse error: Missing close brace' );
MathRestbaseInterface::throwContentError( 'mml', $input );
}
@ -151,14 +153,14 @@ class MathRestbaseInterfaceTest extends MediaWikiIntegrationTestCase {
public function testLateErrorString() {
// phpcs:ignore Generic.Files.LineLength.TooLong
$input = '{"type":"https://mediawiki.org/wiki/HyperSwitch/errors/bad_request","title":"Bad Request","method":"POST","detail": "TeX parse error: Missing close brace","uri":"/complete"}';
$this->expectException( MWException::class );
$this->expectException( MathRestbaseException::class );
$this->expectExceptionMessage( 'Cannot get mml. TeX parse error: Missing close brace' );
MathRestbaseInterface::throwContentError( 'mml', $input );
}
public function testLateErrorNoDetail() {
$input = '';
$this->expectException( MWException::class );
$this->expectException( MathRestbaseException::class );
$this->expectExceptionMessage( 'Cannot get mml. Server problem.' );
MathRestbaseInterface::throwContentError( 'mml', $input );
}