Skip to content

Commit

Permalink
Handle SqlNormalizedCache merge APIs Exceptions with ApolloExceptionH…
Browse files Browse the repository at this point in the history
…andler (#4002)

Co-authored-by: OlivierG <[email protected]>
  • Loading branch information
2 people authored and martinbonnin committed Apr 11, 2022
1 parent 833d8d2 commit ce35e46
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,36 @@ class SqlNormalizedCache internal constructor(
return selfRemoved + chainRemoved
}

@OptIn(ApolloExperimental::class)
override fun merge(records: Collection<Record>, cacheHeaders: CacheHeaders): Set<String> {
if (cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE)) {
return emptySet()
}
return cacheQueries.updateRecords(
records = records,
)
return try {
cacheQueries.updateRecords(
records = records,
)
} catch (e: Exception) {
// Unable to merge the records in the database, it is possibly corrupted - treat this as a cache miss
apolloExceptionHandler(Exception("Unable to merge records from the database", e))
emptySet()
}
}

@OptIn(ApolloExperimental::class)
override fun merge(record: Record, cacheHeaders: CacheHeaders): Set<String> {
if (cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE)) {
return emptySet()
}
return cacheQueries.updateRecord(
return try {
cacheQueries.updateRecord(
record = record,
)
)
} catch (e: Exception) {
// Unable to merge the record in the database, it is possibly corrupted - treat this as a cache miss
apolloExceptionHandler(Exception("Unable to merge a record from the database", e))
emptySet()
}
}

override fun dump(): Map<KClass<*>, Map<String, Record>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,24 @@ class SqlNormalizedCacheTest {
apolloExceptionHandler = {
throwable = it
}

badCache.loadRecord(STANDARD_KEY, CacheHeaders.NONE)
assertEquals("Unable to read a record from the database", throwable!!.message)
assertEquals("bad cache", throwable!!.cause!!.message)

throwable = null
badCache.merge(
record = Record(
key = STANDARD_KEY,
fields = mapOf(
"fieldKey" to "valueUpdated",
"newFieldKey" to true,
),
),
cacheHeaders = CacheHeaders.NONE,
)
assertEquals("Unable to merge a record from the database", throwable!!.message)
assertEquals("bad cache", throwable!!.cause!!.message)
}

private class BadCacheQueries(goodCacheQueries: CacheQueries = ApolloDatabase(createDriver()).cacheQueries) : CacheQueries by goodCacheQueries {
Expand Down

0 comments on commit ce35e46

Please sign in to comment.