Skip to content

Commit

Permalink
Merge "REST: Modify ReplaceItemStatement to use exceptions"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Feb 27, 2023
2 parents f27249f + a7f4d32 commit 8e910f9
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Wikibase\Repo\RestApi\RouteHandlers;

use LogicException;
use MediaWiki\MediaWikiServices;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\RequestInterface;
Expand All @@ -18,9 +17,9 @@
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\UserAgentCheckMiddleware;
use Wikibase\Repo\RestApi\Serialization\StatementSerializer;
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatement;
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementErrorResponse;
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementRequest;
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementSuccessResponse;
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementResponse;
use Wikibase\Repo\RestApi\UseCases\UseCaseException;
use Wikibase\Repo\RestApi\WbRestApi;
use Wikimedia\ParamValidator\ParamValidator;

Expand Down Expand Up @@ -92,22 +91,20 @@ public function run( ...$args ): Response {

public function runUseCase( string $itemId, string $statementId ): Response {
$requestBody = $this->getValidatedBody();
$useCaseResponse = $this->replaceItemStatement->execute( new ReplaceItemStatementRequest(
$statementId,
$requestBody[self::STATEMENT_BODY_PARAM],
$requestBody[self::TAGS_BODY_PARAM],
$requestBody[self::BOT_BODY_PARAM],
$requestBody[self::COMMENT_BODY_PARAM],
$this->getUsername(),
$itemId
) );

if ( $useCaseResponse instanceof ReplaceItemStatementSuccessResponse ) {

try {
$useCaseResponse = $this->replaceItemStatement->execute( new ReplaceItemStatementRequest(
$statementId,
$requestBody[self::STATEMENT_BODY_PARAM],
$requestBody[self::TAGS_BODY_PARAM],
$requestBody[self::BOT_BODY_PARAM],
$requestBody[self::COMMENT_BODY_PARAM],
$this->getUsername(),
$itemId
) );
return $this->newSuccessHttpResponse( $useCaseResponse );
} elseif ( $useCaseResponse instanceof ReplaceItemStatementErrorResponse ) {
return $this->responseFactory->newErrorResponse( $useCaseResponse );
} else {
throw new LogicException( 'Received an unexpected use case result in ' . __CLASS__ );
} catch ( UseCaseException $e ) {
return $this->responseFactory->newErrorResponseFromException( $e );
}
}

Expand Down Expand Up @@ -157,7 +154,7 @@ public function getBodyValidator( $contentType ): BodyValidator {
] ) : parent::getBodyValidator( $contentType );
}

