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 final int responseCode; 014 private final String response; 015 private final Map<String, List<String>> headers; 016 017 /** 018 * Constructs a BoxAPIException with a specified message. 019 * @param message a message explaining why the exception occurred. 020 */ 021 public BoxAPIException(String message) { 022 super(message); 023 024 this.responseCode = 0; 025 this.response = null; 026 this.headers = null; 027 } 028 029 /** 030 * Constructs a BoxAPIException with details about the server's response. 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 headers. 046 * @param message a message explaining why the exception occurred. 047 * @param responseCode the response code returned by the Box server. 048 * @param responseBody the response body returned by the Box server. 049 * @param responseHeaders the response headers returned by the Box server. 050 */ 051 public BoxAPIException(String message, int responseCode, String responseBody, 052 Map<String, List<String>> responseHeaders) { 053 //People are missing the getResponse method we have. So adding it to message 054 super(message + "\n" + responseBody); 055 056 this.responseCode = responseCode; 057 this.response = responseBody; 058 this.headers = responseHeaders; 059 } 060 061 /** 062 * Constructs a BoxAPIException that wraps another underlying exception. 063 * @param message a message explaining why the exception occurred. 064 * @param cause an underlying exception. 065 */ 066 public BoxAPIException(String message, Throwable cause) { 067 super(message, cause); 068 069 this.responseCode = 0; 070 this.response = null; 071 this.headers = null; 072 } 073 074 /** 075 * Constructs a BoxAPIException that wraps another underlying exception with details about the server's response. 076 * @param message a message explaining why the exception occurred. 077 * @param responseCode the response code returned by the Box server. 078 * @param response the response body returned by the Box server. 079 * @param cause an underlying exception. 080 */ 081 public BoxAPIException(String message, int responseCode, String response, Throwable cause) { 082 super(message, cause); 083 084 this.responseCode = responseCode; 085 this.response = response; 086 this.headers = null; 087 } 088 089 /** 090 * Constructs a BoxAPIException that includes the response headers. 091 * @param message a message explaining why the exception occurred. 092 * @param responseCode the response code returned by the Box server. 093 * @param responseBody the response body returned by the Box server. 094 * @param responseHeaders the response headers returned by the Box server. 095 * @param cause an underlying exception. 096 */ 097 public BoxAPIException(String message, int responseCode, String responseBody, 098 Map<String, List<String>> responseHeaders, Throwable cause) { 099 100 super(message, cause); 101 102 this.responseCode = responseCode; 103 this.response = responseBody; 104 this.headers = responseHeaders; 105 } 106 107 /** 108 * Gets the response code returned by the server when this exception was thrown. 109 * @return the response code returned by the server. 110 */ 111 public int getResponseCode() { 112 return this.responseCode; 113 } 114 115 /** 116 * Gets the body of the response returned by the server when this exception was thrown. 117 * @return the body of the response returned by the server. 118 */ 119 public String getResponse() { 120 return this.response; 121 } 122 123 /** 124 * Gets the response headers, if available. 125 * @return the response headers, or empty map if not available. 126 */ 127 public Map<String, List<String>> getHeaders() { 128 if (this.headers != null) { 129 return this.headers; 130 } else { 131 return Collections.emptyMap(); 132 } 133 } 134}