From: Brad Jorsch Date: Tue, 23 Jul 2019 14:23:07 +0000 (-0400) Subject: API: Only take HTTP code from ApiUsageException X-Git-Tag: 1.34.0-rc.0~873^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22lang_raccourcis%22%2C%22module=%24nom_module%22%29%20.%20%22?a=commitdiff_plain;h=acb2e15615a5cc792b08dd62494dd0608b1a9d33;p=lhc%2Fweb%2Fwiklou.git API: Only take HTTP code from ApiUsageException Codes set on other Exception types are unlikely to be intended as HTTP codes. Bug: T228758 Change-Id: Ia6a53cb621f87ff97d5f16215a1b09ae11ca8f53 --- diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index a77136de1b..8389b24c9c 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -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 ); } }