diff --git "a/include/class-mw\\API\\ArticleExistsException.php" "b/include/class-mw\\API\\ArticleExistsException.php" index 179a4de..95736d7 100644 --- "a/include/class-mw\\API\\ArticleExistsException.php" +++ "b/include/class-mw\\API\\ArticleExistsException.php" @@ -1,24 +1,28 @@ . # MediaWiki API namespace mw\API; /** - * Specific API exception class for the 'articleexists' exception + * Specific API exception class for the 'articleexists' error code */ -class ArticleExistsException extends Exception {} +class ArticleExistsException extends Exception { + + const API_ERROR_CODE = 'articleexists'; + +} diff --git "a/include/class-mw\\API\\BadTokenException.php" "b/include/class-mw\\API\\BadTokenException.php" index 53072a1..0780b1d 100644 --- "a/include/class-mw\\API\\BadTokenException.php" +++ "b/include/class-mw\\API\\BadTokenException.php" @@ -1,24 +1,28 @@ . # MediaWiki API namespace mw\API; /** - * Specific API exception class for the 'bad-token' exception + * Specific API exception class for the 'bad-token' error code */ -class BadTokenException extends Exception {} +class BadTokenException extends Exception { + + const API_ERROR_CODE = 'bad-token'; + +} diff --git "a/include/class-mw\\API\\Exception.php" "b/include/class-mw\\API\\Exception.php" index b7eced4..4a695f6 100644 --- "a/include/class-mw\\API\\Exception.php" +++ "b/include/class-mw\\API\\Exception.php" @@ -1,108 +1,102 @@ . # MediaWiki API namespace mw\API; /** * Generic API exception class */ class Exception extends \Exception { /** * API error object * * @var object */ private $apiError; /** * Constructor * * @param $api_error object The complete error * @param $code int * @param $previous object */ public function __construct( $api_error, $code = 0, Exception $previous = null ) { $this->apiError = $api_error; parent::__construct( sprintf( "API error code: '%s' info: '%s'", $this->getApiErrorCode(), $this->getApiErrorInfo() ), $code, $previous ); } /** * Create a specific exception from a generic API error * * @param $api_error object The complete error * @return self */ public static function createFromApiError( $api_error ) { - $exception = new self( $api_error ); + $known_exceptions = [ + BadTokenException ::class, + MaxLagException ::class, + ArticleExistsException::class, + MissingTitleException ::class, + ProtectedPageException::class, + ]; $code = $exception->getApiErrorCode(); - switch( $code ) { - case 'bad-token': - $exception = new BadTokenException( $api_error ); - break; - case 'maxlag': - $exception = new MaxLagException( $api_error ); - break; - case 'articleexists': - $exception = new ArticleExistsException( $api_error ); - break; - case 'missingtitle': - $exception = new MissingTitleException( $api_error ); - break; - case 'protectedpage': - $exception = new ProtectedPageException( $api_error ); - break; + foreach( $known_exceptions as $exception ) { + if( $exception::API_ERROR_CODE === $code ) { + return new $exception( $api_error ); + } } - return $exception; + return new self( $api_error ); } /** * Get the complete error object * * @return object */ public function getApiError() { return $this->apiError; } /** * Get the API error code * * @return string */ public function getApiErrorCode() { return $this->getApiError()->code; } /** * Get the API error info * * @return string */ public function getApiErrorInfo() { return $this->getApiError()->info; } } diff --git "a/include/class-mw\\API\\MaxLagException.php" "b/include/class-mw\\API\\MaxLagException.php" index 43351df..b52e348 100644 --- "a/include/class-mw\\API\\MaxLagException.php" +++ "b/include/class-mw\\API\\MaxLagException.php" @@ -1,24 +1,28 @@ . # MediaWiki API namespace mw\API; /** - * Specific API exception class for the 'maxlag' exception + * Specific API exception class for the 'maxlag' error code */ -class MaxLagException extends Exception {} +class MaxLagException extends Exception { + + const API_ERROR_CODE = 'maxlag'; + +} diff --git "a/include/class-mw\\API\\MissingTitleException.php" "b/include/class-mw\\API\\MissingTitleException.php" index 6df5158..03d075a 100644 --- "a/include/class-mw\\API\\MissingTitleException.php" +++ "b/include/class-mw\\API\\MissingTitleException.php" @@ -1,24 +1,28 @@ . # MediaWiki API namespace mw\API; /** - * Specific API exception class for the 'missingtitle' exception + * Specific API exception class for the 'missingtitle' error code */ -class MissingTitleException extends Exception {} +class MissingTitleException extends Exception { + + const API_ERROR_CODE = 'missingtitle'; + +} diff --git "a/include/class-mw\\API\\ProtectedPageException.php" "b/include/class-mw\\API\\ProtectedPageException.php" index cefe2fb..e7fb90b 100644 --- "a/include/class-mw\\API\\ProtectedPageException.php" +++ "b/include/class-mw\\API\\ProtectedPageException.php" @@ -1,24 +1,28 @@ . # MediaWiki API namespace mw\API; /** * Specific API exception class for the 'protectedpage' error code */ -class ProtectedPageException extends Exception {} +class ProtectedPageException extends Exception { + + const API_ERROR_CODE = 'protectedpage'; + +}