Errors during parsing of the error-message are wrapped
authorKai Moritz <kai@jupl.de>
Wed, 20 Nov 2019 13:16:14 +0000 (14:16 +0100)
committerKai Moritz <kai@jupl.de>
Wed, 20 Nov 2019 23:30:04 +0000 (00:30 +0100)
* GraphApiException.create() catches any exceptions, thrown during parsing
* Catched exceptions are wrapped with ErrorResponseParsingErrorException
* This exception is then returned as the result of the translation-attempt
* Adjusted GraphApiErrorHandlerTest accordingly

src/main/java/de/juplo/facebook/errors/ErrorResponseParsingErrorException.java [new file with mode: 0644]
src/main/java/de/juplo/facebook/errors/FacebookErrorMessage.java
src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java
src/main/java/de/juplo/facebook/errors/GraphApiException.java

diff --git a/src/main/java/de/juplo/facebook/errors/ErrorResponseParsingErrorException.java b/src/main/java/de/juplo/facebook/errors/ErrorResponseParsingErrorException.java
new file mode 100644 (file)
index 0000000..60dcb6b
--- /dev/null
@@ -0,0 +1,38 @@
+package de.juplo.facebook.errors;
+
+
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+
+
+
+
+/**
+ * Exception that is thrown, if anything went wrong while parsing the
+ * error-response.
+ *
+ * @author Kai Moritz
+ */
+public class ErrorResponseParsingErrorException extends GraphApiException
+{
+  public final Exception cause;
+
+
+  protected  ErrorResponseParsingErrorException(
+      HttpStatus status,
+      HttpHeaders headers,
+      Exception cause
+      )
+  {
+    super(status, headers, new FacebookErrorMessage(cause));
+    this.cause = cause;
+  }
+
+
+
+  @Override
+  public String toString()
+  {
+    return "Error during parsing the error-response: " + cause.toString();
+  }
+}
index f807bc7..b11d98d 100644 (file)
@@ -53,4 +53,12 @@ public class FacebookErrorMessage
   @JsonProperty("is_transient")
   @JsonInclude(NON_EMPTY)
   Boolean isTransient;
+
+
+  FacebookErrorMessage() {}
+
+  FacebookErrorMessage(Exception e)
+  {
+    message = e.toString();
+  }
 }
index 123c7f3..41f00fe 100644 (file)
@@ -87,24 +87,17 @@ public class GraphApiErrorResponseErrorHandler implements ResponseErrorHandler
     final byte[] body = FileCopyUtils.copyToByteArray(response.getBody());
     GraphApiException error;
 
-    try
+    error = GraphApiException.create(response.getStatusCode(), response.getHeaders(), body);
+    if (LOG.isInfoEnabled())
+      LOG.info("error-response: {}", new String(body, Charset.forName("UTF-8")));
+    if (!error.getClass().equals(ErrorResponseParsingErrorException.class))
     {
-      error = GraphApiException.create(response.getStatusCode(), response.getHeaders(), body);
-      if (LOG.isInfoEnabled())
-        LOG.info("error-response: {}", new String(body, Charset.forName("UTF-8")));
+      throw error;
     }
-    catch (Exception e)
+    else
     {
       // The body of the HTTP-message could not be parsed.
       // Let the parent error-handler try to handle the response.
-
-      LOG.warn(
-          "Could not convert the response into an exception, " +
-          "because the body is unparsable: error={}, body={}",
-          e.toString(),
-          new String(body, Charset.forName("UTF-8"))
-          );
-
       // To do so, we have to wrap the original response to fill in
       // the buffered body, if needed
       ClientHttpResponse buffered = new ClientHttpResponse()
@@ -149,7 +142,5 @@ public class GraphApiErrorResponseErrorHandler implements ResponseErrorHandler
       parent.handleError(buffered);
       return;
     }
-
-    throw error;
   }
 }
index 1b7eaaa..636b3d2 100644 (file)
@@ -1,12 +1,11 @@
 package de.juplo.facebook.errors;
 
 
-import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.JsonMappingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import org.slf4j.Logger;
@@ -48,12 +47,15 @@ public class GraphApiException extends RuntimeException
       HttpHeaders headers,
       InputStream in
       )
-      throws
-        IOException,
-        JsonParseException,
-        JsonMappingException
   {
-    return create(status, headers, OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
+    try
+    {
+      return create(status, headers, OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
+    }
+    catch (IOException | RuntimeException e)
+    {
+      return new ErrorResponseParsingErrorException(status, headers, e);
+    }
   }
 
   public static GraphApiException create(
@@ -61,12 +63,8 @@ public class GraphApiException extends RuntimeException
       HttpHeaders headers,
       byte[] message
       )
-      throws
-        IOException,
-        JsonParseException,
-        JsonMappingException
   {
-    return create(status, headers, OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
+    return create(status, headers, new ByteArrayInputStream(message));
   }
 
   public static GraphApiException create(