From 52a787e311f50618a44459118e40c4d308070429 Mon Sep 17 00:00:00 2001 From: Timothy Potter Date: Tue, 23 Sep 2014 20:29:14 +0000 Subject: [PATCH] SOLR-6550: Provide simple mechanism for passing additional metadata / context about a server-side SolrException back to the client-side git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1627154 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/solr/servlet/ResponseUtils.java | 6 ++++- .../impl/ConcurrentUpdateSolrServer.java | 17 +++++++++++++- .../client/solrj/impl/HttpSolrServer.java | 6 ++++- .../org/apache/solr/common/SolrException.java | 22 +++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/servlet/ResponseUtils.java b/solr/core/src/java/org/apache/solr/servlet/ResponseUtils.java index 8422691db01..73d8f60a2bb 100644 --- a/solr/core/src/java/org/apache/solr/servlet/ResponseUtils.java +++ b/solr/core/src/java/org/apache/solr/servlet/ResponseUtils.java @@ -40,7 +40,11 @@ public class ResponseUtils { public static int getErrorInfo(Throwable ex, NamedList info, Logger log) { int code = 500; if (ex instanceof SolrException) { - code = ((SolrException)ex).code(); + SolrException solrExc = (SolrException)ex; + code = solrExc.code(); + NamedList errorMetadata = solrExc.getMetadata(); + if (errorMetadata != null) + info.add("metadata", errorMetadata); } for (Throwable th = ex; th != null; th = th.getCause()) { diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrServer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrServer.java index 2016ab28e3e..a784966a361 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrServer.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrServer.java @@ -237,7 +237,22 @@ public class ConcurrentUpdateSolrServer extends SolrServer { msg.append(response.getStatusLine().getReasonPhrase()); msg.append("\n\n\n\n"); msg.append("request: ").append(method.getURI()); - handleError(new SolrException(ErrorCode.getErrorCode(statusCode), msg.toString())); + + SolrException solrExc = new SolrException(ErrorCode.getErrorCode(statusCode), msg.toString()); + // parse out the metadata from the SolrException + try { + NamedList resp = + server.parser.processResponse(response.getEntity().getContent(), + response.getEntity().getContentType().getValue()); + NamedList error = (NamedList) resp.get("error"); + if (error != null) + solrExc.setMetadata((NamedList) error.get("metadata")); + } catch (Exception exc) { + // don't want to fail to report error if parsing the response fails + log.warn("Failed to parse error response from "+server.getBaseURL()+" due to: "+exc); + } + + handleError(solrExc); } else { onSuccess(response); } diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java index d60db52405a..e612adff4a8 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrServer.java @@ -530,6 +530,7 @@ public class HttpSolrServer extends SolrServer { throw new RemoteSolrException(httpStatus, e.getMessage(), e); } if (httpStatus != HttpStatus.SC_OK) { + NamedList metadata = null; String reason = null; try { NamedList err = (NamedList) rsp.get("error"); @@ -538,6 +539,7 @@ public class HttpSolrServer extends SolrServer { if(reason == null) { reason = (String) err.get("trace"); } + metadata = (NamedList)err.get("metadata"); } } catch (Exception ex) {} if (reason == null) { @@ -547,7 +549,9 @@ public class HttpSolrServer extends SolrServer { msg.append("request: " + method.getURI()); reason = java.net.URLDecoder.decode(msg.toString(), UTF_8); } - throw new RemoteSolrException(httpStatus, reason, null); + RemoteSolrException rss = new RemoteSolrException(httpStatus, reason, null); + if (metadata != null) rss.setMetadata(metadata); + throw rss; } success = true; return rsp; diff --git a/solr/solrj/src/java/org/apache/solr/common/SolrException.java b/solr/solrj/src/java/org/apache/solr/common/SolrException.java index f0723e28464..02bbc045004 100644 --- a/solr/solrj/src/java/org/apache/solr/common/SolrException.java +++ b/solr/solrj/src/java/org/apache/solr/common/SolrException.java @@ -23,6 +23,7 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.solr.common.util.NamedList; import org.slf4j.Logger; /** @@ -86,6 +87,7 @@ public class SolrException extends RuntimeException { } int code=0; + protected NamedList metadata; /** * The HTTP Status code associated with this Exception. For SolrExceptions @@ -98,6 +100,26 @@ public class SolrException extends RuntimeException { */ public int code() { return code; } + public void setMetadata(NamedList metadata) { + this.metadata = metadata; + } + + public NamedList getMetadata() { + return metadata; + } + + public String getMetadata(String key) { + return (metadata != null && key != null) ? metadata.get(key) : null; + } + + public void setMetadata(String key, String value) { + if (key == null || value == null) + throw new IllegalArgumentException("Exception metadata cannot be null!"); + + if (metadata == null) + metadata = new NamedList(); + metadata.add(key, value); + } public void log(Logger log) { log(log,this); } public static void log(Logger log, Throwable e) {