Commit 88efd5ab authored by Ricki Hirner's avatar Ricki Hirner

Use Apache Commons ContextedException instead of own class

parent b44306ea
......@@ -13,4 +13,18 @@ object Constants {
const val DEFAULT_SYNC_INTERVAL = 4 * 3600L // 4 hours
/**
* Context label for [org.apache.commons.lang3.exception.ContextedException].
* Context value is the [at.bitfire.davdroid.resource.LocalResource]
* which is related to the exception cause.
*/
const val EXCEPTION_CONTEXT_LOCAL_RESOURCE = "localResource"
/**
* Context label for [org.apache.commons.lang3.exception.ContextedException].
* Context value is the [okhttp3.HttpUrl] of the remote resource
* which is related to the exception cause.
*/
const val EXCEPTION_CONTEXT_REMOTE_RESOURCE = "remoteResource"
}
/*
* Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.davdroid
import at.bitfire.davdroid.resource.LocalResource
import okhttp3.HttpUrl
class ExceptionInfo(
val exception: Throwable,
val context: Any
): Exception()
\ No newline at end of file
......@@ -25,6 +25,7 @@ import at.bitfire.dav4android.property.GetETag
import at.bitfire.dav4android.property.SyncToken
import at.bitfire.davdroid.*
import at.bitfire.davdroid.BuildConfig
import at.bitfire.davdroid.Constants
import at.bitfire.davdroid.R
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.model.SyncState
......@@ -38,6 +39,7 @@ import at.bitfire.ical4android.TaskProvider
import at.bitfire.vcard4android.ContactsStorageException
import okhttp3.HttpUrl
import okhttp3.RequestBody
import org.apache.commons.lang3.exception.ContextedException
import org.dmfs.tasks.contract.TaskContract
import java.io.IOException
import java.io.InterruptedIOException
......@@ -755,9 +757,12 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L
protected fun<T: ResourceType?, R> useLocal(local: T, body: (T) -> R): R {
try {
return body(local)
} catch(e: Throwable) {
} catch (e: ContextedException) {
e.addContextValue(Constants.EXCEPTION_CONTEXT_LOCAL_RESOURCE, local)
throw e
} catch (e: Throwable) {
if (local != null)
throw ExceptionInfo(e, local)
throw ContextedException(e).setContextValue(Constants.EXCEPTION_CONTEXT_LOCAL_RESOURCE, local)
else
throw e
}
......@@ -766,16 +771,22 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L
protected fun<T: DavResource, R> useRemote(remote: T, body: (T) -> R): R {
try {
return body(remote)
} catch (e: ContextedException) {
e.addContextValue(Constants.EXCEPTION_CONTEXT_REMOTE_RESOURCE, remote.location)
throw e
} catch(e: Throwable) {
throw ExceptionInfo(e, remote.location)
throw ContextedException(e).setContextValue(Constants.EXCEPTION_CONTEXT_REMOTE_RESOURCE, remote.location)
}
}
protected fun<T> useRemote(remote: Response, body: (Response) -> T): T {
try {
return body(remote)
} catch (e: ContextedException) {
e.addContextValue(Constants.EXCEPTION_CONTEXT_REMOTE_RESOURCE, remote.href)
throw e
} catch (e: Throwable) {
throw ExceptionInfo(e, remote.href)
throw ContextedException(e).setContextValue(Constants.EXCEPTION_CONTEXT_REMOTE_RESOURCE, remote.href)
}
}
......@@ -793,17 +804,18 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L
var local: ResourceType? = null
var remote: HttpUrl? = null
while (ex is ExceptionInfo) {
while (ex is ContextedException) {
@Suppress("UNCHECKED_CAST")
(ex.context as? ResourceType)?.let {
// we want the innermost context value, which is the first one
(ex.getFirstContextValue(Constants.EXCEPTION_CONTEXT_LOCAL_RESOURCE) as? ResourceType)?.let {
if (local == null)
local = it
}
(ex.context as? HttpUrl)?.let {
(ex.getFirstContextValue(Constants.EXCEPTION_CONTEXT_REMOTE_RESOURCE) as? HttpUrl)?.let {
if (remote == null)
remote = it
}
ex = ex.exception
ex = ex.cause
}
if (ex != null)
......
......@@ -22,6 +22,7 @@
android:id="@+id/url_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:text="@string/collection_properties_url"
android:labelFor="@id/url" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment