Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8e576ec2 authored by David Luhmer's avatar David Luhmer
Browse files

improve exception handling (add more custom exceptions)

parent 1bd918cf
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -10,4 +10,7 @@ public class Constants {
    // Custom Exceptions
    public static final String EXCEPTION_INVALID_TOKEN = "CE_1";
    public static final String EXCEPTION_ACCOUNT_NOT_FOUND = "CE_2";
    public static final String EXCEPTION_UNSUPPORTED_METHOD = "CE_3";
    public static final String EXCEPTION_INVALID_REQUEST_URL = "CE_4";
    public static final String EXCEPTION_HTTP_REQUEST_FAILED = "CE_5";
}
+13 −0
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@ import com.nextcloud.android.sso.aidl.IThreadListener;
import com.nextcloud.android.sso.aidl.NextcloudRequest;
import com.nextcloud.android.sso.aidl.ParcelFileDescriptorUtil;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;
import com.nextcloud.android.sso.exceptions.NextcloudInvalidRequestUrlException;
import com.nextcloud.android.sso.exceptions.NextcloudUnsupportedMethodException;
import com.nextcloud.android.sso.exceptions.TokenMismatchException;
import com.nextcloud.android.sso.model.SingleSignOnAccount;

@@ -117,12 +120,15 @@ public class NextcloudAPI {
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            Log.i(TAG, "Nextcloud Single sign-on: onServiceConnected");

            mService = IInputStreamService.Stub.asInterface(service);
            mBound = true;
            mCallback.onConnected();
        }

        public void onServiceDisconnected(ComponentName className) {
            Log.e(TAG, "Nextcloud Single sign-on: ServiceDisconnected");
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mService = null;
@@ -199,6 +205,13 @@ public class NextcloudAPI {
                        throw new TokenMismatchException();
                    case Constants.EXCEPTION_ACCOUNT_NOT_FOUND:
                        throw new NextcloudFilesAppAccountNotFoundException();
                    case Constants.EXCEPTION_UNSUPPORTED_METHOD:
                        throw new NextcloudUnsupportedMethodException();
                    case Constants.EXCEPTION_INVALID_REQUEST_URL:
                        throw new NextcloudInvalidRequestUrlException(exception.getCause().getMessage());
                    case Constants.EXCEPTION_HTTP_REQUEST_FAILED:
                        int statusCode = Integer.parseInt(exception.getCause().getMessage());
                        throw new NextcloudHttpRequestFailedException(statusCode);
                    default:
                        throw exception;
                }
+46 −0
Original line number Diff line number Diff line
package com.nextcloud.android.sso.exceptions;

import android.content.Context;

import com.nextcloud.android.sso.R;
import com.nextcloud.android.sso.model.ExceptionMessage;

/**
 *  Nextcloud SingleSignOn
 *
 *  @author David Luhmer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class NextcloudHttpRequestFailedException extends SSOException {

    private int statusCode;

    public NextcloudHttpRequestFailedException(int statusCode) {
        this.statusCode = statusCode;
    }

    @Override
    public void loadExceptionMessage(Context context) {
        this.em = new ExceptionMessage(
                context.getString(R.string.nextcloud_http_request_failed_title),
                context.getString(R.string.nextcloud_http_request_failed_message, statusCode)
        );
    }

    public int getStatusCode() {
        return statusCode;
    }
}
+42 −0
Original line number Diff line number Diff line
package com.nextcloud.android.sso.exceptions;

import android.content.Context;

import com.nextcloud.android.sso.R;
import com.nextcloud.android.sso.model.ExceptionMessage;

/**
 *  Nextcloud SingleSignOn
 *
 *  @author David Luhmer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class NextcloudInvalidRequestUrlException extends SSOException {

    private String text;

    public NextcloudInvalidRequestUrlException(String text) {
        this.text = text;
    }

    @Override
    public void loadExceptionMessage(Context context) {
        this.em = new ExceptionMessage(
                context.getString(R.string.nextcloud_invalid_request_url_title),
                context.getString(R.string.nextcloud_invalid_request_url_message, text)
        );
    }
}
+36 −0
Original line number Diff line number Diff line
package com.nextcloud.android.sso.exceptions;

import android.content.Context;

import com.nextcloud.android.sso.R;
import com.nextcloud.android.sso.model.ExceptionMessage;

/**
 *  Nextcloud SingleSignOn
 *
 *  @author David Luhmer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class NextcloudUnsupportedMethodException extends SSOException {

    @Override
    public void loadExceptionMessage(Context context) {
        this.em = new ExceptionMessage(
                context.getString(R.string.nextcloud_unsupported_method_title),
                context.getString(R.string.nextcloud_unsupported_method_message)
        );
    }
}
Loading