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

com.afrozaar.wordpress.wpapi.v2.exception.WpApiParsedException Maven / Gradle / Ivy

There is a newer version: 4.8.3
Show newest version
package com.afrozaar.wordpress.wpapi.v2.exception;

import static java.lang.String.format;

import org.springframework.web.client.HttpStatusCodeException;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Collection;
import java.util.Optional;

public class WpApiParsedException extends Exception {

    private static ObjectMapper mapper;

    private final String code;
    private final String errorMessage;
    private final Object data;

    private final Collection additionalErrors;

    public WpApiParsedException(ParsedRestException parsed) {
        this(parsed.getCause().getMessage(), parsed.getCause(), parsed.getErrorMessage(), parsed.getCode(), parsed.getData(), parsed.getAdditionalErrors());
    }

    private WpApiParsedException(String message, HttpStatusCodeException cause, String errorMessage, String code, Object data, Collection additionalErrors) {
        super(format("%s - %s", message, errorMessage), cause);
        this.code = code;
        this.errorMessage = errorMessage;
        this.data = data;
        this.additionalErrors = additionalErrors;
    }

    public static WpApiParsedException of(ParsedRestException parsed) {
        return new WpApiParsedException(parsed.getCause().getMessage(), parsed.getCause(), parsed.getErrorMessage(), parsed.getCode(), parsed.getData(), parsed.getAdditionalErrors());
    }

    public static WpApiParsedException of(HttpStatusCodeException cause) {
        return of(ParsedRestException.of(cause));
    }

    public String getCode() {
        return code;
    }

    public Object getData() {
        return data;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public Optional> getAdditionalErrors() {
        return Optional.ofNullable(additionalErrors);
    }

    private static ObjectMapper getMapper() {
        return mapper == null ? mapper = new ObjectMapper() : mapper;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy