You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ho...@apache.org on 2023/08/23 21:13:01 UTC

[solr] branch main updated: SOLR-16933: Use JSONMapResponseWriter for CLI errors

This is an automated email from the ASF dual-hosted git repository.

houston pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/main by this push:
     new 57acde3752b SOLR-16933: Use JSONMapResponseWriter for CLI errors
57acde3752b is described below

commit 57acde3752bec704442cd08d54fc516f93ee4169
Author: Houston Putman <ho...@apache.org>
AuthorDate: Wed Aug 23 16:49:01 2023 -0400

    SOLR-16933: Use JSONMapResponseWriter for CLI errors
    
    Related to #1863
---
 solr/core/src/java/org/apache/solr/cli/ApiTool.java           | 11 ++++++++---
 solr/core/src/java/org/apache/solr/cli/CreateTool.java        | 11 ++++++++---
 solr/core/src/java/org/apache/solr/cli/DeleteTool.java        | 10 ++++++++--
 .../org/apache/solr/client/solrj/impl/Http2SolrClient.java    |  8 +++++++-
 4 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/cli/ApiTool.java b/solr/core/src/java/org/apache/solr/cli/ApiTool.java
index 82980ea5257..8ecfcd36a40 100644
--- a/solr/core/src/java/org/apache/solr/cli/ApiTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/ApiTool.java
@@ -23,10 +23,12 @@ import java.util.List;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 import org.apache.solr.client.solrj.SolrRequest;
-import org.apache.solr.client.solrj.impl.NoOpResponseParser;
+import org.apache.solr.client.solrj.impl.JsonMapResponseParser;
 import org.apache.solr.client.solrj.request.GenericSolrRequest;
 import org.apache.solr.common.params.ModifiableSolrParams;
 import org.apache.solr.common.util.NamedList;
+import org.noggit.CharArr;
+import org.noggit.JSONWriter;
 
 /**
  * Supports api command in the bin/solr script.
@@ -90,9 +92,12 @@ public class ApiTool extends ToolBase {
       // When trying to re-write into JSON, the JSONWriter doesn't have the right info to print it
       // correctly.
       // All we want to do is pass the JSON response to the user, so do that.
-      req.setResponseParser(new NoOpResponseParser("json"));
+      req.setResponseParser(new JsonMapResponseParser());
       NamedList<Object> response = solrClient.request(req);
-      return (String) response.get("response");
+      // pretty-print the response to stdout
+      CharArr arr = new CharArr();
+      new JSONWriter(arr, 2).write(response.asMap());
+      return arr.toString();
     }
   }
 
diff --git a/solr/core/src/java/org/apache/solr/cli/CreateTool.java b/solr/core/src/java/org/apache/solr/cli/CreateTool.java
index cd7aaddb1f1..e62c42d185a 100644
--- a/solr/core/src/java/org/apache/solr/cli/CreateTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/CreateTool.java
@@ -38,7 +38,7 @@ import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.impl.CloudHttp2SolrClient;
 import org.apache.solr.client.solrj.impl.CloudSolrClient;
 import org.apache.solr.client.solrj.impl.Http2SolrClient;
-import org.apache.solr.client.solrj.impl.NoOpResponseParser;
+import org.apache.solr.client.solrj.impl.JsonMapResponseParser;
 import org.apache.solr.client.solrj.request.CollectionAdminRequest;
 import org.apache.solr.client.solrj.request.CoreAdminRequest;
 import org.apache.solr.client.solrj.request.GenericSolrRequest;
@@ -49,6 +49,8 @@ import org.apache.solr.common.params.CollectionAdminParams;
 import org.apache.solr.common.params.CommonParams;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.core.ConfigSetService;
+import org.noggit.CharArr;
+import org.noggit.JSONWriter;
 
 /** Supports create command in the bin/solr script. */
 public class CreateTool extends ToolBase {
@@ -289,7 +291,7 @@ public class CreateTool extends ToolBase {
       var req =
           CollectionAdminRequest.createCollection(
               collectionName, confName, numShards, replicationFactor);
-      req.setResponseParser(new NoOpResponseParser("json"));
+      req.setResponseParser(new JsonMapResponseParser());
       response = cloudSolrClient.request(req);
     } catch (SolrServerException sse) {
       throw new Exception(
@@ -297,7 +299,10 @@ public class CreateTool extends ToolBase {
     }
 
     if (cli.hasOption(SolrCLI.OPTION_VERBOSE.getOpt())) {
-      echo((String) response.get("response"));
+      // pretty-print the response to stdout
+      CharArr arr = new CharArr();
+      new JSONWriter(arr, 2).write(response.asMap());
+      echo(arr.toString());
     } else {
       String endMessage =
           String.format(
diff --git a/solr/core/src/java/org/apache/solr/cli/DeleteTool.java b/solr/core/src/java/org/apache/solr/cli/DeleteTool.java
index 1b8eec4e7bf..bae371226ae 100644
--- a/solr/core/src/java/org/apache/solr/cli/DeleteTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/DeleteTool.java
@@ -32,11 +32,14 @@ import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.impl.CloudHttp2SolrClient;
 import org.apache.solr.client.solrj.impl.CloudSolrClient;
 import org.apache.solr.client.solrj.impl.Http2SolrClient;
+import org.apache.solr.client.solrj.impl.JsonMapResponseParser;
 import org.apache.solr.client.solrj.impl.NoOpResponseParser;
 import org.apache.solr.client.solrj.request.CollectionAdminRequest;
 import org.apache.solr.client.solrj.request.CoreAdminRequest;
 import org.apache.solr.common.cloud.ZkStateReader;
 import org.apache.solr.common.util.NamedList;
+import org.noggit.CharArr;
+import org.noggit.JSONWriter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -173,7 +176,7 @@ public class DeleteTool extends ToolBase {
     NamedList<Object> response;
     try {
       var req = CollectionAdminRequest.deleteCollection(collectionName);
-      req.setResponseParser(new NoOpResponseParser("json"));
+      req.setResponseParser(new JsonMapResponseParser());
       response = cloudSolrClient.request(req);
     } catch (SolrServerException sse) {
       throw new Exception(
@@ -195,7 +198,10 @@ public class DeleteTool extends ToolBase {
     }
 
     if (response != null) {
-      echo((String) response.get("response"));
+      // pretty-print the response to stdout
+      CharArr arr = new CharArr();
+      new JSONWriter(arr, 2).write(response.asMap());
+      echo(arr.toString());
       echo("\n");
     }
 
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java
index 867b5edc85d..73001f7685f 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java
@@ -871,6 +871,9 @@ public class Http2SolrClient extends SolrClient {
       }
 
       Object error = rsp == null ? null : rsp.get("error");
+      if (rsp != null && error == null && processor instanceof NoOpResponseParser) {
+        error = rsp.get("response");
+      }
       if (error != null
           && (String.valueOf(getObjectByPath(error, true, errPath))
               .endsWith("ExceptionWithErrObject"))) {
@@ -907,9 +910,12 @@ public class Http2SolrClient extends SolrClient {
         if (reason == null) {
           StringBuilder msg = new StringBuilder();
           msg.append(response.getReason())
-              .append("\n\n")
+              .append("\n")
               .append("request: ")
               .append(response.getRequest().getMethod());
+          if (error != null) {
+            msg.append("\n\nError returned:\n").append(error);
+          }
           reason = java.net.URLDecoder.decode(msg.toString(), FALLBACK_CHARSET);
         }
         RemoteSolrException rss =