<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>
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;
/**
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;
+ }
}
--- /dev/null
+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);
+ }
+}