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        //People are missing the getResponse method we have. So adding it to message
031        super(message + "\n" + response);
032
033        this.responseCode = responseCode;
034        this.response = response;
035    }
036
037    /**
038     * Constructs a BoxAPIException that wraps another underlying exception.
039     * @param  message a message explaining why the exception occurred.
040     * @param  cause   an underlying exception.
041     */
042    public BoxAPIException(String message, Throwable cause) {
043        super(message, cause);
044
045        this.responseCode = 0;
046        this.response = null;
047    }
048
049    /**
050     * Constructs a BoxAPIException that wraps another underlying exception with details about the server's response.
051     * @param  message      a message explaining why the exception occurred.
052     * @param  responseCode the response code returned by the Box server.
053     * @param  response     the response body returned by the Box server.
054     * @param  cause        an underlying exception.
055     */
056    public BoxAPIException(String message, int responseCode, String response, Throwable cause) {
057        super(message, cause);
058
059        this.responseCode = responseCode;
060        this.response = response;
061    }
062
063    /**
064     * Gets the response code returned by the server when this exception was thrown.
065     * @return the response code returned by the server.
066     */
067    public int getResponseCode() {
068        return this.responseCode;
069    }
070
071    /**
072     * Gets the body of the response returned by the server when this exception was thrown.
073     * @return the body of the response returned by the server.
074     */
075    public String getResponse() {
076        return this.response;
077    }
078}