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;
14 import org.springframework.http.HttpHeaders;
15 import org.springframework.http.HttpStatus;
20 * Base exception for Facebook Graph-Api exceptions.
24 public class GraphApiException extends RuntimeException
26 public enum Type { OAuthException, GraphMethodException }
29 final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
30 final static ObjectMapper OBJECT_MAPPER;
32 public final HttpStatus status;
33 public final HttpHeaders headers;
34 public final FacebookErrorMessage error;
39 OBJECT_MAPPER = new ObjectMapper();
40 OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
41 OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);
42 OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
46 public static GraphApiException create(
56 return create(status, headers, OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
59 public static GraphApiException create(
69 return create(status, headers, OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
72 public static GraphApiException create(
75 FacebookErrorMessage error
78 // see: http://fbdevwiki.com/wiki/Error_codes
81 // 1..99: general errors
82 case 1: return new UnknownErrorException(status, headers, error);
83 case 2: return new UnexpectedErrorException(status, headers, error);
84 case 4: return new ApplicationRequestLimitReachedException(status, headers, error);
85 case 10: return new AuthorizationMissingException(status, headers, error);
86 case 12: return new DeprecatedException(status, headers, error);
87 case 17: return new AccountRequestLimitReachedException(status, headers, error);
88 case 21: return new PageMigratedException(status, headers, error);
89 case 32: return new PageRequestLimitReachedException(status, headers, error);
90 // 100..199: graph method errors
91 case 100: return new UnsupportedGetRequestException(status, headers, error);
92 case 102: return new UserAccessTokenRequiredException(status, headers, error);
93 case 104: return new AccessTokenRequiredException(status, headers, error);
94 // 200..299: permission errors
194 case 299: return new AuthorizationMissingException(status, headers, error);
195 // 200..299: permission errors
196 // 300..399: data editing errors ?
197 case 341: return new TemporaryRateLimitExceededException(status, headers, error);
198 // 400..449: authentication error
199 // 450..499: session errors
200 // 500..599: application messaging errors ?
201 case 506: return new MultipleConcurrentPostsException(status, headers, error);
202 // 600..699: FQL errors
203 case 613: return new RateLimitExceededException(status, headers, error);
204 // 700..749: ref errors
205 // 750..799: application integration errors
206 // 900..949: application information errors
207 // 950..999: batch api errors
208 // 1000..1099: event api errors
209 // 1100..1199: live-message errors
210 case 1609005: return new LinkPostFailureException(status, headers, error);
211 case 2200: return new CallbackVerificationFailedException(status, headers, error);
214 LOG.info("unmapped error: {}", error);
215 return new UnmappedErrorException(status, headers, error);
220 protected GraphApiException(
223 FacebookErrorMessage error
226 super(error.message);
227 this.status = status;
228 this.headers = headers;
233 public HttpStatus getStatus()
238 public HttpHeaders getHeaders()
243 public Type getType()
245 return error.type == null ? null : Type.valueOf(error.type);
248 public Integer getCode()
253 public Integer getSubCode()
255 return error.subCode;
258 public String getUserTitle()
260 return error.userTitle;
263 public String getUserMessage()
265 return error.userMessage;
268 public String getTraceId()
270 return error.traceId;
275 public String toString()
279 return OBJECT_MAPPER.writeValueAsString(error);
281 catch(JsonProcessingException e)
283 // This should never happen. But in case of a mistake: be verbose!
284 LOG.error("could not convert message into JSON: {}", e);
285 return e.getMessage();