From acb2e15615a5cc792b08dd62494dd0608b1a9d33 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Tue, 23 Jul 2019 10:23:07 -0400 Subject: [PATCH] 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 --- includes/api/ApiMain.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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 ); } } -- 2.20.1