Skip to content

Commit c37ecc2

Browse files
authored
Add LocalStorageException to synctools package (#13)
* Add LocalStorageException to synctools package * Replace CalendarStorageException with generic LocalStorageException * Replace ContactsStorageException with generic LocalStorageException
1 parent 33226ad commit c37ecc2

File tree

14 files changed

+60
-56
lines changed

14 files changed

+60
-56
lines changed

lib/src/androidTest/kotlin/at/bitfire/ical4android/BatchOperationTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import androidx.test.rule.GrantPermissionRule
1818
import at.bitfire.ical4android.impl.TestCalendar
1919
import at.bitfire.ical4android.impl.TestEvent
2020
import at.bitfire.ical4android.util.MiscUtils.closeCompat
21+
import at.bitfire.synctools.LocalStorageException
2122
import net.fortuna.ical4j.model.property.Attendee
2223
import net.fortuna.ical4j.model.property.DtEnd
2324
import net.fortuna.ical4j.model.property.DtStart
@@ -117,7 +118,7 @@ class BatchOperationTest {
117118
}
118119
}
119120

120-
@Test(expected = CalendarStorageException::class)
121+
@Test(expected = LocalStorageException::class)
121122
fun testLargeTransactionSingleRow() {
122123
val event = Event()
123124
event.uid = "sample1@testLargeTransaction"

lib/src/androidTest/kotlin/at/bitfire/ical4android/DmfsTaskTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import android.net.Uri
1414
import at.bitfire.ical4android.impl.TestTask
1515
import at.bitfire.ical4android.impl.TestTaskList
1616
import at.bitfire.ical4android.util.DateUtils
17+
import at.bitfire.synctools.LocalStorageException
1718
import net.fortuna.ical4j.model.Date
1819
import net.fortuna.ical4j.model.DateList
1920
import net.fortuna.ical4j.model.DateTime
@@ -694,7 +695,7 @@ class DmfsTaskTest(
694695
}
695696
}
696697

697-
@Test(expected = CalendarStorageException::class)
698+
@Test(expected = LocalStorageException::class)
698699
fun testAddTaskWithInvalidDue() {
699700
val task = Task()
700701
task.uid = "invalidDUE@ical4android.tests"

lib/src/androidTest/kotlin/at/bitfire/vcard4android/AndroidContactTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.test.filters.MediumTest
1515
import androidx.test.filters.SmallTest
1616
import androidx.test.platform.app.InstrumentationRegistry
1717
import androidx.test.rule.GrantPermissionRule
18+
import at.bitfire.synctools.LocalStorageException
1819
import at.bitfire.vcard4android.impl.TestAddressBook
1920
import at.bitfire.vcard4android.property.XAbDate
2021
import ezvcard.VCardVersion
@@ -169,7 +170,7 @@ class AndroidContactTest {
169170
}
170171
}
171172

