The exception stores the HttpStatus and HttpHeaders of the failed request
[facebook-errors] / src / main / java / de / juplo / facebook / errors / GraphApiException.java
index 53bbbe6..7263c4f 100644 (file)
@@ -11,6 +11,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
 
 
 
@@ -27,7 +29,9 @@ public class GraphApiException extends RuntimeException
   final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
   final static ObjectMapper OBJECT_MAPPER;
 
-  private final FacebookErrorMessage error;
+  public final HttpStatus status;
+  public final HttpHeaders headers;
+  public final FacebookErrorMessage error;
 
 
   static
@@ -39,42 +43,54 @@ public class GraphApiException extends RuntimeException
   }
 
 
-  public static GraphApiException create(InputStream in)
+  public static GraphApiException create(
+      HttpStatus status,
+      HttpHeaders headers,
+      InputStream in
+      )
       throws
         IOException,
         JsonParseException,
         JsonMappingException
   {
-    return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
+    return create(status, headers, OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
   }
 
-  public static GraphApiException create(byte[] message)
+  public static GraphApiException create(
+      HttpStatus status,
+      HttpHeaders headers,
+      byte[] message
+      )
       throws
         IOException,
         JsonParseException,
         JsonMappingException
   {
-    return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
+    return create(status, headers, OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
   }
 
-  public static GraphApiException create(FacebookErrorMessage error)
+  public static GraphApiException create(
+      HttpStatus status,
+      HttpHeaders headers,
+      FacebookErrorMessage error
+      )
   {
     // see: http://fbdevwiki.com/wiki/Error_codes
     switch(error.code)
     {
       // 1..99: general errors
-      case 1:     return new UnknownErrorException(error);
-      case 2:     return new UnexpectedErrorException(error);
-      case 4:     return new ApplicationRequestLimitReachedException(error);
-      case 10:    return new AuthorizationMissingException(error);
-      case 12:    return new DeprecatedException(error);
-      case 17:    return new AccountRequestLimitReachedException(error);
-      case 21:    return new PageMigratedException(error);
-      case 32:    return new PageRequestLimitReachedException(error);
+      case 1:     return new UnknownErrorException(status, headers, error);
+      case 2:     return new UnexpectedErrorException(status, headers, error);
+      case 4:     return new ApplicationRequestLimitReachedException(status, headers, error);
+      case 10:    return new AuthorizationMissingException(status, headers, error);
+      case 12:    return new DeprecatedException(status, headers, error);
+      case 17:    return new AccountRequestLimitReachedException(status, headers, error);
+      case 21:    return new PageMigratedException(status, headers, error);
+      case 32:    return new PageRequestLimitReachedException(status, headers, error);
       // 100..199: graph method errors
-      case 100:   return new UnsupportedGetRequestException(error);
-      case 102:   return new UserAccessTokenRequiredException(error);
-      case 104:   return new AccessTokenRequiredException(error);
+      case 100:   return new UnsupportedGetRequestException(status, headers, error);
+      case 102:   return new UserAccessTokenRequiredException(status, headers, error);
+      case 104:   return new AccessTokenRequiredException(status, headers, error);
       // 200..299: permission errors
       case 200:
       case 201:
@@ -175,39 +191,55 @@ public class GraphApiException extends RuntimeException
       case 296:
       case 297:
       case 298:
-      case 299:   return new AuthorizationMissingException(error);
+      case 299:   return new AuthorizationMissingException(status, headers, error);
       // 200..299: permission errors
       // 300..399: data editing errors ?
-      case 341:   return new TemporaryRateLimitExceededException(error);
+      case 341:   return new TemporaryRateLimitExceededException(status, headers, error);
       // 400..449: authentication error
       // 450..499: session errors
       // 500..599: application messaging errors ?
-      case 506:   return new MultipleConcurrentPostsException(error);
+      case 506:   return new MultipleConcurrentPostsException(status, headers, error);
       // 600..699: FQL errors
-      case 613:   return new CustomRequestLimitReachedException(error);
+      case 613:   return new CustomRequestLimitReachedException(status, headers, error);
       // 700..749: ref errors
       // 750..799: application integration errors
       // 900..949: application information errors
       // 950..999: batch api errors
       // 1000..1099: event api errors
       // 1100..1199: live-message errors
-      case 1609005: return new LinkPostFailureException(error);
-      case 2200:  return new CallbackVerificationFailedException(error);
+      case 1609005: return new LinkPostFailureException(status, headers, error);
+      case 2200:  return new CallbackVerificationFailedException(status, headers, error);
 
       default:
         LOG.info("unmapped error: {}", error);
-        return new UnmappedErrorException(error);
+        return new UnmappedErrorException(status, headers, error);
     }
   }
 
 
-  protected GraphApiException(FacebookErrorMessage error)
+  protected GraphApiException(
+      HttpStatus status,
+      HttpHeaders headers,
+      FacebookErrorMessage error
+      )
   {
     super(error.message);
+    this.status = status;
+    this.headers = headers;
     this.error = error;
   }
 
 
+  public HttpStatus getStatus()
+  {
+    return status;
+  }
+
+  public HttpHeaders getHeaders()
+  {
+    return headers;
+  }
+
   public Type getType()
   {
     return error.type == null ? null : Type.valueOf(error.type);