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.databind.DeserializationFeature;
9 import com.fasterxml.jackson.databind.JsonMappingException;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.SerializationFeature;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
17 * Base exception for Facebook Graph-Api exceptions.
21 public class GraphApiException extends OAuth2Exception
23 final static ObjectMapper OBJECT_MAPPER;
25 private final FacebookErrorMessage error;
30 OBJECT_MAPPER = new ObjectMapper();
31 OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
32 OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);
33 OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
37 public static GraphApiException create(InputStream in)
43 return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
46 public static GraphApiException create(byte[] message)
52 return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
55 public static GraphApiException create(FacebookErrorMessage error)
57 // see: http://fbdevwiki.com/wiki/Error_codes
60 // 1..99: general errors
61 case 1: return new UnknownErrorException(error);
62 case 2: return new UnexpectedErrorException(error);
63 case 21: return new PageMigratedException(error);
64 // 100..199: graph method errors
65 case 100: return new UnsupportedGetRequestException(error);
66 // 200..299: permission errors
67 // 300..399: data editing errors
68 // 400..449: authentication error
69 // 450..499: session errors
70 // 500..599: application messaging errors
71 // 600..699: FQL errors
72 case 613: return new RateExceededException(error);
73 // 700..749: ref errors
74 // 750..799: application integration errors
75 // 900..949: application information errors
76 // 950..999: batch api errors
77 // 1000..1099: event api errors
78 // 1100..1199: live-message errors
80 default: return new UnmappedErrorException(error);
85 protected GraphApiException(FacebookErrorMessage error)
92 public String getType()
97 public Integer getCode()
102 public Integer getSubCode()
104 return error.subCode;
107 public String getUserTitle()
109 return error.userTitle;
112 public String getUserMessage()
114 return error.userMessage;
117 public String getTraceId()
119 return error.traceId;
124 public String toString()
128 return OBJECT_MAPPER.writeValueAsString(error);
130 catch(JsonProcessingException e)
132 return "Could not convert error in JSON-representation: " + e.getMessage();
138 * This class represents an error message from the Graph-API
140 * @see <a href="https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors">Graph-API Documentation</a>
142 @JsonRootName("error")
143 @JsonPropertyOrder({ "message", "type", "code", "error_subcode", "error_user_title", "error_user_msg", "fbtrace_id" })
144 public static class FacebookErrorMessage
146 @JsonProperty("message")
148 @JsonProperty("type")
150 @JsonProperty("code")
152 @JsonProperty("error_subcode")
154 @JsonProperty("error_user_title")
156 @JsonProperty("error_user_msg")
158 @JsonProperty("fbtrace_id")