Implemented FacebookErrorHandler to handle facebook-error-codes
[facebook-utils] / src / main / java / de / juplo / facebook / GraphApiException.java
1 package de.juplo.facebook;
2
3 import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
4
5 /**
6  * Base exception for Facebook Graph-Api exceptions.
7  * 
8  * @author Kai Moritz
9  */
10 @org.codehaus.jackson.map.annotate.JsonSerialize(using = GraphApiExceptionJackson1Serializer.class)
11 @org.codehaus.jackson.map.annotate.JsonDeserialize(using = GraphApiExceptionJackson1Deserializer.class)
12 @com.fasterxml.jackson.databind.annotation.JsonSerialize(using = GraphApiExceptionJackson2Serializer.class)
13 @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = GraphApiExceptionJackson2Deserializer.class)
14 public class GraphApiException extends OAuth2Exception
15 {
16   private final String type;
17   private final int code;
18
19   private int httpErrorCode;
20
21
22   public GraphApiException(String message, String type, int code)
23   {
24     super(message);
25     this.type = type;
26     this.code = code;
27   }
28
29
30   public String getType()
31   {
32     return type;
33   }
34
35   public int getCode()
36   {
37     return code;
38   }
39
40   @Override
41   public int getHttpErrorCode()
42   {
43     return httpErrorCode == 0 ? super.getHttpErrorCode() : httpErrorCode;
44   }
45
46   public void setHttpErrorCode(int httpErrorCode)
47   {
48     this.httpErrorCode = httpErrorCode;
49   }
50
51   @Override
52   public String toString()
53   {
54     StringBuilder builder = new StringBuilder();
55     builder.append("{error:{\"message\":\"");
56     builder.append(getMessage().replaceAll("\"", "\\\""));
57     builder.append("\",\"type\":");
58     builder.append(type.replaceAll("\"", "\\\""));
59     builder.append("\",\"code\":");
60     builder.append(code);
61     builder.append("}}");
62     return builder.toString();
63   }
64 }