From f22d6b44c7ca3f63d3fa60a9c98129137325a83d Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 10 Nov 2015 11:46:25 +0100 Subject: [PATCH] The types of the exeptions are now mapped to an enum Unknown types will result in an IllegalArgumentException when accessing the method GraphApiException.getType() of the parsed exception. --- .../exceptions/GraphApiException.java | 7 ++- .../client/GraphApiErrorHandlerTest.java | 43 +++++++++++++------ .../FacebookErrorMessageMappingTest.java | 5 ++- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java b/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java index 705caa4..9379abe 100644 --- a/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java +++ b/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java @@ -22,6 +22,9 @@ import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; */ public class GraphApiException extends OAuth2Exception { + public enum Type { OAuthException, GraphMethodException } + + final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class); final static ObjectMapper OBJECT_MAPPER; @@ -96,9 +99,9 @@ public class GraphApiException extends OAuth2Exception } - public String getType() + public Type getType() { - return error.type; + return error.type == null ? null : Type.valueOf(error.type); } public Integer getCode() diff --git a/src/test/java/de/juplo/facebook/client/GraphApiErrorHandlerTest.java b/src/test/java/de/juplo/facebook/client/GraphApiErrorHandlerTest.java index f0c26e9..c9921fa 100644 --- a/src/test/java/de/juplo/facebook/client/GraphApiErrorHandlerTest.java +++ b/src/test/java/de/juplo/facebook/client/GraphApiErrorHandlerTest.java @@ -6,6 +6,7 @@ import de.juplo.facebook.exceptions.UnsupportedGetRequestException; import de.juplo.facebook.exceptions.UnexpectedErrorException; import de.juplo.facebook.exceptions.RateExceededException; import de.juplo.facebook.exceptions.GraphApiException; +import de.juplo.facebook.exceptions.GraphApiException.Type; import de.juplo.facebook.exceptions.UnknownErrorException; import de.juplo.facebook.exceptions.PageMigratedException; import de.juplo.facebook.exceptions.UnmappedErrorException; @@ -84,7 +85,7 @@ public class GraphApiErrorHandlerTest assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(1), e.getCode()); assertEquals("An unknown error has occurred.", e.getMessage()); - assertEquals("OAuthException", e.getType()); + assertEquals(Type.OAuthException, e.getType()); } } @@ -115,7 +116,7 @@ public class GraphApiErrorHandlerTest assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(2), e.getCode()); assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage()); - assertEquals("OAuthException", e.getType()); + assertEquals(Type.OAuthException, e.getType()); } } @@ -146,7 +147,7 @@ public class GraphApiErrorHandlerTest assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(21), e.getCode()); assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID", e.getMessage()); - assertEquals("OAuthException", e.getType()); + assertEquals(Type.OAuthException, e.getType()); } } @@ -177,7 +178,7 @@ public class GraphApiErrorHandlerTest assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(100), e.getCode()); assertEquals("Unsupported get request.", e.getMessage()); - assertEquals("GraphMethodException", e.getType()); + assertEquals(Type.GraphMethodException, e.getType()); } } @@ -199,7 +200,7 @@ public class GraphApiErrorHandlerTest assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(104), e.getCode()); assertEquals("An access token is required to request this resource.", e.getMessage()); - assertEquals("OAuthException", e.getType()); + assertEquals(Type.OAuthException, e.getType()); assertEquals("E2Jjkj5++LL", e.getTraceId()); } } @@ -231,7 +232,7 @@ public class GraphApiErrorHandlerTest assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(613), e.getCode()); assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage()); - assertEquals("OAuthException", e.getType()); + assertEquals(Type.OAuthException, e.getType()); } } @@ -267,7 +268,7 @@ public class GraphApiErrorHandlerTest " \"error\":\n" + " {\n" + " \"message\": \"This error does not exist.\",\n" + - " \"type\": \"NonexistentException\",\n" + + " \"type\": \"NonexistentTypeException\",\n" + " \"code\": 999999999\n" + " }\n" + "}"); @@ -283,7 +284,13 @@ public class GraphApiErrorHandlerTest assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(999999999), e.getCode()); assertEquals("This error does not exist.", e.getMessage()); - assertEquals("NonexistentException", e.getType()); + try + { + Type type = e.getType(); + log.error("unknown type: {}", type); + fail("unmapped type was resolved by enum: " + type); + } + catch (IllegalArgumentException ee) {} } } @@ -298,7 +305,7 @@ public class GraphApiErrorHandlerTest " \"error\":\n" + " {\n" + " \"message\": null,\n" + - " \"type\": \"Whatever\",\n" + + " \"type\": \"WhateverTypeException\",\n" + " \"code\": 999999999\n" + " }\n" + "}"); @@ -312,7 +319,13 @@ public class GraphApiErrorHandlerTest { log.debug("{}", e.toString()); assertNull(e.getMessage()); - assertEquals("Whatever", e.getType()); + try + { + Type type = e.getType(); + log.error("unknown type: {}", type); + fail("unmapped type was resolved by enum: " + type); + } + catch (IllegalArgumentException ee) {} assertEquals(new Integer(999999999), e.getCode()); assertNull(e.getSubCode()); assertNull(e.getUserTitle()); @@ -329,7 +342,7 @@ public class GraphApiErrorHandlerTest "{\n" + " \"error\":\n" + " {\n" + - " \"type\": \"Whatever\",\n" + + " \"type\": \"WhateverTypeException\",\n" + " \"code\": 999999999\n" + " }\n" + "}"); @@ -343,7 +356,13 @@ public class GraphApiErrorHandlerTest { log.debug("{}", e.toString()); assertNull(e.getMessage()); - assertEquals("Whatever", e.getType()); + try + { + Type type = e.getType(); + log.error("unknown type: {}", type); + fail("unmapped type was resolved by enum: " + type); + } + catch (IllegalArgumentException ee) {} assertEquals(new Integer(999999999), e.getCode()); assertNull(e.getSubCode()); assertNull(e.getUserTitle()); diff --git a/src/test/java/de/juplo/facebook/exceptions/FacebookErrorMessageMappingTest.java b/src/test/java/de/juplo/facebook/exceptions/FacebookErrorMessageMappingTest.java index cb25662..3d672a4 100644 --- a/src/test/java/de/juplo/facebook/exceptions/FacebookErrorMessageMappingTest.java +++ b/src/test/java/de/juplo/facebook/exceptions/FacebookErrorMessageMappingTest.java @@ -3,6 +3,7 @@ package de.juplo.facebook.exceptions; import com.fasterxml.jackson.core.JsonProcessingException; import de.juplo.facebook.exceptions.GraphApiException.FacebookErrorMessage; import static de.juplo.facebook.exceptions.GraphApiException.OBJECT_MAPPER; +import de.juplo.facebook.exceptions.GraphApiException.Type; import java.io.IOException; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -31,7 +32,7 @@ public class FacebookErrorMessageMappingTest { FacebookErrorMessage error = new FacebookErrorMessage(); error.message = "Message describing the error"; - error.type = "OAuthException"; + error.type = Type.OAuthException.name(); error.code = 190; error.subCode = 460; error.userTitle = "A title"; @@ -48,7 +49,7 @@ public class FacebookErrorMessageMappingTest OBJECT_MAPPER.readValue(example, FacebookErrorMessage.class); assertEquals("Message describing the error", error.message); - assertEquals("OAuthException", error.type); + assertEquals(Type.OAuthException.name(), error.type); assertEquals(new Integer(190), error.code); assertEquals(new Integer(460), error.subCode); assertEquals("A title", error.userTitle); -- 2.20.1