com.lipy.android.http.exception.ActionException.java Source code

Java tutorial

Introduction

Here is the source code for com.lipy.android.http.exception.ActionException.java

Source

/*
 *    Copyright (C) 2016 Tamic
 *
 *    link :https://github.com/Tamicer/Novate
 *
 *    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.lipy.android.http.exception;

import android.net.ParseException;
import android.util.Log;

import com.google.gson.JsonParseException;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;

import retrofit2.HttpException;

/**
 * Created by Tamic on 2016-08-12.
 */
public class ActionException {

    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;
    private static final int ACCESS_DENIED = 302;
    private static final int HANDEL_ERRROR = 417;

    public static Throwable handleException(java.lang.Throwable e) {

        Log.e("Novate", e.getMessage());
        Throwable ex;
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new Throwable(e, ERROR.HTTP_ERROR);
            switch (httpException.code()) {
            case UNAUTHORIZED:
                ex.setMessage("?");
            case FORBIDDEN:
                ex.setMessage("?");
            case NOT_FOUND:
                ex.setMessage("??");
            case REQUEST_TIMEOUT:
                ex.setMessage("");
            case GATEWAY_TIMEOUT:
                ex.setMessage("?");
            case INTERNAL_SERVER_ERROR:
                ex.setMessage("?");
            case BAD_GATEWAY:
                ex.setMessage("");
            case SERVICE_UNAVAILABLE:
                ex.setMessage("???");
            case ACCESS_DENIED:
                ex.setMessage("");
            case HANDEL_ERRROR:
                ex.setMessage("??");
            default:
                ex.setMessage(e.getMessage());
                break;
            }
            ex.setCode(httpException.code());
            return ex;
        } else if (e instanceof ServerException) {
            ServerException resultException = (ServerException) e;
            ex = new Throwable(resultException, resultException.code);
            ex.setMessage(resultException.getMessage());
            return ex;
        } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
            ex = new Throwable(e, ERROR.PARSE_ERROR);
            ex.setMessage("?");
            return ex;
        } else if (e instanceof ConnectException) {
            ex = new Throwable(e, ERROR.NETWORD_ERROR);
            ex.setMessage("");
            return ex;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            ex = new Throwable(e, ERROR.SSL_ERROR);
            ex.setMessage("??");
            return ex;
        } else if (e instanceof java.security.cert.CertPathValidatorException) {
            ex = new Throwable(e, ERROR.SSL_NOT_FOUND);
            ex.setMessage("?");
            return ex;
        } else if (e instanceof ConnectTimeoutException) {
            ex = new Throwable(e, ERROR.TIMEOUT_ERROR);
            ex.setMessage("");
            return ex;
        } else if (e instanceof java.net.SocketTimeoutException) {
            ex = new Throwable(e, ERROR.TIMEOUT_ERROR);
            ex.setMessage("");
            return ex;
        } else if (e instanceof ClassCastException) {
            ex = new Throwable(e, ERROR.FORMAT_ERROR);
            ex.setMessage("?");
            return ex;
        } else if (e instanceof NullPointerException) {
            ex = new Throwable(e, ERROR.NULL);
            ex.setMessage("?");
            return ex;
        } else if (e instanceof FormatException) {
            FormatException resultException = (FormatException) e;
            ex = new Throwable(resultException, resultException.code);
            ex.setMessage(resultException.message);
            return ex;
        } else {

            ex = new Throwable(e, ERROR.UNKNOWN);
            ex.setMessage(e.getLocalizedMessage());
            return ex;
        }
    }

    /**
     * 
     */
    public class ERROR {
        /**
         * 
         */
        public static final int UNKNOWN = 1000;
        /**
         * ?
         */
        public static final int PARSE_ERROR = 1001;
        /**
         * 
         */
        public static final int NETWORD_ERROR = 1002;
        /**
         * ??
         */
        public static final int HTTP_ERROR = 1003;

        /**
         * ?
         */
        public static final int SSL_ERROR = 1005;

        /**
         * 
         */
        public static final int TIMEOUT_ERROR = 1006;

        /**
         * ?
         */
        public static final int SSL_NOT_FOUND = 1007;

        /**
         * 
         */
        public static final int NULL = -100;

        /**
         * ?
         */
        public static final int FORMAT_ERROR = 1008;
    }

}