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.security.oauth2.common.exceptions.OAuth2Exception;
19 * Base exception for Facebook Graph-Api exceptions.
23 public class GraphApiException extends OAuth2Exception
25 public enum Type { OAuthException, GraphMethodException }
28 final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
29 final static ObjectMapper OBJECT_MAPPER;
31 private final FacebookErrorMessage error;
36 OBJECT_MAPPER = new ObjectMapper();
37 OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
38 OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);
39 OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
43 public static GraphApiException create(InputStream in)
49 return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
52 public static GraphApiException create(byte[] message)
58 return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
61 public static GraphApiException create(FacebookErrorMessage error)
63 // see: http://fbdevwiki.com/wiki/Error_codes
66 // 1..99: general errors
67 case 1: return new UnknownErrorException(error);
68 case 2: return new UnexpectedErrorException(error);
69 case 21: return new PageMigratedException(error);
70 // 100..199: graph method errors
71 case 100: return new UnsupportedGetRequestException(error);
72 case 104: return new AccessTokenRequiredException(error);
73 // 200..299: permission errors
74 // 300..399: data editing errors
75 // 400..449: authentication error
76 // 450..499: session errors
77 // 500..599: application messaging errors
78 // 600..699: FQL errors
79 case 613: return new RateExceededException(error);
80 // 700..749: ref errors
81 // 750..799: application integration errors
82 // 900..949: application information errors
83 // 950..999: batch api errors
84 // 1000..1099: event api errors
85 // 1100..1199: live-message errors
86 case 2200: return new CallbackVerificationFailedException(error);
89 LOG.info("unmapped error: {}", error);
90 return new UnmappedErrorException(error);
95 protected GraphApiException(FacebookErrorMessage error)
102 public Type getType()
104 return error.type == null ? null : Type.valueOf(error.type);
107 public Integer getCode()
112 public Integer getSubCode()
114 return error.subCode;
117 public String getUserTitle()
119 return error.userTitle;
122 public String getUserMessage()
124 return error.userMessage;
127 public String getTraceId()
129 return error.traceId;
134 public String toString()
138 return OBJECT_MAPPER.writeValueAsString(error);
140 catch(JsonProcessingException e)
142 // This should never happen. But in case of a mistake: be verbose!
143 LOG.error("could not convert message into JSON: {}", e);
144 return e.getMessage();