001package com.box.sdk; 002 003import com.eclipsesource.json.JsonObject; 004import java.util.ArrayList; 005import java.util.List; 006import java.util.Map; 007import java.util.TreeMap; 008 009/** 010 * Thrown to indicate than an error occured while returning with a response from the Box API. 011 */ 012public class BoxAPIResponseException extends BoxAPIException { 013 014 private String message; 015 private BoxAPIResponse responseObj; 016 017 /** 018 * Constructs a BoxAPIException that contains detailed message for underlying exception. 019 * 020 * @param message a message explaining why the error occurred. 021 * @param responseObj a response object from the server. 022 */ 023 public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { 024 super(message, responseObj.getResponseCode(), responseObj.bodyToString()); 025 String requestId = ""; 026 String apiMessage = ""; 027 JsonObject responseJSON = null; 028 this.responseObj = responseObj; 029 030 Map<String, List<String>> responseHeaders = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER); 031 for (String headerKey : responseObj.getHeaders().keySet()) { 032 List<String> headerValues = new ArrayList<String>(); 033 headerValues.add(responseObj.getHeaderField(headerKey)); 034 responseHeaders.put(headerKey, headerValues); 035 } 036 037 this.setHeaders(responseHeaders); 038 039 if (this.getHeaders().containsKey("BOX-REQUEST-ID")) { 040 requestId += "." + this.getHeaders().get("BOX-REQUEST-ID").get(0).toString(); 041 } 042 043 try { 044 responseJSON = JsonObject.readFrom(responseObj.bodyToString()); 045 } catch (Exception ex) { 046 // Continue because we will construct the exception message below and return it to user. 047 } 048 049 if (responseJSON != null) { 050 if (responseJSON.get("request_id") != null) { 051 requestId = responseJSON.get("request_id").asString() + requestId; 052 } 053 054 if (responseJSON.get("code") != null) { 055 apiMessage += " " + responseJSON.get("code").asString(); 056 } else if (responseJSON.get("error") != null) { 057 apiMessage += " " + responseJSON.get("error").asString(); 058 } 059 060 if (responseJSON.get("message") != null) { 061 apiMessage += " - " + responseJSON.get("message").asString(); 062 } else if (responseJSON.get("error_description") != null) { 063 apiMessage += " - " + responseJSON.get("error_description").asString(); 064 } 065 } 066 067 if (!requestId.isEmpty()) { 068 this.setMessage(message + " [" + responseObj.getResponseCode() + " | " + requestId + "]" 069 + apiMessage); 070 } else { 071 this.setMessage(message + " [" + responseObj.getResponseCode() + "]" + apiMessage); 072 } 073 } 074 075 /** 076 * @return The constructed message for the API exception. 077 */ 078 public String getMessage() { 079 return this.message; 080 } 081 082 /** 083 * The message to return for the API exception. 084 * 085 * @param message the constructed for the API exception. 086 */ 087 protected void setMessage(String message) { 088 this.message = message; 089 } 090}