172-
@Test(expected = ContactsStorageException::class)
173+
@Test(expected = LocalStorageException::class)
173174
fun testLargeTransactionSingleRow() {
174175
val vcard = Contact()
175176
vcard.displayName = "Large Transaction (one row which is too large)"

lib/src/main/kotlin/at/bitfire/ical4android/AndroidEvent.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import at.bitfire.ical4android.util.TimeApiExtensions.toLocalDate
3434
import at.bitfire.ical4android.util.TimeApiExtensions.toLocalTime
3535
import at.bitfire.ical4android.util.TimeApiExtensions.toRfc5545Duration
3636
import at.bitfire.ical4android.util.TimeApiExtensions.toZonedDateTime
37+
import at.bitfire.synctools.LocalStorageException
3738
import net.fortuna.ical4j.model.Date
3839
import net.fortuna.ical4j.model.DateList
3940
import net.fortuna.ical4j.model.DateTime
@@ -221,7 +222,8 @@ abstract class AndroidEvent(
221222
}
222223

223224
val allDay = (row.getAsInteger(Events.ALL_DAY) ?: 0) != 0
224-
val tsStart = row.getAsLong(Events.DTSTART) ?: throw CalendarStorageException("Found event without DTSTART")
225+
val tsStart = row.getAsLong(Events.DTSTART)
226+
?: throw LocalStorageException("Found event without DTSTART")
225227

226228
var tsEnd = row.getAsLong(Events.DTEND)
227229
var duration = // only use DURATION of DTEND is not defined
@@ -566,7 +568,7 @@ abstract class AndroidEvent(
566568
*
567569
* @return content URI of the created event
568570
*
569-
* @throws CalendarStorageException when the calendar provider doesn't return a result row
571+
* @throws LocalStorageException when the calendar provider doesn't return a result row
570572
* @throws RemoteException on calendar provider errors
571573
*/
572574
fun add(): Uri {
@@ -575,7 +577,7 @@ abstract class AndroidEvent(
575577
batch.commit()
576578

577579
val resultUri = batch.getResult(idxEvent)?.uri
578-
?: throw CalendarStorageException("Empty result from content provider when adding event")
580+
?: throw LocalStorageException("Empty result from content provider when adding event")
579581
id = ContentUris.parseId(resultUri)
580582
return resultUri
581583
}
@@ -691,7 +693,7 @@ abstract class AndroidEvent(
691693
/**
692694
* Updates an already existing event in the calendar storage with the values
693695
* from the instance.
694-
* @throws CalendarStorageException when the calendar provider doesn't return a result row
696+
* @throws LocalStorageException when the calendar provider doesn't return a result row
695697
* @throws RemoteException on calendar provider errors
696698
*/
697699
fun update(event: Event): Uri {

lib/src/main/kotlin/at/bitfire/ical4android/BatchOperation.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import android.content.OperationApplicationException
1414
import android.net.Uri
1515
import android.os.RemoteException
1616
import android.os.TransactionTooLargeException
17+
import at.bitfire.synctools.LocalStorageException
1718
import java.util.LinkedList
1819
import java.util.logging.Level
1920
import java.util.logging.Logger
@@ -51,7 +52,7 @@ class BatchOperation(
5152
* @throws RemoteException on calendar provider errors. In case of [android.os.DeadObjectException],
5253
* the provider has probably been killed/crashed or the calling process is cached and thus IPC is frozen (Android 14+).
5354
*
54-
* @throws CalendarStorageException if
55+
* @throws LocalStorageException if
5556
*
5657
* - the transaction is too large and can't be split (wrapped [TransactionTooLargeException])
5758
* - the batch can't be processed (wrapped [OperationApplicationException])
@@ -94,7 +95,7 @@ class BatchOperation(
9495
* @throws RemoteException on calendar provider errors. In case of [android.os.DeadObjectException],
9596
* the provider has probably been killed/crashed or the calling process is cached and thus IPC is frozen (Android 14+).
9697
*
97-
* @throws CalendarStorageException if
98+
* @throws LocalStorageException if
9899
*
99100
* - the transaction is too large and can't be split (wrapped [TransactionTooLargeException])
100101
* - the batch can't be processed (wrapped [OperationApplicationException])
@@ -115,15 +116,15 @@ class BatchOperation(
115116
System.arraycopy(partResults, 0, results, start, partResults.size)
116117

117118
} catch (e: OperationApplicationException) {
118-
throw CalendarStorageException("Couldn't apply batch operation", e)
119+
throw LocalStorageException("Couldn't apply batch operation", e)
119120

120121
} catch (e: RuntimeException) {
121-
throw CalendarStorageException("Content provider threw a runtime exception", e)
122+
throw LocalStorageException("Content provider threw a runtime exception", e)
122123

123124
} catch(e: TransactionTooLargeException) {
124125
if (end <= start + 1)
125126
// only one operation, can't be split
126-
throw CalendarStorageException("Can't transfer data to content provider (too large data row can't be split)", e)
127+
throw LocalStorageException("Can't transfer data to content provider (too large data row can't be split)", e)
127128

128129
logger.warning("Transaction too large, splitting (losing atomicity)")
129130
val mid = start + (end - start)/2
@@ -148,7 +149,8 @@ class BatchOperation(
148149
val originalIdx = backref.originalIndex
149150
if (originalIdx < start) {
150151
// back reference is outside of the current batch, get result from previous execution ...
151-
val resultUri = results[originalIdx]?.uri ?: throw CalendarStorageException("Referenced operation didn't produce a valid result")
152+
val resultUri = results[originalIdx]?.uri
153+
?: throw LocalStorageException("Referenced operation didn't produce a valid result")
152154
val resultId = ContentUris.parseId(resultUri)
153155
// ... and use result directly instead of using a back reference
154156
cpoBuilder .removeValueBackReference(backrefKey)

lib/src/main/kotlin/at/bitfire/ical4android/CalendarStorageException.kt

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/src/main/kotlin/at/bitfire/ical4android/DmfsTask.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import at.bitfire.ical4android.BatchOperation.CpoBuilder
1515
import at.bitfire.ical4android.util.AndroidTimeUtils
1616
import at.bitfire.ical4android.util.DateUtils
1717
import at.bitfire.ical4android.util.MiscUtils.toValues
18+
import at.bitfire.synctools.LocalStorageException
1819
import net.fortuna.ical4j.model.Date
1920
import net.fortuna.ical4j.model.DateTime
2021
import net.fortuna.ical4j.model.Parameter
@@ -343,7 +344,8 @@ abstract class DmfsTask(
343344

344345
batch.commit()
345346

346-
val resultUri = batch.getResult(0)?.uri ?: throw CalendarStorageException("Empty result from provider when adding a task")
347+
val resultUri = batch.getResult(0)?.uri
348+
?: throw LocalStorageException("Empty result from provider when adding a task")
347349
id = ContentUris.parseId(resultUri)
348350
return resultUri
349351
}

lib/src/main/kotlin/at/bitfire/ical4android/DmfsTaskList.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import android.net.Uri
1414
import androidx.annotation.CallSuper
1515
import at.bitfire.ical4android.util.MiscUtils.asSyncAdapter
1616
import at.bitfire.ical4android.util.MiscUtils.toValues
17+
import at.bitfire.synctools.LocalStorageException
1718
import org.dmfs.tasks.contract.TaskContract
1819
import org.dmfs.tasks.contract.TaskContract.Property.Relation
1920
import org.dmfs.tasks.contract.TaskContract.TaskLists
@@ -48,7 +49,7 @@ abstract class DmfsTaskList<out T : DmfsTask>(
4849
val url = TaskLists.getContentUri(providerName.authority).asSyncAdapter(account)
4950
logger.log(Level.FINE, "Creating ${providerName.authority} task list", info)
5051
return provider.insert(url, info)
51-
?: throw CalendarStorageException("Couldn't create task list (empty result from provider)")
52+
?: throw LocalStorageException("Couldn't create task list (empty result from provider)")
5253
}
5354

5455
fun <T : DmfsTaskList<DmfsTask>> findByID(

lib/src/main/kotlin/at/bitfire/ical4android/JtxCollection.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.content.ContentValues
1313
import android.content.Context
1414
import android.net.Uri
1515
import at.bitfire.ical4android.util.MiscUtils.toValues
16+
import at.bitfire.synctools.LocalStorageException
1617
import at.techbee.jtx.JtxContract
1718
import at.techbee.jtx.JtxContract.asSyncAdapter
1819
import net.fortuna.ical4j.model.Calendar
@@ -36,7 +37,7 @@ open class JtxCollection<out T: JtxICalObject>(val account: Account,
3637
fun create(account: Account, client: ContentProviderClient, values: ContentValues): Uri {
3738
logger.log(Level.FINE, "Creating jtx Board collection", values)
3839
return client.insert(JtxContract.JtxCollection.CONTENT_URI.asSyncAdapter(account), values)
39-
?: throw CalendarStorageException("Couldn't create JTX Collection")
40+
?: throw LocalStorageException("Couldn't create JTX Collection")
4041
}
4142

4243
fun<T: JtxCollection<JtxICalObject>> find(account: Account, client: ContentProviderClient, context: Context, factory: JtxCollectionFactory<T>, where: String?, whereArgs: Array<String>?): List<T> {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* This file is part of bitfireAT/synctools which is released under GPLv3.
3+
* Copyright © All Contributors. See the LICENSE and AUTHOR files in the root directory for details.
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
7+
package at.bitfire.synctools
8+
9+
/**
10+
* Indicates a problem with a local storage operation, such as failing to insert or update a row
11+
* in contacts or calendar storage.
12+
*
13+
* @param message A detail message explaining the cause of the error.
14+
* @param cause The throwable that caused this exception, if any.
15+
*/
16+
class LocalStorageException @JvmOverloads constructor(
17+
message: String?,
18+
cause: Throwable? = null
19+
) : Exception(message, cause)

0 commit comments

Comments
 (0)