Page MenuHomePhabricator

Fatal error from Lexeme API: "InvalidArgumentException: Not a lexeme ID"
Closed, ResolvedPublic3 Story Points

Description

I accidentally made the following API request:

https://www.wikidata.org/w/api.php?action=wbformatvalue&generate=text%2Fhtml&datavalue=%7B%22type%22%3A%22wikibase-entityid%22%2C%20%22value%22%3A%20%7B%22id%22%3A%22L10-F3%22%7D%7D&datatype=wikibase-lexeme
==
https://www.wikidata.org/w/api.php?action=wbformatvalue&generate=text/html&datavalue={"type":"wikibase-entityid", "value": {"id":"L10-F3"}}&datatype=wikibase-lexeme

i. e., attempting to format a form ID value ({"type":"wikibase-entityid", "value": {"id":"L10-F3"}}) as a lexeme datatype.

It results in the following log error:

InvalidArgumentException: Not a lexeme ID: L10-F3
/srv/mediawiki/php-1.32.0-wmf.26/extensions/WikibaseLexeme/src/Formatters/LexemeIdHtmlFormatter.php:58
#0 /srv/mediawiki/php-1.32.0-wmf.26/extensions/Wikibase/lib/includes/Formatters/EntityIdValueFormatter.php(44): Wikibase\Lexeme\Formatters\LexemeIdHtmlFormatter->formatEntityId(Wikibase\Lexeme\DataModel\FormId)
#1 /srv/mediawiki/php-1.32.0-wmf.26/extensions/Wikibase/lib/includes/Formatters/DispatchingValueFormatter.php(75): Wikibase\Lib\EntityIdValueFormatter->format(Wikibase\DataModel\Entity\EntityIdValue)
#2 /srv/mediawiki/php-1.32.0-wmf.26/extensions/Wikibase/repo/includes/Api/FormatSnakValue.php(104): Wikibase\Lib\Formatters\DispatchingValueFormatter->formatValue(Wikibase\DataModel\Entity\EntityIdValue, string)
#3 /srv/mediawiki/php-1.32.0-wmf.26/includes/api/ApiMain.php(1570): Wikibase\Repo\Api\FormatSnakValue->execute()
#4 /srv/mediawiki/php-1.32.0-wmf.26/includes/api/ApiMain.php(531): ApiMain->executeAction()
#5 /srv/mediawiki/php-1.32.0-wmf.26/includes/api/ApiMain.php(502): ApiMain->executeActionWithErrorHandling()
#6 /srv/mediawiki/php-1.32.0-wmf.26/api.php(87): ApiMain->execute()
#7 /srv/mediawiki/w/api.php(3): include(string)
#8 {main}

The Plan (extracted from a comment below)

From the investigation (T209368) we want to do the following

1 - ValueFormatter interface (datavalues/interfaces) - https://github.com/DataValues/Interfaces/pull/44
The ValueFormatter interface should be documented as allowing InvalidArgumentExceptions to be thrown when arguments are known to be wrong for the type of Formatter instance.
Many instances of ValueFormatter already do this, but lets add it to the interface documentation.

2 - EntityIdFormatter interface (datamodel-services) - https://github.com/wmde/WikibaseDataModelServices/pull/213
EntityIdFormatter interface should also document throwing an InvalidArgumentException when being provided with an argument that the current formatter can not format.

3 - FormatSnakValue API
EntityIdValueFormatter::format itself already throws InvalidArgumentExceptions, and the 2 cases above bubble up through this method.
When $valueFormatter->format is called in the FormatSnakValue API, InvalidArgumentExceptions from the call should be caught and exposed to the user as a regular API error, not an exception.

Details

Related Gerrit Patches:

Event Timeline

The similar mistake of attempting to format a form ID value as a sense datatype results in a PHP Fatal Error:

