1 package de.juplo.facebook.exceptions;
3 import com.fasterxml.jackson.annotation.JsonProperty;
4 import com.fasterxml.jackson.annotation.JsonPropertyOrder;
5 import com.fasterxml.jackson.annotation.JsonRootName;
6 import com.fasterxml.jackson.core.JsonParseException;
7 import com.fasterxml.jackson.core.JsonProcessingException;
8 import com.fasterxml.jackson.core.SerializableString;
9 import com.fasterxml.jackson.core.io.CharacterEscapes;
10 import com.fasterxml.jackson.databind.DeserializationFeature;
11 import com.fasterxml.jackson.databind.JsonMappingException;
12 import com.fasterxml.jackson.databind.ObjectMapper;
13 import com.fasterxml.jackson.databind.SerializationFeature;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17 import java.nio.ByteBuffer;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
23 * Base exception for Facebook Graph-Api exceptions.
27 public class GraphApiException extends OAuth2Exception
29 public enum Type { OAuthException, GraphMethodException }
32 final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
33 final static ObjectMapper OBJECT_MAPPER;
35 private final FacebookErrorMessage error;
40 OBJECT_MAPPER = new ObjectMapper();
41 OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
42 OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);
43 OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
44 OBJECT_MAPPER.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
48 public static GraphApiException create(InputStream in)
54 return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
57 public static GraphApiException create(byte[] message)
63 return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
66 public static GraphApiException create(FacebookErrorMessage error)
68 // see: http://fbdevwiki.com/wiki/Error_codes
71 // 1..99: general errors
72 case 1: return new UnknownErrorException(error);
73 case 2: return new UnexpectedErrorException(error);
74 case 21: return new PageMigratedException(error);
75 // 100..199: graph method errors
76 case 100: return new UnsupportedGetRequestException(error);
77 case 104: return new AccessTokenRequiredException(error);
78 // 200..299: permission errors
79 // 300..399: data editing errors
80 // 400..449: authentication error
81 // 450..499: session errors
82 // 500..599: application messaging errors
83 // 600..699: FQL errors
84 case 613: return new RateExceededException(error);
85 // 700..749: ref errors
86 // 750..799: application integration errors
87 // 900..949: application information errors
88 // 950..999: batch api errors
89 // 1000..1099: event api errors
90 // 1100..1199: live-message errors
91 case 2200: return new CallbackVerificationFailedException(error);
94 LOG.info("unmapped error: {}", error);
95 return new UnmappedErrorException(error);
100 protected GraphApiException(FacebookErrorMessage error)
102 super(error.message);
107 public Type getType()
109 return error.type == null ? null : Type.valueOf(error.type);
112 public Integer getCode()
117 public Integer getSubCode()
119 return error.subCode;
122 public String getUserTitle()
124 return error.userTitle;
127 public String getUserMessage()
129 return error.userMessage;
132 public String getTraceId()
134 return error.traceId;
139 public String toString()
143 return OBJECT_MAPPER.writeValueAsString(error);
145 catch(JsonProcessingException e)
147 // This should never happen. But in case of a mistake: be verbose!
148 LOG.error("could not convert message into JSON: {}", e);
149 return e.getMessage();
155 * This class represents an error message from the Graph-API
157 * @see <a href="https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors">Graph-API Documentation</a>
159 @JsonRootName("error")
160 @JsonPropertyOrder({ "message", "type", "code", "error_subcode", "error_user_title", "error_user_msg", "fbtrace_id" })
161 public static class FacebookErrorMessage
163 @JsonProperty("message")
165 @JsonProperty("type")
167 @JsonProperty("code")
169 @JsonProperty("error_subcode")
171 @JsonProperty("error_user_title")
173 @JsonProperty("error_user_msg")
175 @JsonProperty("fbtrace_id")
179 public static class CustomCharacterEscapes extends CharacterEscapes
181 private final int[] _asciiEscapes;
184 public CustomCharacterEscapes()
186 _asciiEscapes = standardAsciiEscapesForJSON();
187 _asciiEscapes['/'] = CharacterEscapes.ESCAPE_CUSTOM;
192 public int[] getEscapeCodesForAscii()
194 return _asciiEscapes;
198 public SerializableString getEscapeSequence(int i)
202 return new SerializableString()
205 public String getValue()
211 public int charLength()
217 public char[] asQuotedChars()
219 return new char[]{'\\','/'};
223 public byte[] asUnquotedUTF8()
225 return new byte[]{'\\','/'};
229 public byte[] asQuotedUTF8()
231 return new byte[]{'\\','/'};
235 public int appendQuotedUTF8(byte[] buffer, int offset)
237 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
241 public int appendQuoted(char[] buffer, int offset)
243 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
247 public int appendUnquotedUTF8(byte[] buffer, int offset)
249 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
253 public int appendUnquoted(char[] buffer, int offset)
255 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
259 public int writeQuotedUTF8(OutputStream out) throws IOException
261 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
265 public int writeUnquotedUTF8(OutputStream out) throws IOException
267 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
271 public int putQuotedUTF8(ByteBuffer buffer) throws IOException
273 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
277 public int putUnquotedUTF8(ByteBuffer out) throws IOException
279 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.