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