1 package de.juplo.facebook.errors;
4 import com.fasterxml.jackson.core.JsonParseException;
5 import com.fasterxml.jackson.core.JsonProcessingException;
6 import com.fasterxml.jackson.databind.DeserializationFeature;
7 import com.fasterxml.jackson.databind.JsonMappingException;
8 import com.fasterxml.jackson.databind.ObjectMapper;
9 import com.fasterxml.jackson.databind.SerializationFeature;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
18 * Base exception for Facebook Graph-Api exceptions.
22 public class GraphApiException extends RuntimeException
24 public enum Type { OAuthException, GraphMethodException }
27 final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
28 final static ObjectMapper OBJECT_MAPPER;
30 private final FacebookErrorMessage error;
35 OBJECT_MAPPER = new ObjectMapper();
36 OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
37 OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);
38 OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
42 public static GraphApiException create(InputStream in)
48 return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
51 public static GraphApiException create(byte[] message)
57 return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
60 public static GraphApiException create(FacebookErrorMessage error)
62 // see: http://fbdevwiki.com/wiki/Error_codes
65 // 1..99: general errors
66 case 1: return new UnknownErrorException(error);
67 case 2: return new UnexpectedErrorException(error);
68 case 4: return new ApplicationRequestLimitReachedException(error);
69 case 10: return new AuthorizationMissingException(error);
70 case 12: return new DeprecatedException(error);
71 case 17: return new AccountRequestLimitReachedException(error);
72 case 21: return new PageMigratedException(error);
73 case 32: return new PageRequestLimitReachedException(error);
74 // 100..199: graph method errors
75 case 100: return new UnsupportedGetRequestException(error);
76 case 102: return new UserAccessTokenRequiredException(error);
77 case 104: return new AccessTokenRequiredException(error);
78 // 200..299: permission errors
178 case 299: return new AuthorizationMissingException(error);
179 // 200..299: permission errors
180 // 300..399: data editing errors ?
181 case 341: return new TemporaryRateLimitExceededException(error);
182 // 400..449: authentication error
183 // 450..499: session errors
184 // 500..599: application messaging errors ?
185 case 506: return new MultipleConcurrentPostsException(error);
186 // 600..699: FQL errors
187 case 613: return new CustomRequestLimitReachedException(error);
188 // 700..749: ref errors
189 // 750..799: application integration errors
190 // 900..949: application information errors
191 // 950..999: batch api errors
192 // 1000..1099: event api errors
193 // 1100..1199: live-message errors
194 case 1609005: return new LinkPostFailureException(error);
195 case 2200: return new CallbackVerificationFailedException(error);
198 LOG.info("unmapped error: {}", error);
199 return new UnmappedErrorException(error);
204 protected GraphApiException(FacebookErrorMessage error)
206 super(error.message);
211 public Type getType()
213 return error.type == null ? null : Type.valueOf(error.type);
216 public Integer getCode()
221 public Integer getSubCode()
223 return error.subCode;
226 public String getUserTitle()
228 return error.userTitle;
231 public String getUserMessage()
233 return error.userMessage;
236 public String getTraceId()
238 return error.traceId;
243 public String toString()
247 return OBJECT_MAPPER.writeValueAsString(error);
249 catch(JsonProcessingException e)
251 // This should never happen. But in case of a mistake: be verbose!
252 LOG.error("could not convert message into JSON: {}", e);
253 return e.getMessage();