All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sherpa.partner.api.client.ClientServerException Maven / Gradle / Ivy

The newest version!
package sherpa.partner.api.client;

import sherpa.partner.api.common.ErrorResponse;

import java.util.Map;

public class ClientServerException extends ClientGeneralException {
    private final int statusCode;
    private final String statusMessage;
    private final ErrorResponse error;

    public ClientServerException(int statusCode, String statusMessage, ErrorResponse error) {
        super(buildMessage(statusCode, statusMessage, error));
        this.statusCode = statusCode;
        this.statusMessage = statusMessage;
        this.error = error;
    }

    /**
     * Get the HTTP status code
     *
     * @return the status code
     */
    public int getStatusCode() {
        return statusCode;
    }

    /**
     * Get the HTTP status message
     *
     * @return the status message
     */
    public String getStatusMessage() {
        return statusMessage;
    }

    /**
     * Get the error returned by the server
     *
     * @return the list of errors
     */
    public ErrorResponse getError() {
        return error;
    }

    private static String buildMessage(int statusCode, String statusMessage, ErrorResponse error) {
        StringBuilder sb = new StringBuilder();
        sb.append(statusCode).append(": ").append(statusMessage);

        if (error != null) {
            if (error.getError() != null) {
                sb.append(statusMessage == null || statusMessage.trim().isEmpty() ? "" : " - ")
                        .append(error.getError());
            }

            if (error.getFieldErrors() != null && !error.getFieldErrors().isEmpty()) {
                for (Map.Entry entry : error.getFieldErrors().entrySet()) {
                    sb.append("\n     ").append(entry.getKey()).append(" - ").append(entry.getValue());
                }
            }
        }

        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy