diff --git a/includes/VRSParsoidClient.php b/includes/VRSParsoidClient.php index add9d4a36c..55b64cdb88 100644 --- a/includes/VRSParsoidClient.php +++ b/includes/VRSParsoidClient.php @@ -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; } diff --git a/tests/phpunit/integration/VRSParsoidClientTest.php b/tests/phpunit/integration/VRSParsoidClientTest.php index fed8119bb3..8652753553 100644 --- a/tests/phpunit/integration/VRSParsoidClientTest.php +++ b/tests/phpunit/integration/VRSParsoidClientTest.php @@ -87,7 +87,7 @@ class VRSParsoidClientTest extends MediaWikiIntegrationTestCase { 'code' => 200, 'headers' => [], 'body' => 'Response body', - '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' => 'Response body', - '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 ); } }