Refactoring: moved FacebookErrorMessage in its own file/class
[facebook-utils] / src / main / java / de / juplo / facebook / exceptions / GraphApiException.java
index bd02565..375c5b8 100644 (file)
@@ -1,8 +1,5 @@
 package de.juplo.facebook.exceptions;
 
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonPropertyOrder;
-import com.fasterxml.jackson.annotation.JsonRootName;
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -11,6 +8,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
 import java.io.IOException;
 import java.io.InputStream;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
 
 /**
@@ -20,6 +19,10 @@ 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;
 
   private final FacebookErrorMessage error;
@@ -63,6 +66,7 @@ public class GraphApiException extends OAuth2Exception
       case 21:    return new PageMigratedException(error);
       // 100..199: graph method errors
       case 100:   return new UnsupportedGetRequestException(error);
+      case 104:   return new AccessTokenRequiredException(error);
       // 200..299: permission errors
       // 300..399: data editing errors
       // 400..449: authentication error
@@ -76,8 +80,11 @@ public class GraphApiException extends OAuth2Exception
       // 950..999: batch api errors
       // 1000..1099: event api errors
       // 1100..1199: live-message errors
+      case 2200:  return new CallbackVerificationFailedException(error);
 
-      default:    return new UnmappedErrorException(error);
+      default:
+        LOG.info("unmapped error: {}", error);
+        return new UnmappedErrorException(error);
     }
   }
 
@@ -89,9 +96,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()
@@ -129,33 +136,9 @@ public class GraphApiException extends OAuth2Exception
     }
     catch(JsonProcessingException e)
     {
-      return "Could not convert error in JSON-representation: " + e.getMessage();
+      // This should never happen. But in case of a mistake: be verbose!
+      LOG.error("could not convert message into JSON: {}", e);
+      return e.getMessage();
     }
   }
-
-
-  /**
-   * This class represents an error message from the Graph-API
-   *
-   * @see <a href="https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors">Graph-API Documentation</a>
-   */
-  @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;
-  }
 }