Page MenuHomePhabricator

ApiHelpPanelPostQuestion throws an unhandled exception on invalid API calls
Closed, ResolvedPublic2 Estimated Story PointsBUG REPORT

Description

Steps to replicate the issue (include links if applicable):

On a wiki with no mentors available, submit the following API query:

{
	"action": "helppanelquestionposter",
	"format": "json",
	"body": "Testing question",
	"source": "mentor-helppanel",
	"token": "REPLACE WITH THE ACTUAL CSRF TOKEN",
	"formatversion": "2"
}

By "no mentors available", I mean no mentors listed at Special:ManageMentors and no mentor being already assigned to you.

What happens?:

An unhandled exception is thrown. Currently: [{reqId}] {exception_url} Wikimedia\Assert\InvariantException: Invariant failed: MentorQuestionPoster called without a mentor present

What should have happened instead?:
The API call is invalid (it is not possible to submit a question to a mentor when mentorship is not in use). However, it still should not thrown an unhandled exception.

Other information (browser name/version, screenshots, etc.):
Due to the refactoring done in T351234, the error would change from WikiConfigException to a failed assertion. However, the issue remains the same.

This is not as important, as the API is marked as internal, and only GrowthExperiments itself is supposed to call it. However, we still shouldn't fail hard on invalid calls.

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript
KStoller-WMF set the point value for this task to 2.
Michael raised the priority of this task from Low to Medium.Jul 10 2025, 7:47 AM

Let's raise the priority of this as it keeps happening and cause log-spam in logstash and thus making other issues harder to find: T388403: Wikimedia\Assert\InvariantException: Invariant failed: MentorQuestionPoster called without a mentor present.

The solution here would probably be to check earlier in the stack, maybe in QuestionPosterFactory::getQuestionPoster, in the case when the user is trying to post a question to their mentor's talk-page, whether they actually have a mentor. If they don't then we can throw a specific exception there, and catch that also in ApiHelpPanelPostQuestion::getQuestionPoster and turn it into a ApiUsageException.

EMcFarland-WMF changed the task status from Open to In Progress.Nov 21 2025, 12:58 PM

Change #1211638 had a related patch set uploaded (by EMcFarland; author: EMcFarland):

[mediawiki/extensions/GrowthExperiments@master] Handle exception from ApiHelpPanelPostQuestion when a mentee asks a question to a nonexistent mentor

https://gerrit.wikimedia.org/r/1211638

Change #1211639 had a related patch set uploaded (by EMcFarland; author: EMcFarland):

[mediawiki/core@master] Add an exception for wikis that lack mentors.

https://gerrit.wikimedia.org/r/1211639

Change #1211639 abandoned by EMcFarland:

[mediawiki/core@master] Add an exception for wikis that lack mentors.

Reason:

I am abandoning the change in favor of adding an exception to the GrowthExperiments extension repo.

https://gerrit.wikimedia.org/r/1211639

Here are notes for how to do manual QA for this task. (I know the task isn't there yet, but I am including these notes for future QA.)
Scenario: No mentors on a wiki.

  1. On a wiki that has no mentors, navigate to the Special:ApiSandbox page.
  2. Select action=helppanelquestionposter.
  3. For source, select "mentor-helppanel" from the dropdown.
  4. Submit the request.
  5. You should see an error that explains there are no mentors.

T386567MustHaveMentorsErrorMessage.png (1×3 px, 582 KB)

Scenario: A wiki has mentors, but a user attempts to ask a mentor a question when they themselves don't have a mentor.

  1. While logged in as a user who has a mentor, navigate to Special:Homepage.
  2. Click on the mentor module and navigate to Special:Homepage#/homepage/mentorship/question.
  3. Type a question to your mentor, but don't publish it.
  4. Open Special:Homepage in another tab as the same user. Navigate to Learn more about mentors.
  5. Still in that same tab, opt out of mentorship.
  6. Return to the previous tab. Attempt to publish your question. You should see the below error message.

T386567CantAskMentorAQuestionWhenUnmentored.png (1×3 px, 435 KB)

Change #1214554 had a related patch set uploaded (by EMcFarland; author: EMcFarland):

[mediawiki/extensions/GrowthExperiments@master] MentorQuestionPoster: Refactor so ::construct works without a mentor

https://gerrit.wikimedia.org/r/1214554

Change #1214554 merged by jenkins-bot:

[mediawiki/extensions/GrowthExperiments@master] MentorQuestionPoster: Refactor so ::construct works without a mentor

https://gerrit.wikimedia.org/r/1214554

Change #1211638 merged by jenkins-bot:

[mediawiki/extensions/GrowthExperiments@master] Handle exception when mentee asks a question to a nonexistent mentor

https://gerrit.wikimedia.org/r/1211638

Etonkovidova subscribed.

Thank you, @EMcFarland-WMF for the QA plan! Checked on beta - works as expected. Moving to check in production.

(1) Testing on frwiki beta (and ukwiki beta) - Special:ManageMentors list is empty.

{
    "error": {
        "code": "notmentored",
        "info": "You cannot ask a question to your mentor if you do not have a mentor.",
        "docref": "See https://fr.wikipedia.beta.wmcloud.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/postorius/lists/mediawiki-api-announce.lists.wikimedia.org/> for notice of API deprecations and breaking changes."
    },
    "servedby": "deployment-mediawiki13"
}

✅ Checked on cawiki wmf.10

{
    "error": {
        "code": "notmentored",
        "info": "You cannot ask a question to your mentor if you do not have a mentor.",
        "docref": "See https://ca.wikipedia.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/postorius/lists/mediawiki-api-announce.lists.wikimedia.org/> for notice of API deprecations and breaking changes."
    },
    "servedby": "mw-api-ext.codfw.main-667764956f-pcdmf"
}

(2) When a user opts-out from Mentorship - beta wiki

Screenshot 2025-12-23 at 5.17.21 PM.png (524×620 px, 77 KB)

Testing notes for checking production wikis

(1)
The current api call in wmf.7 - check after deployment to production (or cawiki can be used):

{
    "error": {
        "code": "internal_api_error_Wikimedia\\Assert\\InvariantException",
        "info": "[58527291-f8b0-4842-97a5-ec0b1450821e] Caught exception of type Wikimedia\\Assert\\InvariantException",
        "errorclass": "Wikimedia\\Assert\\InvariantException"
    },
    "servedby": "mw-api-ext.codfw.main-6c9d649c6d-xh9br"
}

(2) When a user opts-out from Mentorship
testwiki wmf.7

Screenshot 2025-12-23 at 5.15.05 PM.png (954×1 px, 157 KB)

✅ Checked testwiki wmf.10
{F71464647}