mw.Api: Add helper method #getErrorMessage


mw.Api: Add helper method #getErrorMessage

Given an API response indicating an error, it returns a jQuery object
containing a human-readable error message that you can display
somewhere on the page.

Handling the "real" API responses from the server is easy, since they
already contain the error message and we basically just extract it.
The important part of this patch is handling the issues that happen
on the client, or server-side issues where the error-handling API
code never runs, so we have to figure out what happened and make up
our own error message.

I have not yet seen any mw.Api consumer that handles all the error
cases correctly (I thought VisualEditor did, but in the process of
writing this patch I learned that we missed a few cases there as
well), and most don't even try. This might not be perfect but it
should be the closest thing so far.

The new code is inspired by error handling in VisualEditor (see
I69d9432162f195dcfe9470485b549a1b007617ff) and in mw.Upload.BookletLayout.

New messages:

  • 'api-clientside-error-noconnect'
    • Adapted from 'apierror-offline' and 'visualeditor-error-noconnect'
  • 'api-clientside-error-http'
    • Adapted from 'visualeditor-loadwarning' and 'visualeditor-error-http'
  • 'api-clientside-error-timeout'
    • Copied from 'apierror-timeout'
  • 'api-clientside-error-aborted'
    • Entirely new
  • 'api-clientside-error-invalidresponse'
    • Copied from 'visualeditor-error-invalidresponse'

Update some existing code to use it, as an example:

  • mediawiki.page.watch.ajax
    • Removed messages: 'watcherrortext'
  • mediawiki.special.unwatchedPages
    • Removed messages: 'watcherrortext'
  • mediawiki.action.edit.preview
    • (errors were hardcoded in English)
  • mediawiki.feedback
    • Removed messages: 'feedback-error2', 'feedback-error3' (error1 and error4 remain, they handle errors not caused by the API)
  • mediawiki.Upload.BookletLayout
    • Removed messages: 'apierror-timeout', 'apierror-offline', 'apierror-unknownerror'
  • mediawiki.page.ready
    • Removed messages: 'logout-failed'
  • 'mediawiki.page.patrol.ajax
    • (the messages are already used by the patrol API)

Example usages in extensions:

  • VisualEditor: I69d9432162f195dcfe9470485b549a1b007617ff
  • DiscussionTools: Ice92fafb1f546510dab28e3f8aa7d2280668965a

Bug: T240519
Change-Id: Ie18666b41f4aff1ab4bcf93f9df6e3000ac7b500


matmarexAuthored on Dec 11 2019, 1:12 AM
rMW2b3b312f4eff: Merge "Allow specifying autoloaded classes for tests"