001package com.box.sdk;
002
003import java.util.Collections;
004import java.util.List;
005import java.util.Map;
006
007/** Thrown to indicate that an error occurred while communicating with the Box API. */
008public class BoxAPIException extends RuntimeException {
009  private static final long serialVersionUID = 1L;
010
011  private int responseCode;
012  private String response;
013  private Map<String, List<String>> headers;
014
015  /**
016   * Constructs a BoxAPIException with a specified message.
017   *
018   * @param message a message explaining why the exception occurred.
019   */
020  public BoxAPIException(String message) {
021    super(message);
022
023    this.responseCode = 0;
024    this.response = null;
025    this.headers = null;
026  }
027
028  /**
029   * Constructs a BoxAPIException with details about the server's response.
030   *
031   * @param message a message explaining why the exception occurred.
032   * @param responseCode the response code returned by the Box server.
033   * @param response the response body returned by the Box server.
034   */
035  public BoxAPIException(String message, int responseCode, String response) {
036    // People are missing the getResponse method we have. So adding it to message
037    super(message + "\n" + response);
038
039    this.responseCode = responseCode;
040    this.response = response;
041    this.headers = null;
042  }
043
044  /**
045   * Constructs a BoxAPIException with details about the server's response, including response
046   * headers.
047   *
048   * @param message a message explaining why the exception occurred.
049   * @param responseCode the response code returned by the Box server.
050   * @param responseBody the response body returned by the Box server.
051   * @param responseHeaders the response headers returned by the Box server.
052   */
053  public BoxAPIException(
054      String message,
055      int responseCode,
056      String responseBody,
057      Map<String, List<String>> responseHeaders) {
058    // People are missing the getResponse method we have. So adding it to message
059    super(message + "\n" + responseBody);
060
061    this.responseCode = responseCode;
062    this.response = responseBody;
063    this.headers = responseHeaders;
064  }
065
066  /**
067   * Constructs a BoxAPIException that wraps another underlying exception.
068   *
069   * @param message a message explaining why the exception occurred.
070   * @param cause an underlying exception.
071   */
072  public BoxAPIException(String message, Throwable cause) {
073    super(message, cause);
074
075    this.responseCode = 0;
076    this.response = null;
077    this.headers = null;
078  }
079
080  /**
081   * Constructs a BoxAPIException that wraps another underlying exception with details about the
082   * server's response.
083   *
084   * @param message a message explaining why the exception occurred.
085   * @param responseCode the response code returned by the Box server.
086   * @param response the response body returned by the Box server.
087   * @param cause an underlying exception.
088   */
089  public BoxAPIException(String message, int responseCode, String response, Throwable cause) {
090    super(message, cause);
091
092    this.responseCode = responseCode;
093    this.response = response;
094    this.headers = null;
095  }
096
097  /**
098   * Constructs a BoxAPIException that includes the response headers.
099   *
100   * @param message a message explaining why the exception occurred.
101   * @param responseCode the response code returned by the Box server.
102   * @param responseBody the response body returned by the Box server.
103   * @param responseHeaders the response headers returned by the Box server.
104   * @param cause an underlying exception.
105   */
106  public BoxAPIException(
107      String message,
108      int responseCode,
109      String responseBody,
110      Map<String, List<String>> responseHeaders,
111      Throwable cause) {
112
113    super(message, cause);
114
115    this.responseCode = responseCode;
116    this.response = responseBody;
117    this.headers = responseHeaders;
118  }
119
120  /**
121   * Gets the response code returned by the server when this exception was thrown.
122   *
123   * @return the response code returned by the server.
124   */
125  public int getResponseCode() {
126    return this.responseCode;
127  }
128
129  /**
130   * Sets the response code returned by the server.
131   *
132   * @param responseCode the response code returned by the server.
133   */
134  protected void setResponseCode(int responseCode) {
135    this.responseCode = responseCode;
136  }
137
138  /**
139   * Gets the body of the response returned by the server when this exception was thrown.
140   *
141   * @return the body of the response returned by the server.
142   */
143  public String getResponse() {
144    return this.response;
145  }
146
147  /**
148   * Sets the response returned by ther server.
149   *
150   * @param response the response returned by the server.
151   */
152  protected void setResponse(String response) {
153    this.response = response;
154  }
155
156  /**
157   * Gets the response headers, if available.
158   *
159   * @return the response headers, or empty map if not available.
160   */
161  public Map<String, List<String>> getHeaders() {
162    if (this.headers != null) {
163      return this.headers;
164    } else {
165      return Collections.emptyMap();
166    }
167  }
168
169  /**
170   * Sets the response headers.
171   *
172   * @param headers headers to set.
173   */
174  protected void setHeaders(Map<String, List<String>> headers) {
175    this.headers = headers;
176  }
177}