Avoid try…catch where if…else will do when using LanguageFactory

Needed for I869af06896b9757af18488b916211c5a41a8c563, where I am
trying to change LanguageFactory in MediaWiki core not to use
MWException.

Language::isSupportedLanguage() does not actually throw,
so this sigh can be one of relief.

Change-Id: I3079d8e18d88a4a26c2f2b09dccd4beea06678ee
This commit is contained in:
Bartosz Dziewoński 2022-11-18 20:37:45 +01:00
parent e415ed564c
commit 46bb5b0ac4
2 changed files with 14 additions and 24 deletions

View file

@ -7,7 +7,6 @@ use DateTimeZone;
use Exception;
use Language;
use MediaWiki\MediaWikiServices;
use MWException;
use MWTimestamp;
use Title;
use User;
@ -82,12 +81,7 @@ class LanguageLibrary extends LibraryBase {
*/
public function isSupportedLanguage( $code ) {
$this->checkType( 'isSupportedLanguage', 1, $code, 'string' );
try {
// There's no good reason this should throw, but it does. Sigh.
return [ Language::isSupportedLanguage( $code ) ];
} catch ( MWException $ex ) {
return [ false ];
}
return [ Language::isSupportedLanguage( $code ) ];
}
/**
@ -180,9 +174,9 @@ class LanguageLibrary extends LibraryBase {
if ( count( $this->langCache ) > $this->maxLangCacheSize ) {
throw new LuaError( 'too many language codes requested' );
}
try {
if ( Language::isValidCode( $code ) ) {
$this->langCache[$code] = Language::factory( $code );
} catch ( MWException $ex ) {
} else {
throw new LuaError( "language code '$code' is invalid" );
}
}

View file

@ -2,9 +2,9 @@
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
use Language;
use MediaWiki\MediaWikiServices;
use Message;
use MWException;
use RawMessage;
class MessageLibrary extends LibraryBase {
@ -43,8 +43,12 @@ class MessageLibrary extends LibraryBase {
} else {
$msg = Message::newFallbackSequence( $data['keys'] );
}
$msg->inLanguage( $data['lang'] )
->useDatabase( $data['useDB'] );
if ( is_string( $data['lang'] ) && !Language::isValidCode( $data['lang'] ) ) {
throw new LuaError( "language code '{$data['lang']}' is invalid" );
} else {
$msg->inLanguage( $data['lang'] );
}
$msg->useDatabase( $data['useDB'] );
if ( $setParams ) {
$msg->params( array_values( $data['params'] ) );
}
@ -58,12 +62,8 @@ class MessageLibrary extends LibraryBase {
* @return string[]
*/
public function messagePlain( $data ) {
try {
$msg = $this->makeMessage( $data, true );
return [ $msg->plain() ];
} catch ( MWException $ex ) {
throw new LuaError( "msg:plain() failed (" . $ex->getMessage() . ")" );
}
$msg = $this->makeMessage( $data, true );
return [ $msg->plain() ];
}
/**
@ -78,11 +78,7 @@ class MessageLibrary extends LibraryBase {
throw new LuaError( "invalid what for 'messageCheck'" );
}
try {
$msg = $this->makeMessage( $data, false );
return [ call_user_func( [ $msg, $what ] ) ];
} catch ( MWException $ex ) {
throw new LuaError( "msg:$what() failed (" . $ex->getMessage() . ")" );
}
$msg = $this->makeMessage( $data, false );
return [ call_user_func( [ $msg, $what ] ) ];
}
}