Skip to content

Commit

Permalink
refactor: avoid swallowed exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
JuancaG05 committed Jan 29, 2025
1 parent d357775 commit 56fc727
Show file tree
Hide file tree
Showing 23 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions owncloudApp/src/main/java/com/owncloud/android/MainApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ class MainApp : Application() {
val longVersionCode: Long = PackageInfoCompat.getLongVersionCode(pInfo)
longVersionCode.toInt()
} catch (e: PackageManager.NameNotFoundException) {
Timber.w(e, "Version code not found, using 0 as fallback")
0
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ fun Activity.openFileWithIntent(intentForSavedMimeType: Intent, intentForGuessed
)
)
} catch (anfe: ActivityNotFoundException) {
Timber.i(anfe, "No app found for file type")
showMessageInSnackbar(
message = this.getString(
R.string.file_list_no_app_for_file_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ManageAccountsAdapter(
holder.binding.name.text = oca.displayName
} catch (e: Exception) {
Timber.w(
"Account not found right after being read :\\ ; using account name instead of display name"
e, "Account not found right after being read :\\ ; using account name instead of display name"
)
holder.binding.name.text = AccountUtils.getUsernameOfAccount(account.name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class DocumentsStorageProvider : DocumentsProvider() {
}
}
} catch (e: IOException) {
Timber.e(e, "Couldn't open document")
throw FileNotFoundException("Failed to open document with id $documentId and mode $mode")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class RemoveShareDialogFragment : ConfirmationDialogFragment(), ConfirmationDial
try {
listener = activity as ShareFragmentListener?
} catch (e: IllegalStateException) {
Timber.e(e, "The activity attached doesn't implement OnShareFragmentInteractionListener")
throw IllegalStateException(requireActivity().toString() + " must implement OnShareFragmentInteractionListener")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe
try {
listener = context as ShareFragmentListener?
} catch (e: ClassCastException) {
Timber.e(e, "The activity attached doesn't implement OnShareFragmentInteractionListener")
throw ClassCastException(activity.toString() + " must implement OnShareFragmentInteractionListener")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class EditPrivateShareFragment : DialogFragment() {
try {
listener = activity as ShareFragmentListener?
} catch (e: IllegalStateException) {
Timber.e(e, "The activity attached doesn't implement OnShareFragmentInteractionListener")
throw IllegalStateException(requireActivity().toString() + " must implement OnShareFragmentInteractionListener")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class SearchShareesFragment : Fragment(),
try {
listener = activity as ShareFragmentListener?
} catch (e: ClassCastException) {
Timber.e(e, "The activity attached doesn't implement OnFragmentInteractionListener")
throw ClassCastException(requireActivity().toString() + " must implement OnFragmentInteractionListener")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ class PublicShareDialogFragment : DialogFragment() {
try {
listener = activity as ShareFragmentListener?
} catch (e: IllegalStateException) {
Timber.e(e, "The activity attached doesn't implement OnShareFragmentInteractionListener")
throw IllegalStateException(activity?.toString() + " must implement OnShareFragmentInteractionListener")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class TransfersAdapter(
DisplayUtils.convertIdn(account.name.substring(account.name.lastIndexOf("@") + 1), false)
uploadAccount.text = accountName
} catch (e: Exception) {
Timber.w("Couldn't get display name for account, using old style")
Timber.w(e, "Couldn't get display name for account, using old style")
uploadAccount.text = transferItem.transfer.accountName
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ abstract class DrawerActivity : ToolbarActivity() {
val ocAccount = OwnCloudAccount(account, this)
getDrawerUserName()?.text = ocAccount.displayName
} catch (e: Exception) {
Timber.w("Couldn't read display name of account; using account name instead")
Timber.w(e, "Couldn't read display name of account; using account name instead")
getDrawerUserName()?.text = drawerViewModel.getUsernameOfAccount(account.name)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ open class FolderPickerActivity : FileActivity(),
val currentDir = try {
getCurrentFolder()
} catch (e: NullPointerException) {
Timber.i(e, "Couldn't retrieve current folder, so using file property")
file
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class PreviewAudioFragment : FileFragment() {
imagePreview?.setImageResource(R.drawable.ic_place_holder_music_cover_art)
}
} catch (t: Throwable) {
Timber.i(t, "Couldn't extract cover art, setting default image instead")
imagePreview?.setImageResource(R.drawable.ic_place_holder_music_cover_art)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class SynchronizeFileUseCase(
spaceId = fileToSynchronize.spaceId
)
} catch (exception: FileNotFoundException) {
Timber.i(exception, "File does not exist anymore in remote")
// 1.1 File does not exist anymore in remote
val localFile = fileToSynchronize.id?.let { fileRepository.getFileById(it) }
// If it still exists locally, but file has different path, another operation could have been done simultaneously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class AutomaticUploadsWorker(
checkSourcePathIsAValidUriOrThrowException(pictureUploadsConfiguration.sourcePath)
syncFolder(pictureUploadsConfiguration)
} catch (illegalArgumentException: IllegalArgumentException) {
Timber.e(illegalArgumentException, "Source path for picture uploads is not valid")
showNotificationToUpdateUri(SyncType.PICTURE_UPLOADS)
return Result.failure()
}
Expand All @@ -97,6 +98,7 @@ class AutomaticUploadsWorker(
checkSourcePathIsAValidUriOrThrowException(videoUploadsConfiguration.sourcePath)
syncFolder(videoUploadsConfiguration)
} catch (illegalArgumentException: IllegalArgumentException) {
Timber.e(illegalArgumentException, "Source path for video uploads is not valid")
showNotificationToUpdateUri(SyncType.VIDEO_UPLOADS)
return Result.failure()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class AvailableOfflinePeriodicWorker(

Result.success()
} catch (exception: Exception) {
Timber.e(exception, "Sync of available offline files failed")
Result.failure()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ class OldLogsCollectorWorker(
removeOldLogs(logsFiles)
Result.success()
} catch (ioException: IOException) {
Timber.e(ioException, "An error occurred trying to access the file")
Result.failure()
} catch (securityException: SecurityException) {
Timber.e(securityException, "A security violation was produced")
Result.failure()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.owncloud.android.ui.preview.PreviewVideoActivity
import com.owncloud.android.usecases.files.RemoveLocallyFilesWithLastUsageOlderThanGivenTimeUseCase
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import timber.log.Timber
import java.util.concurrent.TimeUnit

class RemoveLocallyFilesWithLastUsageOlderThanGivenTimeWorker(
Expand All @@ -50,6 +51,7 @@ class RemoveLocallyFilesWithLastUsageOlderThanGivenTimeWorker(
)
Result.success()
} catch (exception: Exception) {
Timber.e(exception, "An error occurred when trying to remove local files")
Result.failure()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCo
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_PREFIX
import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_PREFIX
import org.json.JSONException
import timber.log.Timber

/**
* Checks if the server is valid
Expand Down Expand Up @@ -63,6 +64,7 @@ class GetRemoteStatusOperation : RemoteOperation<RemoteServerInfo>() {
updateClientBaseUrl(client, result.data.baseUrl)
return result
} catch (e: JSONException) {
Timber.e(e, "JSON is not correct")
RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
} catch (e: Exception) {
RemoteOperationResult(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package com.owncloud.android.lib.resources.status

import android.os.Parcel
import android.os.Parcelable
import timber.log.Timber

class OwnCloudVersion(version: String) : Comparable<OwnCloudVersion>, Parcelable {

Expand Down Expand Up @@ -94,6 +95,7 @@ class OwnCloudVersion(version: String) : Comparable<OwnCloudVersion>, Parcelable
isVersionValid = true

} catch (e: Exception) {
Timber.w(e, "Version is invalid")
isVersionValid = false
// if invalid, the instance will respond as if server is 8.1, minimum with capabilities API,
// and "dead" : https://github.com/owncloud/core/wiki/Maintenance-and-Release-Schedule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.owncloud.android.data.extensions

import timber.log.Timber
import java.io.File
import java.io.IOException

Expand Down Expand Up @@ -86,6 +87,7 @@ fun File.moveRecursively(
src.delete()
}
} catch (e: IOException) {
Timber.e(e, "An error occurred while trying to move the file")
src.delete()
dstFile.delete()
}
Expand All @@ -96,6 +98,7 @@ fun File.moveRecursively(
return true

} catch (e: TerminateException) {
Timber.e(e, "The process terminated unexpectedly")
return false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ class OCFileRepository(
spaceWebDavUrl = spaceWebDavUrl,
)
} catch (fileNotFoundException: FileNotFoundException) {
Timber.i("File ${ocFile.fileName} was not found in server. Let's remove it from local storage")
Timber.i(fileNotFoundException, "File ${ocFile.fileName} was not found in server. Let's remove it from local storage")
}
}
ocFile.etagInConflict?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class OCServerInfoRepository(
val openIDConnectServerConfiguration = try {
oidcRemoteOAuthDataSource.performOIDCDiscovery(serverInfo.baseUrl)
} catch (exception: Exception) {
Timber.d("OIDC discovery not found")
Timber.d(exception, "OIDC discovery not found")
null
}

Expand Down Expand Up @@ -85,7 +85,7 @@ class OCServerInfoRepository(
resource = serverUrl,
).firstOrNull()
} catch (exception: Exception) {
Timber.d("Cant retrieve the oidc issuer from webfinger.")
Timber.d(exception, "Cant retrieve the oidc issuer from webfinger.")
null
}

Expand Down

0 comments on commit 56fc727

Please sign in to comment.