PHP Fatal Error: Argument 1 passed to Wikibase\Lexeme\DataModel\Lexeme::getSense() must be an instance of Wikibase\Lexeme\DataModel\SenseId, Wikibase\Lexeme\DataModel\FormId given
#0 /srv/mediawiki/php-1.32.0-wmf.26/extensions/WikibaseLexeme/src/Formatters/SenseIdHtmlFormatter.php(85): NO_FUNCTION_GIVEN()
#1 /srv/mediawiki/php-1.32.0-wmf.26/extensions/Wikibase/lib/includes/Formatters/EntityIdValueFormatter.php(44): Wikibase\Lexeme\Formatters\SenseIdHtmlFormatter->formatEntityId(Wikibase\Lexeme\DataModel\FormId)
#2 /srv/mediawiki/php-1.32.0-wmf.26/extensions/Wikibase/lib/includes/Formatters/DispatchingValueFormatter.php(75): Wikibase\Lib\EntityIdValueFormatter->format(Wikibase\DataModel\Entity\EntityIdValue)
#3 /srv/mediawiki/php-1.32.0-wmf.26/extensions/Wikibase/repo/includes/Api/FormatSnakValue.php(104): Wikibase\Lib\Formatters\DispatchingValueFormatter->formatValue(Wikibase\DataModel\Entity\EntityIdValue, string)
#4 /srv/mediawiki/php-1.32.0-wmf.26/includes/api/ApiMain.php(1570): Wikibase\Repo\Api\FormatSnakValue->execute()
#5 /srv/mediawiki/php-1.32.0-wmf.26/includes/api/ApiMain.php(531): ApiMain->executeAction()
#6 /srv/mediawiki/php-1.32.0-wmf.26/includes/api/ApiMain.php(502): ApiMain->executeActionWithErrorHandling()
#7 /srv/mediawiki/php-1.32.0-wmf.26/api.php(87): ApiMain->execute()
#8 /srv/mediawiki/w/api.php(3): include(string)
#9 {main}
Krinkle renamed this task from InvalidArgumentException in LexemeIdHtmlFormatter when passing form or sense ID to Fatal error from Lexeme API: "InvalidArgumentException: Not a lexeme ID".Oct 27 2018, 6:18 PM
Krinkle updated the task description. (Show Details)Nov 7 2018, 8:10 PM
Addshore triaged this task as Medium priority.Nov 12 2018, 6:04 PM
Addshore moved this task from Incoming to Ready to estimate on the Wikidata-Campsite board.
Addshore moved this task from incoming to consider for next sprint on the Wikidata board.
Addshore added a subscriber: Addshore.

During the task breakdown we realized that the "right" thing to do probably isn't to catch these exceptions and show an error.
So we will boot it back into the Wikidata-Campsite and pick up an investigation task instead.

Addshore updated the task description. (Show Details)Jan 21 2019, 10:44 AM
Addshore changed the task status from Stalled to Open.Feb 19 2019, 2:45 PM
Addshore moved this task from Needs Work to Ready to estimate on the Wikidata-Campsite board.

From the investigation (T209368) we want to do the following

1 - ValueFormatter interface
The ValueFormatter interface should be documented as allowing InvalidArgumentExceptions to be thrown when arguments are known to be wrong for the type of Formatter instance.
Many instances of ValueFormatter already do this, but lets add it to the interface documentation.

2 - EntityIdFormatter interface
EntityIdFormatter interface should also document throwing an InvalidArgumentException when being provided with an argument that the current formatter can not format.

3 - FormatSnakValue API
EntityIdValueFormatter::format itself already throws InvalidArgumentExceptions, and the 2 cases above bubble up through this method.
When $valueFormatter->format is called in the FormatSnakValue API, InvalidArgumentExceptions from the call should be caught and exposed to the user as a regular API error, not an exception.

Addshore updated the task description. (Show Details)Feb 19 2019, 2:53 PM
Addshore updated the task description. (Show Details)
alaa_wmde updated the task description. (Show Details)Feb 20 2019, 4:21 PM

Change 491935 had a related patch set uploaded (by Alaa Sarhan; owner: Alaa Sarhan):
[mediawiki/extensions/Wikibase@master] lexem.api: Return API error instead of bubbling up InvalidArgumentException in wbformatvalue action.

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

This comment was removed by alaa_wmde.
This comment was removed by alaa_wmde.
alaa_wmde closed this task as Resolved.Feb 22 2019, 1:43 PM
alaa_wmde reopened this task as Open.Feb 24 2019, 5:06 PM

Change 491935 merged by jenkins-bot:
[mediawiki/extensions/Wikibase@master] lexeme.api: throw API error instead of InvalidArgumentException in wbformatvalue

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

Restricted Application added a subscriber: Liuxinyu970226. · View Herald TranscriptMar 12 2019, 5:10 PM
Jdforrester-WMF closed this task as Resolved.Mar 21 2019, 7:58 PM
Jdforrester-WMF added a subscriber: Jdforrester-WMF.

The link in the description now results in production in a 200 -> API error response (shouldn't that be a 40x?), so Resolved.

The link in the description now results in production in a 200 -> API error response (shouldn't that be a 40x?)

I thought the Action API always returns 200 (unless an internal error occurs)? For example, action=query&titles=Does not exist doesn’t return 404 Not Found either.

Jdforrester-WMF added a comment.EditedMar 22 2019, 5:05 PM

The link in the description now results in production in a 200 -> API error response (shouldn't that be a 40x?)

I thought the Action API always returns 200 (unless an internal error occurs)? For example, action=query&titles=Does not exist doesn’t return 404 Not Found either.

Yeah, this is T26144 T40716: API should use status codes for errors, not specific to this API. :-)

mmodell changed the subtype of this task from "Task" to "Production Error".Aug 28 2019, 11:08 PM