Actually return errors for action=edit API

Setting 'apiHookResult' results in a "successful" response; if we want
to report an error, we need to use ApiMessage. We already were doing
this for action=upload. Now our action=edit API responses will be
consistent with MediaWiki and other extensions, and will be able to
take advantage of errorformat=html.

Additionally, remove incorrect 'message' value from action=upload
output. It was anyway redundant to the normal error information.

To avoid user interface regressions in VisualEditor, the changes
I3b9c4fef (in VE) and I106dbd3c (in MediaWiki) should be merged first.

Before:
    {
        "edit": {
            "spamblacklist": "example.com/test|example.net/test",
            "result": "Failure"
        }
    }

After:
    {
        "errors": [
            {
                "code": "spamblacklist",
                "data": {
                    "spamblacklist": {
                        "matches": [
                            "example.com/test",
                            "example.net/test"
                        ]
                    }
                },
                "module": "edit",
                "*": "The text you wanted to save was blocked ..."
            }
        ],
        "*": "See http://localhost:3080/w/api.php for API usage. ..."
    }

For comparison, a 'readonly' error:
    {
        "errors": [
            {
                "code": "readonly",
                "data": {
                    "readonlyreason": "foo bar"
                },
                "module": "main",
                "*": "The wiki is currently in read-only mode."
            }
        ],
        "*": "See http://localhost:3080/w/api.php for API usage. ..."
    }

Bug: T229539
Depends-On: I106dbd3cbdbf7082b1d1f1c1106ece6b19c22a86
Depends-On: I3b9c4fefc0869ef7999c21cef754434febd852ec
Change-Id: Id36aa6bdb8f873fe7deb8123a7fc774103721c01
This commit is contained in:
Bartosz Dziewoński 2019-08-20 20:47:21 +02:00 committed by Anomie
parent 2f1456c4e9
commit 5ddc45b62c
2 changed files with 12 additions and 10 deletions

View file

@ -42,10 +42,14 @@ class SpamBlacklistHooks {
$matches = $spamObj->filter( $links, $title );
if ( $matches !== false ) {
$status->fatal( 'spam-blacklisted-link', $context->getLanguage()->listToText( $matches ) );
$status->apiHookResult = [
'spamblacklist' => implode( '|', $matches ),
];
$error = new ApiMessage(
wfMessage( 'spam-blacklisted-link', Message::listParam( $matches ) ),
'spamblacklist',
[
'spamblacklist' => [ 'matches' => $matches ],
]
);
$status->fatal( $error );
}
// Always return true, EditPage will look at $status->isOk().
@ -222,10 +226,6 @@ class SpamBlacklistHooks {
'spamblacklist',
[
'spamblacklist' => [ 'matches' => $matches ],
'message' => [
'key' => 'spamprotectionmatch',
'params' => $matches[0],
],
]
);
}

View file

@ -7,13 +7,15 @@ mw.libs.ve.targetLoader.addPlugin( function () {
ve.init.mw.SpamBlacklistSaveErrorHandler.static.name = 'spamBlacklist';
ve.init.mw.SpamBlacklistSaveErrorHandler.static.matchFunction = function ( data ) {
return !!ve.getProp( data, 'visualeditoredit', 'edit', 'spamblacklist' );
return data.errors && data.errors.some( function ( err ) {
return err.code === 'spamblacklist';
} );
};
ve.init.mw.SpamBlacklistSaveErrorHandler.static.process = function ( data, target ) {
// Handle spam blacklist error from Extension:SpamBlacklist
target.showSaveError(
$( $.parseHTML( ve.getProp( data, 'visualeditoredit', 'edit', 'sberrorparsed' ) ) ),
target.extractErrorMessages( data ),
false // prevents reapply
);
// Emit event for tracking. TODO: This is a bad design