SLING-11366: Provide more exception error context on the JSON API responses (#59)
* SLING-11366: Provide more exception error context on the JSON API responses
* SLING-11366: Provide more exception error context on the JSON API responses
* SLING-11366: Provide more exception error context on the JSON API responses
Co-authored-by: josec <josec@adobe.com>
diff --git a/src/main/java/org/apache/sling/distribution/servlet/DistributionAgentServlet.java b/src/main/java/org/apache/sling/distribution/servlet/DistributionAgentServlet.java
index 98acf36..54df02c 100644
--- a/src/main/java/org/apache/sling/distribution/servlet/DistributionAgentServlet.java
+++ b/src/main/java/org/apache/sling/distribution/servlet/DistributionAgentServlet.java
@@ -21,6 +21,8 @@
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
@@ -71,7 +73,7 @@
log.debug("distribution response : {}", distributionResponse);
} catch (Throwable e) {
log.error("an unexpected error has occurred", e);
- ServletJsonUtils.writeJson(response, 503, "an unexpected error has occurred", null);
+ ServletJsonUtils.writeJson(response, 503, e.getMessage(), null);
}
} else {
ServletJsonUtils.writeJson(response, 404, "agent not found", null);
diff --git a/src/main/java/org/apache/sling/distribution/servlet/DistributionPackageImporterServlet.java b/src/main/java/org/apache/sling/distribution/servlet/DistributionPackageImporterServlet.java
index dad37de..4f2aedf 100644
--- a/src/main/java/org/apache/sling/distribution/servlet/DistributionPackageImporterServlet.java
+++ b/src/main/java/org/apache/sling/distribution/servlet/DistributionPackageImporterServlet.java
@@ -18,6 +18,7 @@
*/
package org.apache.sling.distribution.servlet;
+import static java.lang.String.format;
import static javax.servlet.http.HttpServletResponse.*;
import static org.apache.sling.distribution.util.impl.DigestUtils.openDigestInputStream;
import static org.apache.sling.distribution.util.impl.DigestUtils.readDigestMessage;
@@ -112,7 +113,7 @@
String receivedDigestMessage = readDigestMessage((DigestInputStream) stream);
if (!digestMessage.equalsIgnoreCase(receivedDigestMessage)) {
log.error("Error during distribution import: received distribution package is corrupted, expected [{}] but received [{}]",
- digestMessage, receivedDigestMessage);
+ digestMessage, receivedDigestMessage);
Map<String, String> kv = new HashMap<String, String>();
kv.put("digestAlgorithm", digestAlgorithm);
kv.put("expected", digestMessage);
@@ -126,8 +127,11 @@
ServletJsonUtils.writeJson(response, SC_OK, "package imported successfully", null);
} catch (final Throwable e) {
- ServletJsonUtils.writeJson(response, SC_INTERNAL_SERVER_ERROR, "an unexpected error has occurred during distribution import", null);
- log.error("Error during distribution import", e);
+ String msg = format("an unexpected error has occurred during distribution import. " +
+ "Error: %s",
+ e.getMessage());
+ log.error(msg, e);
+ ServletJsonUtils.writeJson(response, SC_INTERNAL_SERVER_ERROR, msg, null);
} finally {
long end = System.currentTimeMillis();
log.debug("Processed package import request in {} ms", end - start);