Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Nextcloud Android SingleSignOn Library
*
* SPDX-FileCopyrightText: 2018-2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package com.nextcloud.android.sso.exceptions;

import androidx.annotation.Nullable;
import com.nextcloud.android.sso.R;

public class NextcloudNetworkException extends SSOException {
public NextcloudNetworkException(@Nullable Throwable cause) {
super("Network connection failed. Please check your internet connection and server URL.",
R.string.network_error_title,
R.string.retry_action,
null,
cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,39 @@ public static SSOException parseNextcloudCustomException(@NonNull Context contex
case Constants.EXCEPTION_INVALID_REQUEST_URL ->
new NextcloudInvalidRequestUrlException(context, exception.getCause());
case Constants.EXCEPTION_HTTP_REQUEST_FAILED -> {
final int statusCode = Integer.parseInt(exception.getCause().getMessage());
final var cause = exception.getCause().getCause();
yield new NextcloudHttpRequestFailedException(context, statusCode, cause);
Throwable rootCause = exception.getCause();
if (rootCause != null && rootCause.getMessage() != null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check NPE.

int statusCode = Integer.parseInt(rootCause.getMessage());
Throwable cause = (rootCause.getCause() != null) ? rootCause.getCause() : null;
yield new NextcloudHttpRequestFailedException(context, statusCode, cause);
} else {
yield new UnknownErrorException(message);
}
}
case Constants.EXCEPTION_ACCOUNT_ACCESS_DECLINED ->
new NextcloudFilesAppAccountPermissionNotGrantedException(context);
default -> new UnknownErrorException(exception.getMessage());
default -> {
if (isNetworkConnectivityError(exception)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check exception and throw NextcloudNetworkException

yield new NextcloudNetworkException(exception);
} else {
yield new UnknownErrorException(exception.getMessage());
}
}
};
}

private static boolean isNetworkConnectivityError(Exception exception) {
Throwable current = exception;
while (current != null) {
if (current instanceof java.net.UnknownHostException ||
current instanceof javax.net.ssl.SSLException ||
current instanceof java.net.SocketException ||
current instanceof java.io.InterruptedIOException ||
current instanceof java.net.HttpRetryException) {
return true;
}
current = current.getCause();
}
return false;
}
}
3 changes: 3 additions & 0 deletions lib/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
-->
<string name="close">Close</string>

<string name="network_error_title">Network Error</string>
<string name="retry_action">Retry</string>

<string name="unknown_error_title">Unknown error</string>

<string name="no_current_account_selected_exception_title">No account selected</string>
Expand Down