1 package de.juplo.facebook.exceptions;
3 import com.fasterxml.jackson.core.JsonParseException;
4 import com.fasterxml.jackson.core.JsonProcessingException;
5 import com.fasterxml.jackson.databind.DeserializationFeature;
6 import com.fasterxml.jackson.databind.JsonMappingException;
7 import com.fasterxml.jackson.databind.ObjectMapper;
8 import com.fasterxml.jackson.databind.SerializationFeature;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
16 * Base exception for Facebook Graph-Api exceptions.
20 public class GraphApiException extends OAuth2Exception
22 public enum Type { OAuthException, GraphMethodException }
25 final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
26 final static ObjectMapper OBJECT_MAPPER;
28 private final FacebookErrorMessage error;
33 OBJECT_MAPPER = new ObjectMapper();
34 OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
35 OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);
36 OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
40 public static GraphApiException create(InputStream in)
46 return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
49 public static GraphApiException create(byte[] message)
55 return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
58 public static GraphApiException create(FacebookErrorMessage error)
60 // see: http://fbdevwiki.com/wiki/Error_codes
63 // 1..99: general errors
64 case 1: return new UnknownErrorException(error);
65 case 2: return new UnexpectedErrorException(error);
66 case 21: return new PageMigratedException(error);
67 // 100..199: graph method errors
68 case 100: return new UnsupportedGetRequestException(error);
69 case 104: return new AccessTokenRequiredException(error);
70 // 200..299: permission errors
71 // 300..399: data editing errors
72 // 400..449: authentication error
73 // 450..499: session errors
74 // 500..599: application messaging errors
75 // 600..699: FQL errors
76 case 613: return new RateExceededException(error);
77 // 700..749: ref errors
78 // 750..799: application integration errors
79 // 900..949: application information errors
80 // 950..999: batch api errors
81 // 1000..1099: event api errors
82 // 1100..1199: live-message errors
83 case 2200: return new CallbackVerificationFailedException(error);
86 LOG.info("unmapped error: {}", error);
87 return new UnmappedErrorException(error);
92 protected GraphApiException(FacebookErrorMessage error)
101 return error.type == null ? null : Type.valueOf(error.type);
104 public Integer getCode()
109 public Integer getSubCode()
111 return error.subCode;
114 public String getUserTitle()
116 return error.userTitle;
119 public String getUserMessage()
121 return error.userMessage;
124 public String getTraceId()
126 return error.traceId;
131 public String toString()
135 return OBJECT_MAPPER.writeValueAsString(error);
137 catch(JsonProcessingException e)
139 // This should never happen. But in case of a mistake: be verbose!
140 LOG.error("could not convert message into JSON: {}", e);
141 return e.getMessage();