Refactoring: generalized the GraphApiErrorHandler
[facebook-errors] / src / main / java / de / juplo / facebook / errors / GraphApiException.java
1 package de.juplo.facebook.errors;
2
3
4 import com.fasterxml.jackson.core.JsonParseException;
5 import com.fasterxml.jackson.core.JsonProcessingException;
6 import com.fasterxml.jackson.databind.DeserializationFeature;
7 import com.fasterxml.jackson.databind.JsonMappingException;
8 import com.fasterxml.jackson.databind.ObjectMapper;
9 import com.fasterxml.jackson.databind.SerializationFeature;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15
16
17 /**
18  * Base exception for Facebook Graph-Api exceptions.
19  * 
20  * @author Kai Moritz
21  */
22 public class GraphApiException extends RuntimeException
23 {
24   public enum Type { OAuthException, GraphMethodException }
25
26
27   final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
28   final static ObjectMapper OBJECT_MAPPER;
29
30   private final FacebookErrorMessage error;
31
32
33   static
34   {
35     OBJECT_MAPPER = new ObjectMapper();
36     OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
37     OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);
38     OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
39   }
40
41
42   public static GraphApiException create(InputStream in)
43       throws
44         IOException,
45         JsonParseException,
46         JsonMappingException
47   {
48     return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
49   }
50
51   public static GraphApiException create(byte[] message)
52       throws
53         IOException,
54         JsonParseException,
55         JsonMappingException
56   {
57     return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
58   }
59
60   public static GraphApiException create(FacebookErrorMessage error)
61   {
62     // see: http://fbdevwiki.com/wiki/Error_codes
63     switch(error.code)
64     {
65       // 1..99: general errors
66       case 1:     return new UnknownErrorException(error);
67       case 2:     return new UnexpectedErrorException(error);
68       case 21:    return new PageMigratedException(error);
69       // 100..199: graph method errors
70       case 100:   return new UnsupportedGetRequestException(error);
71       case 104:   return new AccessTokenRequiredException(error);
72       // 200..299: permission errors
73       // 300..399: data editing errors
74       // 400..449: authentication error
75       // 450..499: session errors
76       // 500..599: application messaging errors
77       // 600..699: FQL errors
78       case 613:   return new RateExceededException(error);
79       // 700..749: ref errors
80       // 750..799: application integration errors
81       // 900..949: application information errors
82       // 950..999: batch api errors
83       // 1000..1099: event api errors
84       // 1100..1199: live-message errors
85       case 2200:  return new CallbackVerificationFailedException(error);
86
87       default:
88         LOG.info("unmapped error: {}", error);
89         return new UnmappedErrorException(error);
90     }
91   }
92
93
94   protected GraphApiException(FacebookErrorMessage error)
95   {
96     super(error.message);
97     this.error = error;
98   }
99
100
101   public Type getType()
102   {
103     return error.type == null ? null : Type.valueOf(error.type);
104   }
105
106   public Integer getCode()
107   {
108     return error.code;
109   }
110
111   public Integer getSubCode()
112   {
113     return error.subCode;
114   }
115
116   public String getUserTitle()
117   {
118     return error.userTitle;
119   }
120
121   public String getUserMessage()
122   {
123     return error.userMessage;
124   }
125
126   public String getTraceId()
127   {
128     return error.traceId;
129   }
130
131
132   @Override
133   public String toString()
134   {
135     try
136     {
137       return OBJECT_MAPPER.writeValueAsString(error);
138     }
139     catch(JsonProcessingException e)
140     {
141       // This should never happen. But in case of a mistake: be verbose!
142       LOG.error("could not convert message into JSON: {}", e);
143       return e.getMessage();
144     }
145   }
146 }