18eaf81854df5ba71667b3f13f8ede834b65cfae
[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.JsonDeserialize(using = GraphApiExceptionJackson1Deserializer.class)
11 @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = GraphApiExceptionJackson2Deserializer.class)
12 public class GraphApiException extends OAuth2Exception
13 {
14   private final String type;
15   private final int code;
16
17   private int httpErrorCode;
18
19
20   public GraphApiException(String message, String type, int code)
21   {
22     super(message);
23     this.type = type;
24     this.code = code;
25   }
26
27
28   public String getType()
29   {
30     return type;
31   }
32
33   public int getCode()
34   {
35     return code;
36   }
37
38   @Override
39   public int getHttpErrorCode()
40   {
41     return httpErrorCode == 0 ? super.getHttpErrorCode() : httpErrorCode;
42   }
43
44   public void setHttpErrorCode(int httpErrorCode)
45   {
46     this.httpErrorCode = httpErrorCode;
47   }
48
49   @Override
50   public String toString()
51   {
52     StringBuilder builder = new StringBuilder();
53     builder.append("{error:{\"message\":\"");
54     builder.append(getMessage().replaceAll("\"", "\\\""));
55     builder.append("\",\"type\":");
56     builder.append(type.replaceAll("\"", "\\\""));
57     builder.append("\",\"code\":");
58     builder.append(code);
59     builder.append("}}");
60     return builder.toString();
61   }
62 }