private function newSuccessHttpResponse( ReplaceItemStatementSuccessResponse $useCaseResponse ): Response {
private function newSuccessHttpResponse( ReplaceItemStatementResponse $useCaseResponse ): Response {
$httpResponse = $this->getResponseFactory()->create();
$httpResponse->setStatus( 200 );
$httpResponse->setHeader( 'Content-Type', 'application/json' );
Expand Down
33 changes: 15 additions & 18 deletions repo/rest-api/src/RouteHandlers/ReplaceStatementRouteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Wikibase\Repo\RestApi\RouteHandlers;

use LogicException;
use MediaWiki\MediaWikiServices;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\RequestInterface;
Expand All @@ -19,9 +18,9 @@
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\UserAgentCheckMiddleware;
use Wikibase\Repo\RestApi\Serialization\StatementSerializer;
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatement;
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementErrorResponse;
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementRequest;
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementSuccessResponse;
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementResponse;
use Wikibase\Repo\RestApi\UseCases\UseCaseException;
use Wikibase\Repo\RestApi\WbRestApi;
use Wikimedia\ParamValidator\ParamValidator;

Expand Down Expand Up @@ -94,21 +93,19 @@ public function run( ...$args ): Response {

public function runUseCase( string $statementId ): Response {
$requestBody = $this->getValidatedBody();
$useCaseResponse = $this->replaceItemStatement->execute( new ReplaceItemStatementRequest(
$statementId,
$requestBody[self::STATEMENT_BODY_PARAM],
$requestBody[self::TAGS_BODY_PARAM],
$requestBody[self::BOT_BODY_PARAM],
$requestBody[self::COMMENT_BODY_PARAM],
$this->getUsername()
) );

if ( $useCaseResponse instanceof ReplaceItemStatementSuccessResponse ) {

try {
$useCaseResponse = $this->replaceItemStatement->execute( new ReplaceItemStatementRequest(
$statementId,
$requestBody[self::STATEMENT_BODY_PARAM],
$requestBody[self::TAGS_BODY_PARAM],
$requestBody[self::BOT_BODY_PARAM],
$requestBody[self::COMMENT_BODY_PARAM],
$this->getUsername()
) );
return $this->newSuccessHttpResponse( $useCaseResponse );
} elseif ( $useCaseResponse instanceof ReplaceItemStatementErrorResponse ) {
return $this->responseFactory->newErrorResponse( $useCaseResponse );
} else {
throw new LogicException( 'Received an unexpected use case result in ' . __CLASS__ );
} catch ( UseCaseException $e ) {
return $this->responseFactory->newErrorResponseFromException( $e );
}
}

Expand Down Expand Up @@ -153,7 +150,7 @@ public function getBodyValidator( $contentType ): BodyValidator {
] ) : parent::getBodyValidator( $contentType );
}

private function newSuccessHttpResponse( ReplaceItemStatementSuccessResponse $useCaseResponse ): Response {
private function newSuccessHttpResponse( ReplaceItemStatementResponse $useCaseResponse ): Response {
$httpResponse = $this->getResponseFactory()->create();
$httpResponse->setStatus( 200 );
$httpResponse->setHeader( 'Content-Type', 'application/json' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Wikibase\Repo\RestApi\Domain\Services\ItemUpdater;
use Wikibase\Repo\RestApi\Domain\Services\PermissionChecker;
use Wikibase\Repo\RestApi\UseCases\ErrorResponse;
use Wikibase\Repo\RestApi\UseCases\UseCaseException;

/**
* @license GPL-2.0-or-later
Expand Down Expand Up @@ -44,13 +45,10 @@ public function __construct(
}

/**
* @return ReplaceItemStatementSuccessResponse|ReplaceItemStatementErrorResponse
* @throws UseCaseException
*/
public function execute( ReplaceItemStatementRequest $request ) {
$validationError = $this->validator->validate( $request );
if ( $validationError ) {
return ReplaceItemStatementErrorResponse::newFromValidationError( $validationError );
}
public function execute( ReplaceItemStatementRequest $request ): ReplaceItemStatementResponse {
$this->validator->assertValidRequest( $request );

$requestedItemId = $request->getItemId();
$statementIdParser = new StatementGuidParser( new ItemIdParser() );
Expand All @@ -61,19 +59,19 @@ public function execute( ReplaceItemStatementRequest $request ) {

$latestRevision = $this->revisionMetadataRetriever->getLatestRevisionMetadata( $itemId );
if ( $requestedItemId && !$latestRevision->itemExists() ) {
return new ReplaceItemStatementErrorResponse(
throw new UseCaseException(
ErrorResponse::ITEM_NOT_FOUND,
"Could not find an item with the ID: {$itemId}"
);
} elseif ( !$latestRevision->itemExists()
|| $latestRevision->isRedirect()
|| !$itemId->equals( $statementId->getEntityId() ) ) {
return $this->newStatementNotFoundErrorResponse( $statementId );
$this->throwStatementNotFoundException( $statementId );
}

$user = $request->hasUser() ? User::withUsername( $request->getUsername() ) : User::newAnonymous();
if ( !$this->permissionChecker->canEdit( $user, $itemId ) ) {
return new ReplaceItemStatementErrorResponse(
throw new UseCaseException(
ErrorResponse::PERMISSION_DENIED,
'You have no permission to edit this item.'
);
Expand All @@ -85,14 +83,14 @@ public function execute( ReplaceItemStatementRequest $request ) {
try {
$item->getStatements()->replaceStatement( $statementId, $newStatement );
} catch ( StatementNotFoundException $e ) {
return $this->newStatementNotFoundErrorResponse( $statementId );
$this->throwStatementNotFoundException( $statementId );
} catch ( StatementGuidChangedException $e ) {
return new ReplaceItemStatementErrorResponse(
throw new UseCaseException(
ErrorResponse::INVALID_OPERATION_CHANGED_STATEMENT_ID,
'Cannot change the ID of the existing statement'
);
} catch ( PropertyChangedException $e ) {
return new ReplaceItemStatementErrorResponse(
throw new UseCaseException(
ErrorResponse::INVALID_OPERATION_CHANGED_PROPERTY,
'Cannot change the property of the existing statement'
);
Expand All @@ -105,15 +103,18 @@ public function execute( ReplaceItemStatementRequest $request ) {
);
$newRevision = $this->itemUpdater->update( $item, $editMetadata );

return new ReplaceItemStatementSuccessResponse(
return new ReplaceItemStatementResponse(
$newRevision->getItem()->getStatements()->getStatementById( $statementId ),
$newRevision->getLastModified(),
$newRevision->getRevisionId()
);
}

private function newStatementNotFoundErrorResponse( StatementGuid $statementId ): ReplaceItemStatementErrorResponse {
return new ReplaceItemStatementErrorResponse(
/**
* @throws UseCaseException
*/
private function throwStatementNotFoundException( StatementGuid $statementId ): void {
throw new UseCaseException(
ErrorResponse::STATEMENT_NOT_FOUND,
"Could not find a statement with the ID: $statementId"
);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @license GPL-2.0-or-later
*/
class ReplaceItemStatementSuccessResponse {
class ReplaceItemStatementResponse {

private Statement $statement;
private string $lastModified;
Expand Down
Loading

0 comments on commit 8e910f9

Please sign in to comment.