1 package de.juplo.facebook.errors;
4 import com.fasterxml.jackson.core.JsonProcessingException;
5 import com.fasterxml.jackson.databind.DeserializationFeature;
6 import com.fasterxml.jackson.databind.ObjectMapper;
7 import com.fasterxml.jackson.databind.SerializationFeature;
8 import java.io.ByteArrayInputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.http.HttpHeaders;
14 import org.springframework.http.HttpStatus;
19 * Base exception for Facebook Graph-Api exceptions.
23 public class GraphApiException extends RuntimeException
25 public enum Type { OAuthException, GraphMethodException }
28 final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
29 final static ObjectMapper OBJECT_MAPPER;
31 public final HttpStatus status;
32 public final HttpHeaders headers;
33 public final FacebookErrorMessage error;
38 OBJECT_MAPPER = new ObjectMapper();
39 OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
40 OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);
41 OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
45 public static GraphApiException create(
53 return create(status, headers, OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
55 catch (IOException | RuntimeException e)
57 return new ErrorResponseParsingErrorException(status, headers, e);
61 public static GraphApiException create(
67 return create(status, headers, new ByteArrayInputStream(message));
70 public static GraphApiException create(
73 FacebookErrorMessage error
76 // see: http://fbdevwiki.com/wiki/Error_codes
79 // 1..99: general errors
80 case 1: return new UnknownErrorException(status, headers, error);
81 case 2: return new UnexpectedErrorException(status, headers, error);
82 case 4: return new ApplicationRequestLimitReachedException(status, headers, error);
83 case 10: return new AuthorizationMissingException(status, headers, error);
84 case 12: return new DeprecatedException(status, headers, error);
85 case 17: return new AccountRequestLimitReachedException(status, headers, error);
86 case 21: return new PageMigratedException(status, headers, error);
87 case 32: return new PageRequestLimitReachedException(status, headers, error);
88 // 100..199: graph method errors
89 case 100: return new UnsupportedGetRequestException(status, headers, error);
90 case 102: return new UserAccessTokenRequiredException(status, headers, error);
91 case 104: return new AccessTokenRequiredException(status, headers, error);
92 case 190: return new AccessTokenExpiredException(status, headers, error);
93 // 200..299: permission errors
193 case 299: return new AuthorizationMissingException(status, headers, error);
194 // 200..299: permission errors
195 // 300..399: data editing errors ?
196 case 341: return new TemporaryRateLimitExceededException(status, headers, error);
197 // 400..449: authentication error
198 // 450..499: session errors
199 // 500..599: application messaging errors ?
200 case 506: return new MultipleConcurrentPostsException(status, headers, error);
201 // 600..699: FQL errors
202 case 613: return new RateLimitExceededException(status, headers, error);
203 // 700..749: ref errors
204 // 750..799: application integration errors
205 // 900..949: application information errors
206 // 950..999: batch api errors
207 // 1000..1099: event api errors
208 // 1100..1199: live-message errors
209 case 1609005: return new LinkPostFailureException(status, headers, error);
210 case 2200: return new CallbackVerificationFailedException(status, headers, error);
213 LOG.info("unmapped error: {}", error);
214 return new UnmappedErrorException(status, headers, error);
219 protected GraphApiException(
222 FacebookErrorMessage error
225 super(error.message);
226 this.status = status;
227 this.headers = headers;
232 public HttpStatus getStatus()
237 public HttpHeaders getHeaders()
242 public Type getType()
244 return error.type == null ? null : Type.valueOf(error.type);
247 public Integer getCode()
252 public Integer getSubCode()
254 return error.subCode;
257 public String getUserTitle()
259 return error.userTitle;
262 public String getUserMessage()
264 return error.userMessage;
267 public String getTraceId()
269 return error.traceId;
274 public String toString()
278 return OBJECT_MAPPER.writeValueAsString(error);
280 catch(JsonProcessingException e)
282 // This should never happen. But in case of a mistake: be verbose!
283 LOG.error("could not convert message into JSON: {}", e);
284 return e.getMessage();