Android Open Source - etalio-android-sdk Etalio Http Exception






From Project

Back to project page etalio-android-sdk.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions...

If you think the Android project etalio-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2014 Ericsson AB//w  ww.  ja v a 2s  . com
 *
 * 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 {

    }
}




Java Source Code List

com.etalio.android.AuthenticationCallback.java
com.etalio.android.EtalioAPI.java
com.etalio.android.EtalioBase.java
com.etalio.android.EtalioExtendedAPI.java
com.etalio.android.EtalioPersistentStore.java
com.etalio.android.Utils.java
com.etalio.android.client.DefaultEtalioHttpClient.java
com.etalio.android.client.GsonHttpBodyConverter.java
com.etalio.android.client.HttpBodyConverter.java
com.etalio.android.client.HttpClient.java
com.etalio.android.client.HttpRequest.java
com.etalio.android.client.HttpResponse.java
com.etalio.android.client.exception.EtalioAuthorizationCodeException.java
com.etalio.android.client.exception.EtalioHttpError.java
com.etalio.android.client.exception.EtalioHttpException.java
com.etalio.android.client.exception.EtalioProfileExpiredException.java
com.etalio.android.client.exception.EtalioTokenException.java
com.etalio.android.client.models.Action.java
com.etalio.android.client.models.Application.java
com.etalio.android.client.models.Country.java
com.etalio.android.client.models.EtalioToken.java
com.etalio.android.client.models.NewUser.java
com.etalio.android.client.models.Operator.java
com.etalio.android.client.models.PromotedApplications.java
com.etalio.android.client.models.Status.java
com.etalio.android.client.models.TokenResponse.java
com.etalio.android.client.models.UserUpdate.java
com.etalio.android.client.models.User.java
com.etalio.android.client.models.Version.java
com.etalio.android.client.models.requests.ApplicationAdd.java
com.etalio.android.client.models.requests.CreateApplication.java
com.etalio.android.client.models.requests.CreateProfile.java
com.etalio.android.client.models.requests.MsisdnRequestClaim.java
com.etalio.android.client.models.requests.MsisdnRequestCodeResponse.java
com.etalio.android.client.models.requests.MsisdnResponseCodeResponse.java
com.etalio.android.client.models.requests.MsisdnResponseCode.java
com.etalio.android.client.models.requests.MsisdnVerifyToken.java
com.etalio.android.client.models.requests.Msisdn.java
com.etalio.android.client.models.requests.PasswordReset.java
com.etalio.android.client.models.requests.ProfileApplicationAdd.java
com.etalio.android.client.models.requests.ProfileSms.java
com.etalio.android.sample.MainActivity.java
com.etalio.android.util.Log.java
com.etalio.android.util.MimeTypeUtil.java