com.etalio.android.client.exception.EtalioHttpException.java Source code

Java tutorial

Introduction

Here is the source code for com.etalio.android.client.exception.EtalioHttpException.java

Source

/*
 * Copyright 2014 Ericsson AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License 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.etalio.android.client.exception;

import com.etalio.android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Class to wrap HTTP errors.
 */
public class EtalioHttpException extends Exception {
    private final static String TAG = EtalioHttpException.class.getCanonicalName();

    private JSONObject parsedError;

    public String getResponseMessage() {
        return responseMessage;
    }

    public String getError() {
        return error;
    }

    private final int responseCode;
    private final String responseMessage;
    private final String error;
    private EtalioHttpError errorType;

    public EtalioHttpException(int responseCode, String responseMessage, String error) {
        this.responseCode = responseCode;
        this.responseMessage = responseMessage;
        this.error = error;
    }

    @Override
    public String getMessage() {
        if (error != null) {
            if (hasJsonError()) {
                try {
                    return getJsonErrorDescription();
                } catch (PropertyNotFoundException e) {
                    return error;
                }
            } else {
                return error;
            }
        }
        return super.getMessage();
    }

    public int getResponseCode() {
        return responseCode;
    }

    private boolean hasJsonError() {
        // If message has already been parsed it is available
        if (parsedError != null) {
            return true;
        }
        if (error != null) {
            try {
                parsedError = new JSONObject(error);
                return true;
            } catch (JSONException e) {
                return false;
            }
        }
        return false;
    }

    public String getJsonError() throws PropertyNotFoundException {
        final String propertyName = "error";
        return getJsonPropertyStringValue(propertyName);
    }

    public EtalioHttpError getErrorType() {
        if (errorType != null) {
            return errorType;
        }
        if (hasJsonError()) {
            if (errorType == null) {
                try {
                    errorType = EtalioHttpError.getEnum(getJsonError());
                } catch (PropertyNotFoundException e) {
                    errorType = EtalioHttpError.UNDEFINED;
                }
            }
            return errorType;
        }
        errorType = EtalioHttpError.UNDEFINED;
        return errorType;
    }

    public String getJsonErrorDescription() throws PropertyNotFoundException {
        final String propertyName = "error_description";
        return getJsonPropertyStringValue(propertyName);
    }

    private String getJsonPropertyStringValue(String propertyName) throws PropertyNotFoundException {
        if (hasJsonError()) {
            try {
                return parsedError.getString(propertyName);
            } catch (Exception e) {
                //  No need to log this exception.
            }
        }
        throw new PropertyNotFoundException();
    }

    private class PropertyNotFoundException extends Throwable {

    }
}