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

com.smartling.api.sdk.exceptions.SmartlingApiException Maven / Gradle / Ivy

There is a newer version: 4.0.25
Show newest version
/*
 * Copyright 2012 Smartling, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this work except in compliance with the License.
 * You may obtain a copy of the License in the LICENSE file, or at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.smartling.api.sdk.exceptions;

import com.smartling.api.sdk.file.response.Error;
import com.smartling.api.sdk.util.HttpUtils;
import org.apache.http.Header;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SmartlingApiException extends Exception
{
    private static final long serialVersionUID = -397098626101615761L;

    private final List originalErrors;

    private final String requestId;
    private final int statusCode;
    private final Map responseHeaders;

    public SmartlingApiException(String message, Throwable cause, List originalErrors)
    {
        super(message, cause);
        this.originalErrors = originalErrors;
        this.requestId = HttpUtils.getRequestId().get() == null ? "N/A" : HttpUtils.getRequestId().get();

        HttpUtils.ResponseDetails responseDetails = HttpUtils.getResponseDetails().get();
        if (responseDetails != null)
        {
            this.statusCode = responseDetails.getStatusCode();
            this.responseHeaders = Collections.unmodifiableMap(convertHeadersToMap(responseDetails));
        }
        else
        {
            this.statusCode = 0;
            this.responseHeaders = Collections.emptyMap();
        }
    }

    private Map convertHeadersToMap(final HttpUtils.ResponseDetails responseDetails)
    {
        Header[] headers = responseDetails.getHeaders();
        Map headersMap = new HashMap<>(headers.length);
        for(Header header : headers)
        {
            headersMap.put(header.getName(), header.getValue());
        }
        return headersMap;
    }

    public SmartlingApiException(String message, List originalErrors)
    {
        this(message, null, originalErrors);
    }

    public SmartlingApiException(String message, Throwable cause)
    {
        this(message, cause, Collections.emptyList());
    }

    public SmartlingApiException(final Exception e)
    {
        this("", e, Collections.emptyList());
    }

    public SmartlingApiException(final String message)
    {
        this(message, null, Collections.emptyList());
    }

    public List getOriginalErrors()
    {
        return originalErrors;
    }

    public String getRequestId()
    {
        return requestId;
    }

    public int getStatusCode()
    {
        return statusCode;
    }

    public Map getResponseHeaders()
    {
        return responseHeaders;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy