Page MenuHomePhabricator

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

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.

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
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 subscribed.

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.

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.

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

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

Jdforrester-WMF subscribed.

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.

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