001package com.box.sdk;
002
003/**
004 * Thrown to indicate that an error occurred while communicating with the Box API.
005 */
006public class BoxAPIException extends RuntimeException {
007    private static final long serialVersionUID = 1L;
008
009    private final int responseCode;
010    private final String response;
011
012    /**
013     * Constructs a BoxAPIException with a specified message.
014     * @param  message a message explaining why the exception occurred.
015     */
016    public BoxAPIException(String message) {
017        super(message);
018
019        this.responseCode = 0;
020        this.response = null;
021    }
022
023    /**
024     * Constructs a BoxAPIException with details about the server's response.
025     * @param  message      a message explaining why the exception occurred.
026     * @param  responseCode the response code returned by the Box server.
027     * @param  response     the response body returned by the Box server.
028     */
029    public BoxAPIException(String message, int responseCode, String response) {
030        super(message);
031
032        this.responseCode = responseCode;
033        this.response = response;
034    }
035
036    /**
037     * Constructs a BoxAPIException that wraps another underlying exception.
038     * @param  message a message explaining why the exception occurred.
039     * @param  cause   an underlying exception.
040     */
041    public BoxAPIException(String message, Throwable cause) {
042        super(message, cause);
043
044        this.responseCode = 0;
045        this.response = null;
046    }
047
048    /**
049     * Constructs a BoxAPIException that wraps another underlying exception with details about the server's response.
050     * @param  message      a message explaining why the exception occurred.
051     * @param  responseCode the response code returned by the Box server.
052     * @param  response     the response body returned by the Box server.
053     * @param  cause        an underlying exception.
054     */
055    public BoxAPIException(String message, int responseCode, String response, Throwable cause) {
056        super(message, cause);
057
058        this.responseCode = responseCode;
059        this.response = response;
060    }
061
062    /**
063     * Gets the response code returned by the server when this exception was thrown.
064     * @return the response code returned by the server.
065     */
066    public int getResponseCode() {
067        return this.responseCode;
068    }
069
070    /**
071     * Gets the body of the response returned by the server when this exception was thrown.
072     * @return the body of the response returned by the server.
073     */
074    public String getResponse() {
075        return this.response;
076    }
077}