From d0830af6ba44c6bb51496c6577df4615f6c1bb46 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Thu, 27 Mar 2014 11:11:17 -0400 Subject: [PATCH] API: Allow for format modules that cannot handle errors ApiFormatFeedWrapper, for example, has nothing particularly useful to do when given an API error to print. So allow for punting errors to the default formatter instead. Bug: 63150 Change-Id: Ifc034d4c7861905e382c42dc22585f0cd2beaf3f --- includes/api/ApiFormatBase.php | 19 +++++++++++++++++++ includes/api/ApiMain.php | 14 +++++++++++--- includes/api/ApiResult.php | 6 ++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/includes/api/ApiFormatBase.php b/includes/api/ApiFormatBase.php index 3c924bc698..d43259dd8d 100644 --- a/includes/api/ApiFormatBase.php +++ b/includes/api/ApiFormatBase.php @@ -120,6 +120,16 @@ abstract class ApiFormatBase extends ApiBase { return $this->mDisabled; } + /** + * Whether this formatter can handle printing API errors. If this returns + * false, then on API errors the default printer will be instantiated. + * @since 1.23 + * @return bool + */ + public function canPrintErrors() { + return true; + } + /** * Initialize the printer function and prepare the output headers, etc. * This method must be the first outputting method during execution. @@ -377,6 +387,15 @@ class ApiFormatFeedWrapper extends ApiFormatBase { return true; } + /** + * ChannelFeed doesn't give us a method to print errors in a friendly + * manner, so just punt errors to the default printer. + * @return false + */ + public function canPrintErrors() { + return false; + } + /** * This class expects the result data to be in a custom format set by self::setResult() * $result['_feed'] - an instance of one of the $wgFeedClasses classes diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index e1c087474f..f7c776de9f 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -641,6 +641,7 @@ class ApiMain extends ApiBase { global $wgShowHostnames; $result = $this->getResult(); + // Printer may not be initialized if the extractRequestParams() fails for the main module if ( !isset( $this->mPrinter ) ) { // The printer has not been created yet. Try to manually get formatter value. @@ -650,11 +651,18 @@ class ApiMain extends ApiBase { } $this->mPrinter = $this->createPrinterByName( $value ); - if ( $this->mPrinter->getNeedsRawData() ) { - $result->setRawMode(); - } } + // Printer may not be able to handle errors. This is particularly + // likely if the module returns something for getCustomPrinter(). + if ( !$this->mPrinter->canPrintErrors() ) { + $this->mPrinter->safeProfileOut(); + $this->mPrinter = $this->createPrinterByName( self::API_DEFAULT_FORMAT ); + } + + // Update raw mode flag for the selected printer. + $result->setRawMode( $this->mPrinter->getNeedsRawData() ); + if ( $e instanceof UsageException ) { // User entered incorrect parameters - print usage screen $errMessage = $e->getMessageArray(); diff --git a/includes/api/ApiResult.php b/includes/api/ApiResult.php index e92202025e..0cbfcc8f2a 100644 --- a/includes/api/ApiResult.php +++ b/includes/api/ApiResult.php @@ -80,9 +80,11 @@ class ApiResult extends ApiBase { /** * Call this function when special elements such as '_element' * are needed by the formatter, for example in XML printing. + * @since 1.23 $flag parameter added + * @param bool $flag Set the raw mode flag to this state */ - public function setRawMode() { - $this->mIsRawMode = true; + public function setRawMode( $flag = true ) { + $this->mIsRawMode = $flag; } /** -- 2.20.1