GraphApiException.create() logs unmapped errors on log-level INFO
[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.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
17
18 /**
19  * Base exception for Facebook Graph-Api exceptions.
20  * 
21  * @author Kai Moritz
22  */
23 public class GraphApiException extends OAuth2Exception
24 {
25   final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
26   final static ObjectMapper OBJECT_MAPPER;
27
28   private final FacebookErrorMessage error;
29
30
31   static
32   {
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);
37   }
38
39
40   public static GraphApiException create(InputStream in)
41       throws
42         IOException,
43         JsonParseException,
44         JsonMappingException
45   {
46     return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
47   }
48
49   public static GraphApiException create(byte[] message)
50       throws
51         IOException,
52         JsonParseException,
53         JsonMappingException
54   {
55     return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
56   }
57
58   public static GraphApiException create(FacebookErrorMessage error)
59   {
60     // see: http://fbdevwiki.com/wiki/Error_codes
61     switch(error.code)
62     {
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       // 200..299: permission errors
70       // 300..399: data editing errors
71       // 400..449: authentication error
72       // 450..499: session errors
73       // 500..599: application messaging errors
74       // 600..699: FQL errors
75       case 613:   return new RateExceededException(error);
76       // 700..749: ref errors
77       // 750..799: application integration errors
78       // 900..949: application information errors
79       // 950..999: batch api errors
80       // 1000..1099: event api errors
81       // 1100..1199: live-message errors
82
83       default:
84         LOG.info("unmapped error: {}", error);
85         return new UnmappedErrorException(error);
86     }
87   }
88
89
90   protected GraphApiException(FacebookErrorMessage error)
91   {
92     super(error.message);
93     this.error = error;
94   }
95
96
97   public String getType()
98   {
99     return error.type;
100   }
101
102   public Integer getCode()
103   {
104     return error.code;
105   }
106
107   public Integer getSubCode()
108   {
109     return error.subCode;
110   }
111
112   public String getUserTitle()
113   {
114     return error.userTitle;
115   }
116
117   public String getUserMessage()
118   {
119     return error.userMessage;
120   }
121
122   public String getTraceId()
123   {
124     return error.traceId;
125   }
126
127
128   @Override
129   public String toString()
130   {
131     try
132     {
133       return OBJECT_MAPPER.writeValueAsString(error);
134     }
135     catch(JsonProcessingException e)
136     {
137       // This should never happen. But in case of a mistake: be verbose!
138       LOG.error("could not convert message into JSON: {}", e);
139       return e.getMessage();
140     }
141   }
142
143
144   /**
145    * This class represents an error message from the Graph-API
146    *
147    * @see <a href="https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors">Graph-API Documentation</a>
148    */
149   @JsonRootName("error")
150   @JsonPropertyOrder({ "message", "type", "code", "error_subcode", "error_user_title", "error_user_msg", "fbtrace_id" })
151   public static class FacebookErrorMessage
152   {
153     @JsonProperty("message")
154     String message;
155     @JsonProperty("type")
156     String type;
157     @JsonProperty("code")
158     Integer code;
159     @JsonProperty("error_subcode")
160     Integer subCode;
161     @JsonProperty("error_user_title")
162     String userTitle;
163     @JsonProperty("error_user_msg")
164     String userMessage;
165     @JsonProperty("fbtrace_id")
166     String traceId;
167   }
168 }