From 0c9409eaf5ac929c16ac28c2971e62743deccd05 Mon Sep 17 00:00:00 2001 From: Rob Rudin Date: Thu, 18 Jan 2024 10:19:51 -0500 Subject: [PATCH] Improved error message when Optic update plan fails --- .../marklogic/client/impl/RowManagerImpl.java | 26 ++++++++++++------- .../client/test/rows/LockForUpdateTest.java | 8 +++++- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/marklogic-client-api/src/main/java/com/marklogic/client/impl/RowManagerImpl.java b/marklogic-client-api/src/main/java/com/marklogic/client/impl/RowManagerImpl.java index 57ce16087..aa8c6c412 100644 --- a/marklogic-client-api/src/main/java/com/marklogic/client/impl/RowManagerImpl.java +++ b/marklogic-client-api/src/main/java/com/marklogic/client/impl/RowManagerImpl.java @@ -33,11 +33,8 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.JsonNodeType; +import com.marklogic.client.*; import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; -import com.marklogic.client.MarkLogicBindingException; -import com.marklogic.client.MarkLogicIOException; -import com.marklogic.client.MarkLogicInternalException; -import com.marklogic.client.Transaction; import com.marklogic.client.document.DocumentWriteSet; import com.marklogic.client.expression.PlanBuilder; import com.marklogic.client.expression.PlanBuilder.Plan; @@ -422,11 +419,22 @@ private RESTServiceResultIterator submitPlan(PlanBuilderBaseImpl.RequestPlan req AbstractWriteHandle astHandle = requestPlan.getHandle(); List contentParams = requestPlan.getContentParams(); final String path = determinePath(); - if (contentParams != null && !contentParams.isEmpty()) { - contentParams.add(new ContentParam(new PlanBuilderBaseImpl.PlanParamBase("query"), astHandle, null)); - return services.postMultipartForm(requestLogger, path, transaction, params, contentParams); - } - return services.postIteratedResource(requestLogger, path, transaction, params, astHandle); + try { + if (contentParams != null && !contentParams.isEmpty()) { + contentParams.add(new ContentParam(new PlanBuilderBaseImpl.PlanParamBase("query"), astHandle, null)); + return services.postMultipartForm(requestLogger, path, transaction, params, contentParams); + } + return services.postIteratedResource(requestLogger, path, transaction, params, astHandle); + } catch (FailedRequestException ex) { + String message = ex.getMessage(); + if (message != null && message.contains("RESTAPI-UPDATEFROMQUERY")) { + String betterMessage = "The Optic plan is attempting an update but was sent to the wrong REST API endpoint. " + + "You must invoke `withUpdate(true)` on the instance of com.marklogic.client.row.RowManager that you " + + "are using to submit the plan"; + throw new FailedRequestException(betterMessage, ex.getFailedRequest()); + } + throw ex; + } } private PlanBuilderBaseImpl.RequestPlan checkPlan(Plan plan) { diff --git a/marklogic-client-api/src/test/java/com/marklogic/client/test/rows/LockForUpdateTest.java b/marklogic-client-api/src/test/java/com/marklogic/client/test/rows/LockForUpdateTest.java index 36834dda4..82bbf950a 100644 --- a/marklogic-client-api/src/test/java/com/marklogic/client/test/rows/LockForUpdateTest.java +++ b/marklogic-client-api/src/test/java/com/marklogic/client/test/rows/LockForUpdateTest.java @@ -69,12 +69,18 @@ public void basicTest() { @Test void wrongEndpoint() { rowManager.withUpdate(false); - assertThrows( + FailedRequestException ex = assertThrows( FailedRequestException.class, () -> rowManager.execute(op.fromDocUris("/optic/test/musician1.json").lockForUpdate()), "Hoping to update this assertion to verify that the server message tells the user to hit v1/rows/update " + "instead; right now, it's mentioning using declareUpdate() which isn't applicable to a REST API user." ); + + assertTrue(ex.getMessage().contains( + "The Optic plan is attempting an update but was sent to the wrong REST API endpoint. " + + "You must invoke `withUpdate(true)` on the instance of com.marklogic.client.row.RowManager " + + "that you are using to submit the plan."), + "Unexpected message: " + ex.getMessage()); } @Test