Merge "Revert "Introduce Special:RedirectExternal""
[lhc/web/wiklou.git] / includes / api / ApiMain.php
index 03d2952..d2a7db2 100644 (file)
@@ -534,7 +534,11 @@ class ApiMain extends ApiBase {
                        MediaWikiServices::getInstance()->getStatsdDataFactory()->timing(
                                'api.' . $this->mModule->getModuleName() . '.executeTiming', 1000 * $runTime
                        );
-               } catch ( Exception $e ) {
+               } catch ( Exception $e ) { // @todo Remove this block when HHVM is no longer supported
+                       $this->handleException( $e );
+                       $this->logRequest( microtime( true ) - $t, $e );
+                       $isError = true;
+               } catch ( Throwable $e ) {
                        $this->handleException( $e );
                        $this->logRequest( microtime( true ) - $t, $e );
                        $isError = true;
@@ -558,12 +562,12 @@ class ApiMain extends ApiBase {
         * Handle an exception as an API response
         *
         * @since 1.23
-        * @param Exception $e
+        * @param Exception|Throwable $e
         */
-       protected function handleException( Exception $e ) {
+       protected function handleException( $e ) {
                // T65145: Rollback any open database transactions
-               if ( !( $e instanceof ApiUsageException || $e instanceof UsageException ) ) {
-                       // UsageExceptions are intentional, so don't rollback if that's the case
+               if ( !$e instanceof ApiUsageException ) {
+                       // ApiUsageExceptions are intentional, so don't rollback if that's the case
                        MWExceptionHandler::rollbackMasterChangesAndLog( $e );
                }
 
@@ -600,18 +604,14 @@ class ApiMain extends ApiBase {
                        foreach ( $ex->getStatusValue()->getErrors() as $error ) {
                                try {
                                        $this->mPrinter->addWarning( $error );
-                               } catch ( Exception $ex2 ) {
+                               } catch ( Exception $ex2 ) { // @todo Remove this block when HHVM is no longer supported
+                                       // WTF?
+                                       $this->addWarning( $error );
+                               } catch ( Throwable $ex2 ) {
                                        // WTF?
                                        $this->addWarning( $error );
                                }
                        }
-               } catch ( UsageException $ex ) {
-                       // The error printer itself is failing. Try suppressing its request
-                       // parameters and redo.
-                       $failed = true;
-                       $this->addWarning(
-                               [ 'apiwarn-errorprinterfailed-ex', $ex->getMessage() ], 'errorprinterfailed'
-                       );
                }
                if ( $failed ) {
                        $this->mPrinter = null;
@@ -631,17 +631,20 @@ class ApiMain extends ApiBase {
         * friendly to clients. If it fails, it will rethrow the exception.
         *
         * @since 1.23
-        * @param Exception $e
-        * @throws Exception
+        * @param Exception|Throwable $e
+        * @throws Exception|Throwable
         */
-       public static function handleApiBeforeMainException( Exception $e ) {
+       public static function handleApiBeforeMainException( $e ) {
                ob_start();
 
                try {
                        $main = new self( RequestContext::getMain(), false );
                        $main->handleException( $e );
                        $main->logRequest( 0, $e );
-               } catch ( Exception $e2 ) {
+               } catch ( Exception $e2 ) { // @todo Remove this block when HHVM is no longer supported
+                       // Nope, even that didn't work. Punt.
+                       throw $e;
+               } catch ( Throwable $e2 ) {
                        // Nope, even that didn't work. Punt.
                        throw $e;
                }
@@ -1002,14 +1005,11 @@ class ApiMain extends ApiBase {
         * If an ApiUsageException, errors/warnings will be extracted from the
         * embedded StatusValue.
         *
-        * If a base UsageException, the getMessageArray() method will be used to
-        * extract the code and English message for a single error (no warnings).
-        *
         * Any other exception will be returned with a generic code and wrapper
         * text around the exception's (presumably English) message as a single
         * error (no warnings).
         *
-        * @param Exception $e
+        * @param Exception|Throwable $e
         * @param string $type 'error' or 'warning'
         * @return ApiMessage[]
         * @since 1.27
@@ -1022,13 +1022,6 @@ class ApiMain extends ApiBase {
                        }
                } elseif ( $type !== 'error' ) {
                        // None of the rest have any messages for non-error types
-               } elseif ( $e instanceof UsageException ) {
-                       // User entered incorrect parameters - generate error response
-                       $data = Wikimedia\quietCall( [ $e, 'getMessageArray' ] );
-                       $code = $data['code'];
-                       $info = $data['info'];
-                       unset( $data['code'], $data['info'] );
-                       $messages[] = new ApiRawMessage( [ '$1', $info ], $code, $data );
                } else {
                        // Something is seriously wrong
                        $config = $this->getConfig();
@@ -1054,7 +1047,7 @@ class ApiMain extends ApiBase {
 
        /**
         * Replace the result data with the information about an exception.
-        * @param Exception $e
+        * @param Exception|Throwable $e
         * @return string[] Error codes
         */
        protected function substituteResultWithError( $e ) {
@@ -1098,7 +1091,7 @@ class ApiMain extends ApiBase {
                } else {
                        $path = null;
                }
-               if ( $e instanceof ApiUsageException || $e instanceof UsageException ) {
+               if ( $e instanceof ApiUsageException ) {
                        $link = wfExpandUrl( wfScript( 'api' ) );
                        $result->addContentValue(
                                $path,
@@ -1609,7 +1602,7 @@ class ApiMain extends ApiBase {
        /**
         * Log the preceding request
         * @param float $time Time in seconds
-        * @param Exception|null $e Exception caught while processing the request
+        * @param Exception|Throwable|null $e Exception caught while processing the request
         */
        protected function logRequest( $time, $e = null ) {
                $request = $this->getRequest();