jenkins-bot has submitted this change and it was merged.
Change subject: Add option to ApiFormatRaw to generate a HTTP errorcode
......................................................................
Add option to ApiFormatRaw to generate a HTTP errorcode
New method setFailWithHTTPError() allows you to configure ApiFormatRaw
to return HTTP status code 400 in case it encounters (any) error.
This is useful for when the output format is intended to be read by a
user-agent which has no other method to be notified of errors.
Change-Id: I11b2824c6725143cff5b4c56194f94c44ce451c0
---
M includes/api/ApiFormatRaw.php
1 file changed, 25 insertions(+), 3 deletions(-)
Approvals:
Anomie: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/api/ApiFormatRaw.php b/includes/api/ApiFormatRaw.php
index 7bb2453..9dbd4a5 100644
--- a/includes/api/ApiFormatRaw.php
+++ b/includes/api/ApiFormatRaw.php
@@ -31,14 +31,20 @@
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() {
@@ -59,6 +65,9 @@
$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 );
}
@@ -85,4 +94,17 @@
}
$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;
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/236076
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I11b2824c6725143cff5b4c56194f94c44ce451c0
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: TheDJ <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits