001/**
002 * Copyright (c) 2010-2019 Mark Allen, Norbert Bartels.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.restfb.exception;
023
024import static java.lang.String.format;
025
026import com.restfb.json.JsonObject;
027
028/**
029 * Indicates that the Facebook Graph API endpoint returned JSON which indicates an error condition.
030 * <p>
031 * Example:<code>
032  {
033      "error": {
034        "type": "Exception",
035        "message": "...",
036        "code": 210,
037        "error_subcode": 123,
038        "error_user_title": "A title",
039        "error_user_msg": "A message"
040      }
041  } </code>
042 * 
043 * @author <a href="http://restfb.com">Mark Allen</a>
044 * @since 1.5
045 */
046public class FacebookGraphException extends FacebookErrorMessageException {
047  private static final long serialVersionUID = 1L;
048
049  /**
050   * The Facebook Graph API error type.
051   */
052  private final String errorType;
053
054  /**
055   * The Facebook API error message.
056   */
057  private final String errorMessage;
058
059  /**
060   * The Facebook API error user title.
061   */
062  private final String errorUserTitle;
063
064  /**
065   * The Facebook API error user message.
066   */
067  private final String errorUserMessage;
068
069  /**
070   * The Facebook API error code.
071   */
072  private final Integer errorCode;
073
074  /**
075   * The Facebook API error subcode.
076   */
077  private final Integer errorSubcode;
078
079  /**
080   * The HTTP status code returned by the server.
081   */
082  private final Integer httpStatusCode;
083
084  private final Boolean isTransient;
085
086  /**
087   * Creates an exception with the given error type and message.
088   * 
089   * @param errorType
090   *          Value of the Facebook response attribute {@code error.type}.
091   * @param errorMessage
092   *          Value of the Facebook response attribute {@code error.message}.
093   * @param errorCode
094   *          Value of the Facebook response attribute {@code error.code}.
095   * @param errorSubcode
096   *          Value of the Facebook response attribute {@code error.error_subcode}.
097   * @param httpStatusCode
098   *          The HTTP status code returned by the server, e.g. 500.
099   * @param errorUserTitle
100   *          Value of the Facebook response attribute {@code error.error_user_title}.
101   * @param errorUserMessage
102   *          Value of the Facebook response attribute {@code error.error_user_msg}.
103   * @param isTransient
104   * 
105   */
106  public FacebookGraphException(String errorType, String errorMessage, Integer errorCode, Integer errorSubcode,
107      Integer httpStatusCode, String errorUserTitle, String errorUserMessage, Boolean isTransient,
108      JsonObject rawError) {
109    super(format("Received Facebook error response of type %s: %s (code %s, subcode %s) '%s - %s'", errorType,
110      errorMessage, errorCode, errorSubcode, errorUserTitle, errorUserMessage));
111    this.errorType = errorType;
112    this.errorMessage = errorMessage;
113    this.errorCode = errorCode;
114    this.errorSubcode = errorSubcode;
115    this.httpStatusCode = httpStatusCode;
116    this.errorUserTitle = errorUserTitle;
117    this.errorUserMessage = errorUserMessage;
118    this.isTransient = isTransient;
119    setRawErrorJson(rawError);
120  }
121
122  /**
123   * Gets the Facebook Graph API error type.
124   * 
125   * @return The Facebook Graph API error type.
126   */
127  public String getErrorType() {
128    return errorType;
129  }
130
131  /**
132   * Gets the Facebook Graph API error message.
133   * 
134   * @return The Facebook Graph API error message.
135   */
136  public String getErrorMessage() {
137    return errorMessage;
138  }
139
140  /**
141   * Gets the Facebook API error code.
142   * 
143   * @return The Facebook API error code.
144   */
145  public Integer getErrorCode() {
146    return errorCode;
147  }
148
149  /**
150   * Gets the Facebook API error subcode.
151   * 
152   * @return The Facebook API error subcode.
153   */
154  public Integer getErrorSubcode() {
155    return errorSubcode;
156  }
157
158  /**
159   * Gets the HTTP status code returned by the server.
160   * 
161   * @return The HTTP status code returned by the server.
162   * @since 1.6.10
163   */
164  public Integer getHttpStatusCode() {
165    return httpStatusCode;
166  }
167
168  /**
169   * Gets the Facebook API error user title.
170   * 
171   * @return the Facebook API error user title
172   * @since 1.7.1
173   */
174  public String getErrorUserTitle() {
175    return errorUserTitle;
176  }
177
178  /**
179   * Gets the Facebook API error user message.
180   * 
181   * @return the Facebook API error user message
182   * @since 1.7.1
183   */
184  public String getErrorUserMessage() {
185    return errorUserMessage;
186  }
187
188  public Boolean getIsTransient() {
189    return isTransient;
190  }
191
192  /**
193   * Gets the Facebook API error {@code fbtrace_id}.
194   *
195   * Internal support identifier. When reporting a bug related to a Graph API call, include the fbtrace_id to help us
196   * find log data for debugging.
197   *
198   * @return the Facebook API error {@code fbtrace_id}
199   */
200  public String getFbtraceId() {
201    if (getRawErrorJson() != null && getRawErrorJson().get("error").isObject()) {
202      JsonObject errorJson = getRawErrorJson().get("error").asObject();
203      return errorJson.getString("fbtrace_id", "");
204    }
205
206    return "";
207  }
208
209}