throw new MWException( 'Successful status passed to ApiBase::dieStatus' );
}
+ // ApiUsageException needs a fatal status, but this method has
+ // historically accepted any non-good status. Convert it if necessary.
+ $status->setOK( false );
+ if ( !$status->getErrorsByType( 'error' ) ) {
+ $newStatus = Status::newGood();
+ foreach ( $status->getErrorsByType( 'warning' ) as $err ) {
+ call_user_func_array(
+ [ $newStatus, 'fatal' ],
+ array_merge( [ $err['message'] ], $err['params'] )
+ );
+ }
+ if ( !$newStatus->getErrorsByType( 'error' ) ) {
+ $newStatus->fatal( 'unknownerror-nocode' );
+ }
+ $status = $newStatus;
+ }
+
throw new ApiUsageException( $this, $status );
}
], $user ) );
}
+ /**
+ * @covers ApiBase::dieStatus
+ */
+ public function testDieStatus() {
+ $mock = new MockApi();
+
+ $status = StatusValue::newGood();
+ $status->error( 'foo' );
+ $status->warning( 'bar' );
+ try {
+ $mock->dieStatus( $status );
+ $this->fail( 'Expected exception not thrown' );
+ } catch ( ApiUsageException $ex ) {
+ $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
+ $this->assertFalse( ApiTestCase::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
+ }
+
+ $status = StatusValue::newGood();
+ $status->warning( 'foo' );
+ $status->warning( 'bar' );
+ try {
+ $mock->dieStatus( $status );
+ $this->fail( 'Expected exception not thrown' );
+ } catch ( ApiUsageException $ex ) {
+ $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
+ $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
+ }
+
+ $status = StatusValue::newGood();
+ $status->setOk( false );
+ try {
+ $mock->dieStatus( $status );
+ $this->fail( 'Expected exception not thrown' );
+ } catch ( ApiUsageException $ex ) {
+ $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'unknownerror-nocode' ),
+ 'Exception has "unknownerror-nocode"' );
+ }
+ }
+
}