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

io.deepsense.neptune.clientlibrary.utils.exceptions.ApiExceptionWrapper Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.utils.exceptions;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.deepsense.neptune.apiclient.ApiException;
import io.deepsense.neptune.clientlibrary.exceptions.common.NeptuneRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public final class ApiExceptionWrapper {

    private static final Logger logger = LoggerFactory.getLogger(ApiExceptionWrapper.class);

    private ApiExceptionWrapper() {

    }

    public static NeptuneRuntimeException wrappedApiException(ApiException apiException) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            ApiExceptionBody apiExceptionBody =
                    objectMapper.readValue(apiException.getResponseBody(), ApiExceptionBody.class);
            return new NeptuneRuntimeException(apiExceptionBody.getMessage(), apiException);
        } catch (IOException ioException) {
            logger.error(
                    "Failed to parse the body of an ApiException: %s, cause: %s",
                    apiException,
                    ioException);
            return new NeptuneRuntimeException("Operation failed!", apiException);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy