001/*
002 * Copyright (c) 2010-2024 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 Legacy REST Facebook endpoint returned JSON which indicates an error condition.
030 * <p>
031 * This exception may also be thrown when executing certain operations against the Graph API, e.g. FQL queries or Batch
032 * API calls.
033 * <p>
034 * Example: <code>{"error_code": 2, "error_msg": "The service is not available at this time.", ...}</code>.
035 * 
036 * @author <a href="http://restfb.com">Mark Allen</a>
037 */
038public class FacebookResponseStatusException extends FacebookErrorMessageException {
039  /**
040   * The Facebook API error code.
041   */
042  private final Integer errorCode;
043
044  /**
045   * The Facebook API error message.
046   */
047  private final String errorMessage;
048
049  private static final long serialVersionUID = 1L;
050
051  /**
052   * Creates an exception with the given message and error code.
053   * 
054   * @param errorCode
055   *          Value of the Facebook response attribute {@code error_code}.
056   * @param errorMessage
057   *          Value of the Facebook response attribute {@code error_msg}.
058   */
059  public FacebookResponseStatusException(Integer errorCode, String errorMessage, JsonObject rawError) {
060    super(format("Received Facebook error response (code %d): %s", errorCode, errorMessage));
061    this.errorCode = errorCode;
062    this.errorMessage = errorMessage;
063    setRawErrorJson(rawError);
064  }
065
066  /**
067   * Gets the Facebook API error code.
068   * 
069   * @return The Facebook API error code.
070   */
071  public Integer getErrorCode() {
072    return errorCode;
073  }
074
075  /**
076   * Gets the Facebook API error message.
077   * 
078   * @return The Facebook API error message.
079   */
080  public String getErrorMessage() {
081    return errorMessage;
082  }
083}