Added JSON-mapping for responses from the Graph-API
authorKai Moritz <kai@juplo.de>
Sat, 7 Nov 2015 10:14:16 +0000 (11:14 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 10 Nov 2015 14:51:14 +0000 (15:51 +0100)
Take a look at the Graph-API documentation:
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors

pom.xml
src/main/java/de/juplo/facebook/exceptions/GraphApiException.java
src/test/java/de/juplo/facebook/exceptions/FacebookErrorMessageMappingTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 2543a7d..f4bd89c 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <scope>provided</scope>
     </dependency>
 
-    <!-- Needed to compile agains spring-security-oauth2 -->
+    <!-- Needed for the mapping of error responses -->
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-core</artifactId>
+      <version>${jackson.version}</version>
+    </dependency>
     <dependency>
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-databind</artifactId>
       <version>${jackson.version}</version>
     </dependency>
+
+    <!-- Needed to compile agains spring-security-oauth2 -->
     <dependency>
       <groupId>org.springframework.security</groupId>
       <artifactId>spring-security-core</artifactId>
index 03aa627..1055b20 100644 (file)
@@ -1,5 +1,8 @@
 package de.juplo.facebook.exceptions;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.annotation.JsonRootName;
 import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
 
 /**
@@ -59,4 +62,30 @@ public class GraphApiException extends OAuth2Exception
     builder.append("}}");
     return builder.toString();
   }
+
+
+  /**
+   * This class represents an error message from the Graph-API
+   *
+   * @see https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors
+   */
+  @JsonRootName("error")
+  @JsonPropertyOrder({ "message", "type", "code", "error_subcode", "error_user_title", "error_user_msg", "fbtrace_id" })
+  public static class FacebookErrorMessage
+  {
+    @JsonProperty("message")
+    String message;
+    @JsonProperty("type")
+    String type;
+    @JsonProperty("code")
+    Integer code;
+    @JsonProperty("error_subcode")
+    Integer subCode;
+    @JsonProperty("error_user_title")
+    String userTitle;
+    @JsonProperty("error_user_msg")
+    String userMessage;
+    @JsonProperty("fbtrace_id")
+    String traceId;
+  }
 }
diff --git a/src/test/java/de/juplo/facebook/exceptions/FacebookErrorMessageMappingTest.java b/src/test/java/de/juplo/facebook/exceptions/FacebookErrorMessageMappingTest.java
new file mode 100644 (file)
index 0000000..cd1a4c8
--- /dev/null
@@ -0,0 +1,72 @@
+package de.juplo.facebook.exceptions;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import de.juplo.facebook.exceptions.GraphApiException.FacebookErrorMessage;
+import java.io.IOException;
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Kai Moritz
+ */
+public class FacebookErrorMessageMappingTest
+{
+  final String example =
+      "{" +
+          "\"error\":{" +
+              "\"message\":\"Message describing the error\"," +
+              "\"type\":\"OAuthException\"," +
+              "\"code\":190," +
+              "\"error_subcode\":460," +
+              "\"error_user_title\":\"A title\"," +
+              "\"error_user_msg\":\"A message\"," +
+              "\"fbtrace_id\":\"EJplcsCHuLu\"" +
+          "}" +
+      "}";
+
+  ObjectMapper mapper;
+
+
+  @Before
+  public void setUp()
+  {
+    mapper = new ObjectMapper();
+    mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
+    mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
+  }
+
+
+  @Test
+  public void testSerialize() throws JsonProcessingException
+  {
+    FacebookErrorMessage error = new FacebookErrorMessage();
+    error.message = "Message describing the error";
+    error.type = "OAuthException";
+    error.code = 190;
+    error.subCode = 460;
+    error.userTitle = "A title";
+    error.userMessage = "A message";
+    error.traceId = "EJplcsCHuLu";
+
+    assertEquals(example, mapper.writeValueAsString(error));
+  }
+
+  @Test
+  public void testDeserialize() throws IOException
+  {
+    FacebookErrorMessage error =
+        mapper.readValue(example, FacebookErrorMessage.class);
+
+    assertEquals("Message describing the error", error.message);
+    assertEquals("OAuthException", error.type);
+    assertEquals(new Integer(190), error.code);
+    assertEquals(new Integer(460), error.subCode);
+    assertEquals("A title", error.userTitle);
+    assertEquals("A message", error.userMessage);
+    assertEquals("EJplcsCHuLu", error.traceId);
+  }
+}