API: Only take HTTP code from ApiUsageException
authorBrad Jorsch <bjorsch@wikimedia.org>
Tue, 23 Jul 2019 14:23:07 +0000 (10:23 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Tue, 23 Jul 2019 14:24:18 +0000 (10:24 -0400)
Codes set on other Exception types are unlikely to be intended as HTTP
codes.

Bug: T228758
Change-Id: Ia6a53cb621f87ff97d5f16215a1b09ae11ca8f53

includes/api/ApiMain.php

index a77136d..8389b24 100644 (file)
@@ -593,9 +593,13 @@ class ApiMain extends ApiBase {
                // Printer may not be initialized if the extractRequestParams() fails for the main module
                $this->createErrorPrinter();
 
+               // Get desired HTTP code from an ApiUsageException. Don't use codes from other
+               // exception types, as they are unlikely to be intended as an HTTP code.
+               $httpCode = $e instanceof ApiUsageException ? $e->getCode() : 0;
+
                $failed = false;
                try {
-                       $this->printResult( $e->getCode() );
+                       $this->printResult( $httpCode );
                } catch ( ApiUsageException $ex ) {
                        // The error printer itself is failing. Try suppressing its request
                        // parameters and redo.
@@ -617,10 +621,10 @@ class ApiMain extends ApiBase {
                        $this->mPrinter = null;
                        $this->createErrorPrinter();
                        $this->mPrinter->forceDefaultParams();
-                       if ( $e->getCode() ) {
+                       if ( $httpCode ) {
                                $response->statusHeader( 200 ); // Reset in case the fallback doesn't want a non-200
                        }
-                       $this->printResult( $e->getCode() );
+                       $this->printResult( $httpCode );
                }
        }