bd0256570772cf9165d58241ef47104750f65c82
[facebook-errors] / src / main / java / de / juplo / facebook / exceptions / GraphApiException.java
1 package de.juplo.facebook.exceptions;
2
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;
15
16 /**
17  * Base exception for Facebook Graph-Api exceptions.
18  * 
19  * @author Kai Moritz
20  */
21 public class GraphApiException extends OAuth2Exception
22 {
23   final static ObjectMapper OBJECT_MAPPER;
24
25   private final FacebookErrorMessage error;
26
27
28   static
29   {
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);
34   }
35
36
37   public static GraphApiException create(InputStream in)
38       throws
39         IOException,
40         JsonParseException,
41         JsonMappingException
42   {
43     return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
44   }
45
46   public static GraphApiException create(byte[] message)
47       throws
48         IOException,
49         JsonParseException,
50         JsonMappingException
51   {
52     return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
53   }
54
55   public static GraphApiException create(FacebookErrorMessage error)
56   {
57     // see: http://fbdevwiki.com/wiki/Error_codes
58     switch(error.code)
59     {
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
79
80       default:    return new UnmappedErrorException(error);
81     }
82   }
83
84
85   protected GraphApiException(FacebookErrorMessage error)
86   {
87     super(error.message);
88     this.error = error;
89   }
90
91
92   public String getType()
93   {
94     return error.type;
95   }
96
97   public Integer getCode()
98   {
99     return error.code;
100   }
101
102   public Integer getSubCode()
103   {
104     return error.subCode;
105   }
106
107   public String getUserTitle()
108   {
109     return error.userTitle;
110   }
111
112   public String getUserMessage()
113   {
114     return error.userMessage;
115   }
116
117   public String getTraceId()
118   {
119     return error.traceId;
120   }
121
122
123   @Override
124   public String toString()
125   {
126     try
127     {
128       return OBJECT_MAPPER.writeValueAsString(error);
129     }
130     catch(JsonProcessingException e)
131     {
132       return "Could not convert error in JSON-representation: " + e.getMessage();
133     }
134   }
135
136
137   /**
138    * This class represents an error message from the Graph-API
139    *
140    * @see <a href="https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors">Graph-API Documentation</a>
141    */
142   @JsonRootName("error")
143   @JsonPropertyOrder({ "message", "type", "code", "error_subcode", "error_user_title", "error_user_msg", "fbtrace_id" })
144   public static class FacebookErrorMessage
145   {
146     @JsonProperty("message")
147     String message;
148     @JsonProperty("type")
149     String type;
150     @JsonProperty("code")
151     Integer code;
152     @JsonProperty("error_subcode")
153     Integer subCode;
154     @JsonProperty("error_user_title")
155     String userTitle;
156     @JsonProperty("error_user_msg")
157     String userMessage;
158     @JsonProperty("fbtrace_id")
159     String traceId;
160   }
161 }