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

io.sphere.sdk.models.SphereException Maven / Gradle / Ivy

There is a newer version: 1.0.0-M26
Show newest version
package io.sphere.sdk.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.sphere.sdk.client.SphereRequest;
import io.sphere.sdk.http.HttpResponse;
import io.sphere.sdk.meta.BuildInfo;

import javax.annotation.Nullable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

/**
 * Base class for all exceptions related to the SDK.
 *
 */
public class SphereException extends RuntimeException {
    static final long serialVersionUID = 0L;
    @Nullable
    private SphereRequest sphereRequest;
    @JsonIgnore
    @Nullable
    protected HttpResponse httpResponse;
    @Nullable
    private String projectKey;
    @Nullable
    private String httpRequest;
    private List additionalNotes = new LinkedList<>();

    public SphereException(final String message, final Throwable cause) {
        super(message, cause);
    }

    public SphereException() {
    }

    public SphereException(final String message) {
        super(message);
    }

    public SphereException(final Throwable cause) {
        super(cause);
    }

    @Nullable
    public final String getProjectKey() {
        return projectKey;
    }

    @Nullable
    public final SphereRequest getSphereRequest() {
        return sphereRequest;
    }

    @Nullable
    public final HttpResponse getHttpResponse() {
        return httpResponse;
    }

    public void setProjectKey(final String projectKey) {
        this.projectKey = projectKey;
    }

    public void setSphereRequest(final SphereRequest sphereRequest) {
        this.sphereRequest = sphereRequest;
        setHttpRequest(sphereRequest.httpRequestIntent().toString());
    }

    public void setHttpRequest(final String httpRequest) {
        this.httpRequest = httpRequest;
    }

    public void addNote(final String note) {
        additionalNotes.add(note);
    }

    @Override
    public final String getMessage() {
        StringBuilder builder = new StringBuilder();
        return builder
                .append("SDK: ").append(BuildInfo.version()).append("\n")
                .append("project: ").append(Optional.ofNullable(getProjectKey()).orElse("")).append("\n")
                        .append(Optional.ofNullable(getSphereRequest()).map(x -> x.httpRequestIntent()).map(x -> "" + x.getHttpMethod() + " " + x.getPath()).map(x -> "endpoint: " + x + "\n").orElse(""))
                        .append("Java: ").append(System.getProperty("java.version")).append("\n")
                        .append("cwd: ").append(System.getProperty("user.dir")).append("\n")
                        .append("date: ").append(new Date()).append("\n")
                        .append("sphere request: ").append(Optional.ofNullable(getSphereRequest()).map(Object::toString).orElse("")).append("\n")
                                //duplicated in case SphereRequest does not implement a proper to String
                        .append("http request: ").append(Optional.ofNullable(httpRequest).orElse("")).append("\n")
                        .append("http response: ").append(Optional.ofNullable(getHttpResponse()).map(Object::toString).orElse("")).append("\n")
                        .append(Optional.ofNullable(super.getMessage()).map(s -> "detailMessage: " + s + "\n").orElse(""))
                        .append("additional notes: ").append(additionalNotes).append("\n")
                        .append("Javadoc: ").append("http://sphereio.github.io/sphere-jvm-sdk/javadoc/").append(getVersionForJavadoc()).append("/").append(this.getClass().getCanonicalName().replace('.', '/')).append(".html").append("\n")
                .toString();
    }


    private static String getVersionForJavadoc() {
        return getVersionForJavadoc(BuildInfo.version());
    }

    //package scope for testing
    static String getVersionForJavadoc(final String version) {
        final boolean releaseVersion = Pattern.compile("^(\\d+\\.\\d+\\.\\d+)(-M\\d+)?$").matcher(version).matches();
        return releaseVersion ? "v" + version : version;
    }

    public void setUnderlyingHttpResponse(final HttpResponse httpResponse) {
        this.httpResponse = httpResponse.withoutRequest();
    }

    public List getAdditionalNotes() {
        return additionalNotes;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy