ParsoidClient: error should be array or null

This follows up the initial fix with additional sanitation and a
regression test. See I21c7a2b2541061a858a9791a2cb12866acd38dc5.

Bug: T318083
Change-Id: I4e433e711c068336a888f4aafa243455764fd710
This commit is contained in:
daniel 2022-09-19 20:00:25 +02:00
parent 87ff7a5fec
commit 4c1ec8fc40
2 changed files with 30 additions and 9 deletions

View file

@ -77,8 +77,8 @@ class VRSParsoidClient implements ParsoidClient {
'apierror-visualeditor-docserver-http-error',
wfEscapeWikiText( $response['error'] )
];
} elseif ( $response['code'] !== 200 ) {
// error null, code not 200
} elseif ( $response['code'] >= 400 ) {
// no error message, but code indicates an error
$json = json_decode( $response['body'], true );
$text = $json['detail'] ?? '(no message)';
$response['error'] = [
@ -86,7 +86,11 @@ class VRSParsoidClient implements ParsoidClient {
$response['code'],
wfEscapeWikiText( $text )
];
} else {
// Needed because $response['error'] may be '' on success!
$response['error'] = null;
}
return $response;
}

View file

@ -87,7 +87,7 @@ class VRSParsoidClientTest extends MediaWikiIntegrationTestCase {
'code' => 200,
'headers' => [],
'body' => '<html><body>Response body</body></html>',
'error' => null,
'error' => '',
];
$vrsClient = $this->createVRSParsoidClient(
[
@ -110,7 +110,8 @@ class VRSParsoidClientTest extends MediaWikiIntegrationTestCase {
$resp = $vrsClient->getPageHtml( $revision, $language );
$this->assertIsArray( $resp );
$this->assertArrayEquals( $resp, $response );
$this->assertFalse( isset( $resp['error'] ) );
$this->assertArrayEquals( $resp, $response, false, true );
}
/**
@ -135,7 +136,7 @@ class VRSParsoidClientTest extends MediaWikiIntegrationTestCase {
'code' => '500',
'headers' => [],
'body' => '{}',
'error' => null,
'error' => '',
],
[
@ -152,7 +153,7 @@ class VRSParsoidClientTest extends MediaWikiIntegrationTestCase {
'body' => json_encode( [
'detail' => 'Another error message',
] ),
'error' => null,
'error' => '',
],
[
@ -161,6 +162,22 @@ class VRSParsoidClientTest extends MediaWikiIntegrationTestCase {
'Another error message'
]
];
yield [
[
'code' => '205',
'headers' => [
'Location' => 'http://example.com/'
],
'body' => json_encode( [
'detail' => 'bla bla bla',
] ),
'error' => '',
],
// not an error
null
];
}
/**
@ -228,7 +245,7 @@ class VRSParsoidClientTest extends MediaWikiIntegrationTestCase {
);
$this->assertIsArray( $resp );
$this->assertArrayEquals( $resp, $response );
$this->assertArrayEquals( $resp, $response, false, true );
}
/** @return Generator */
@ -266,7 +283,7 @@ class VRSParsoidClientTest extends MediaWikiIntegrationTestCase {
'Accept-Language' => $langCode,
],
'body' => '<html><body>Response body</body></html>',
'reason' => null,
'error' => '',
];
$vrsClient = $this->createVRSParsoidClient(
@ -288,6 +305,6 @@ class VRSParsoidClientTest extends MediaWikiIntegrationTestCase {
);
$this->assertIsArray( $resp );
$this->assertArrayEquals( $resp, $response );
$this->assertArrayEquals( $resp, $response, false, true );
}
}