<commit_before>/*
 * Copyright (C) 2016 <NAME>
 *
 * 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.ruesga.rview.misc;

import android.content.Context;
import android.support.annotation.StringRes;
import android.util.Log;

import com.ruesga.rview.R;
import com.ruesga.rview.exceptions.IllegalQueryExpressionException;
import com.ruesga.rview.exceptions.OperationFailedException;
import com.ruesga.rview.gerrit.NoConnectivityException;
import com.ruesga.rview.model.EmptyState;

import java.net.ConnectException;

public class ExceptionHelper {
    @SuppressWarnings("SimplifiableIfStatement")
    public static <T extends Throwable> boolean isException(Throwable cause, Class<T> c) {
        if (c.isInstance(cause)) {
            return true;
        }
        return cause.getCause() != null && isException(cause.getCause(), c);
    }

    public static <T extends Throwable> Throwable getCause(Throwable cause, Class<T> c) {
        if (c.isInstance(cause)) {
            return cause;
        }
        if (cause.getCause() != null) {
            return getCause(cause.getCause(), c);
        }
        return null;
    }

    @SuppressWarnings({"ThrowableResultOfMethodCallIgnored", "ConstantConditions"})
    public static @StringRes int exceptionToMessage(Context context, String tag, Throwable cause) {
        int message;
        if (isException(cause, retrofit2.adapter.rxjava.HttpException.class)) {
            retrofit2.adapter.rxjava.HttpException httpException =
                    (retrofit2.adapter.rxjava.HttpException)
                            getCause(cause, retrofit2.adapter.rxjava.HttpException.class);
            message = httpCode2MessageResource(httpException.code());

        } else if (isException(cause, com.jakewharton.retrofit2.adapter.rxjava2.HttpException.class)) {
            com.jakewharton.retrofit2.adapter.rxjava2.HttpException httpException =
                    (com.jakewharton.retrofit2.adapter.rxjava2.HttpException)
                        getCause(cause, com.jakewharton.retrofit2.adapter.rxjava2.HttpException.class);
            message = httpCode2MessageResource(httpException.code());

        } else if (isException(cause, OperationFailedException.class)) {
            message = R.string.exception_operation_failed;

        } else if (isException(cause, NoConnectivityException.class)) {
            message = R.string.exception_no_network_available;

        } else if (isException(cause, ConnectException.class)) {
            message = R.string.exception_server_cannot_be_reached;

        } else if (isException(cause, IllegalQueryExpressionException.class)) {
            message = R.string.exception_invalid_request;

        } else {
            message = R.string.exception_request_error;
        }

        // Log an return the translated message
        if (exceptionToLevel(cause) == 0) {
            Log.e(tag, context.getString(message), cause);
        } else {
            Log.w(tag, context.getString(message));
        }
        return message;
    }

    private static int httpCode2MessageResource(int code) {
        switch (code) {
            case 400: //Bad request
                return R.string.exception_invalid_request;
            case 403: //Forbidden
                return R.string.exception_insufficient_permissions;
            case 404: //Not found
                return R.string.exception_not_found;
            case 409: //Conflict
                return R.string.exception_conflict;
            default:
                return R.string.exception_bad_request;
        }
    }

    public static int exceptionToLevel(Throwable cause) {
        if (isException(cause, NoConnectivityException.class)
                || isHttpException(cause, 409)
                || isException(cause, IllegalQueryExpressionException.class)) {
            return 1;
        }
        return 0;
    }

    public static boolean hasConnectivity(Throwable cause) {
        return !(isException(cause, NoConnectivityException.class));
    }

    public static boolean hasServerConnectivity(Throwable cause) {
        return !(isException(cause, ConnectException.class));
    }

    @SuppressWarnings({"ThrowableResultOfMethodCallIgnored", "ConstantConditions"})
    private static boolean isHttpException(Throwable cause, int httpCode) {
        if (isException(cause, retrofit2.adapter.rxjava.HttpException.class)) {
            retrofit2.adapter.rxjava.HttpException httpException =
                    (retrofit2.adapter.rxjava.HttpException)
                            getCause(cause, retrofit2.adapter.rxjava.HttpException.class);
            return httpCode == httpException.code();
        }
        if (isException(cause, com.jakewharton.retrofit2.adapter.rxjava2.HttpException.class)) {
            com.jakewharton.retrofit2.adapter.rxjava2.HttpException httpException =
                    (com.jakewharton.retrofit2.adapter.rxjava2.HttpException)
                            getCause(cause, com.jakewharton.retrofit2.adapter.rxjava2.HttpException.class);
            return httpCode == httpException.code();
        }
        return false;
    }

    public static int resolveEmptyState(Throwable error) {
        if (!ExceptionHelper.hasConnectivity(error)) {
            return EmptyState.NOT_CONNECTIVITY_STATE;
        }
        if (!ExceptionHelper.hasServerConnectivity(error)) {
            return EmptyState.SERVER_CANNOT_BE_REACHED;
        }
        return EmptyState.ERROR_STATE;
    }
}
<commit_msg>Add more exceptions to 'server_cannot_be_reached' empty state<commit_after>/*
 * Copyright (C) 2016 <NAME>
 *
 * 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.ruesga.rview.misc;

import android.content.Context;
import android.support.annotation.StringRes;
import android.util.Log;

import com.ruesga.rview.R;
import com.ruesga.rview.exceptions.IllegalQueryExpressionException;
import com.ruesga.rview.exceptions.OperationFailedException;
import com.ruesga.rview.gerrit.NoConnectivityException;
import com.ruesga.rview.model.EmptyState;

import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.PortUnreachableException;

public class ExceptionHelper {
    @SuppressWarnings("SimplifiableIfStatement")
    public static <T extends Throwable> boolean isException(Throwable cause, Class<T> c) {
        if (c.isInstance(cause)) {
            return true;
        }
        return cause.getCause() != null && isException(cause.getCause(), c);
    }

    public static <T extends Throwable> Throwable getCause(Throwable cause, Class<T> c) {
        if (c.isInstance(cause)) {
            return cause;
        }
        if (cause.getCause() != null) {
            return getCause(cause.getCause(), c);
        }
        return null;
    }

    @SuppressWarnings({"ThrowableResultOfMethodCallIgnored", "ConstantConditions"})
    public static @StringRes int exceptionToMessage(Context context, String tag, Throwable cause) {
        int message;
        if (isException(cause, retrofit2.adapter.rxjava.HttpException.class)) {
            retrofit2.adapter.rxjava.HttpException httpException =
                    (retrofit2.adapter.rxjava.HttpException)
                            getCause(cause, retrofit2.adapter.rxjava.HttpException.class);
            message = httpCode2MessageResource(httpException.code());

        } else if (isException(cause, com.jakewharton.retrofit2.adapter.rxjava2.HttpException.class)) {
            com.jakewharton.retrofit2.adapter.rxjava2.HttpException httpException =
                    (com.jakewharton.retrofit2.adapter.rxjava2.HttpException)
                        getCause(cause, com.jakewharton.retrofit2.adapter.rxjava2.HttpException.class);
            message = httpCode2MessageResource(httpException.code());

        } else if (isException(cause, OperationFailedException.class)) {
            message = R.string.exception_operation_failed;

        } else if (isException(cause, NoConnectivityException.class)) {
            message = R.string.exception_no_network_available;

        } else if (isException(cause, ConnectException.class)
                || isException(cause, NoRouteToHostException.class)
                || isException(cause, PortUnreachableException.class)) {
            message = R.string.exception_server_cannot_be_reached;

        } else if (isException(cause, IllegalQueryExpressionException.class)) {
            message = R.string.exception_invalid_request;

        } else {
            message = R.string.exception_request_error;
        }

        // Log an return the translated message
        if (exceptionToLevel(cause) == 0) {
            Log.e(tag, context.getString(message), cause);
        } else {
            Log.w(tag, context.getString(message));
        }
        return message;
    }

    private static int httpCode2MessageResource(int code) {
        switch (code) {
            case 400: //Bad request
                return R.string.exception_invalid_request;
            case 403: //Forbidden
                return R.string.exception_insufficient_permissions;
            case 404: //Not found
                return R.string.exception_not_found;
            case 409: //Conflict
                return R.string.exception_conflict;
            default:
                return R.string.exception_bad_request;
        }
    }

    public static int exceptionToLevel(Throwable cause) {
        if (isException(cause, NoConnectivityException.class)
                || isHttpException(cause, 409)
                || isException(cause, IllegalQueryExpressionException.class)) {
            return 1;
        }
        return 0;
    }

    public static boolean hasConnectivity(Throwable cause) {
        return !(isException(cause, NoConnectivityException.class));
    }

    public static boolean hasServerConnectivity(Throwable cause) {
        return !(isException(cause, ConnectException.class)
                || isException(cause, NoRouteToHostException.class)
                || isException(cause, PortUnreachableException.class));


    }

    @SuppressWarnings({"ThrowableResultOfMethodCallIgnored", "ConstantConditions"})
    private static boolean isHttpException(Throwable cause, int httpCode) {
        if (isException(cause, retrofit2.adapter.rxjava.HttpException.class)) {
            retrofit2.adapter.rxjava.HttpException httpException =
                    (retrofit2.adapter.rxjava.HttpException)
                            getCause(cause, retrofit2.adapter.rxjava.HttpException.class);
            return httpCode == httpException.code();
        }
        if (isException(cause, com.jakewharton.retrofit2.adapter.rxjava2.HttpException.class)) {
            com.jakewharton.retrofit2.adapter.rxjava2.HttpException httpException =
                    (com.jakewharton.retrofit2.adapter.rxjava2.HttpException)
                            getCause(cause, com.jakewharton.retrofit2.adapter.rxjava2.HttpException.class);
            return httpCode == httpException.code();
        }
        return false;
    }

    public static int resolveEmptyState(Throwable error) {
        if (!ExceptionHelper.hasConnectivity(error)) {
            return EmptyState.NOT_CONNECTIVITY_STATE;
        }
        if (!ExceptionHelper.hasServerConnectivity(error)) {
            return EmptyState.SERVER_CANNOT_BE_REACHED;
        }
        return EmptyState.ERROR_STATE;
    }
}

{'fraction_non_alphanumeric': 0.08998875140607424, 'fraction_numerical': 0.008516792543789169, 'mean_word_length': 2.6662739322533135, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2494504', 'n_tokens_mistral': 3483, 'n_tokens_neox': 3305, 'n_words': 696}