class ApiFormatRaw extends ApiFormatBase {
private $errorFallback;
+ private $mFailWithHTTPError = false;
+
/**
* @param ApiMain $main
- * @param ApiFormatBase $errorFallback Object to fall back on for errors
+ * @param ApiFormatBase |null $errorFallback Object to fall back on for errors
*/
- public function __construct( ApiMain $main, ApiFormatBase $errorFallback ) {
+ public function __construct( ApiMain $main, ApiFormatBase $errorFallback = null ) {
parent::__construct( $main, 'raw' );
- $this->errorFallback = $errorFallback;
+ if ( $errorFallback === null ) {
+ $this->errorFallback = $main->createPrinterByName( $main->getParameter( 'format' ) );
+ } else {
+ $this->errorFallback = $errorFallback;
+ }
}
public function getMimeType() {
$data = $this->getResult()->getResultData();
if ( isset( $data['error'] ) ) {
$this->errorFallback->initPrinter( $unused );
+ if ( $this->mFailWithHTTPError ) {
+ $this->getMain()->getRequest()->response()->statusHeader( 400 );
+ }
} else {
parent::initPrinter( $unused );
}
}
$this->printText( $data['text'] );
}
+
+ /**
+ * Output HTTP error code 400 when if an error is encountered
+ *
+ * The purpose is for output formats where the user-agent will
+ * not be able to interpret the validity of the content in any
+ * other way. For example subtitle files read by browser video players.
+ *
+ * @param bool $fail
+ */
+ public function setFailWithHTTPError( $fail ) {
+ $this->mFailWithHTTPError = $fail;
+ }
}