/*
 * Copyright (C) 2016 Jorge Ruesga
 *
 * 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 retrofit2.adapter.rxjava.HttpException;

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

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

    @SuppressWarnings({"ThrowableResultOfMethodCallIgnored", "ConstantConditions"})
    public static @StringRes int exceptionToMessage(Context context, String tag, Throwable cause) {
        int message;
        if (isException(cause, HttpException.class)) {
            HttpException httpException = (HttpException) getCause(cause, HttpException.class);
            final int code = httpException.code();
            switch (code) {
                case 400: //Bad request
                    message = R.string.exception_invalid_request;
                    break;
                case 403: //Forbidden
                    message = R.string.exception_insufficient_permissions;
                    break;
                case 404: //Not found
                    message = R.string.exception_not_found;
                    break;
                default:
                    message = R.string.exception_bad_request;
                    break;
            }

        } 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, IllegalQueryExpressionException.class)) {
            message = R.string.exception_invalid_request;

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

        // Log an return the translated message
        Log.e(tag, context.getString(message), cause);
        return message;
    }
}

@@ -1124,18 +1124,12 @@
     
-return !(!
+if (
 c.is
@@ -1135,36 +1135,83 @@
 sInstance(cause)
- && 
+) %7B%0A            return true;%0A        %7D%0A        if (
 cause.getCause()
@@ -1219,16 +1219,18 @@
 != null)
+ %7B
 %0A       
@@ -1238,14 +1238,14 @@
     
-    %7C%7C
+return
  isE
@@ -1271,24 +1271,56 @@
 ause(), c);%0A
+        %7D%0A        return false;%0A
     %7D%0A%0A    p
@@ -1416,17 +1416,16 @@
     if (
-!
 c.isInst
@@ -1439,12 +1439,60 @@
 use)
- && 
+) %7B%0A            return cause;%0A        %7D%0A        if (
 caus
@@ -1593,38 +1593,8 @@
 turn
- c.isInstance(cause) ? cause :
  nul

Fix FC caused by incorrect exception handling