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