Skip to content
Permalink
Browse files

Add some more logging to help fix DB issues

  • Loading branch information...
chrisbanes committed Mar 31, 2019
1 parent ab69ef6 commit 26c3acb70e258a860b9ccf846deec3e227a2a054
@@ -195,6 +195,8 @@ dependencies {

implementation Libs.threeTenAbp

implementation Libs.timber

implementation Libs.RxJava.rxJava
implementation Libs.RxJava.rxKotlin
implementation Libs.RxJava.rxAndroid
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Google LLC
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@

package app.tivi.util

import com.crashlytics.android.Crashlytics
import timber.log.Timber
import javax.inject.Inject

@@ -61,4 +62,16 @@ class TimberLogger @Inject constructor() : Logger {
override fun wtf(t: Throwable, message: String, vararg args: Any) = Timber.wtf(t, message, args)

override fun wtf(t: Throwable) = Timber.wtf(t)

override fun logForCrash(message: String) {
Crashlytics.log(message)
}

override fun logForCrash(message: String, vararg args: Any) {
if (args.isNotEmpty()) {
Crashlytics.log(message.format(args))
} else {
logForCrash(message)
}
}
}
@@ -38,5 +38,4 @@ dependencies {
implementation project(":base")

implementation Libs.AndroidX.Fragment.fragment
implementation Libs.timber
}
@@ -0,0 +1,43 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.tivi.util

import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ExceptionLogger @Inject constructor(
private val logger: Logger
) {
operator fun <T> invoke(message: String, b: () -> T): T {
try {
return b()
} catch (t: Throwable) {
logger.logForCrash(message)
throw t
}
}

operator fun <T> invoke(message: String, vararg args: Any, b: () -> T): T {
try {
return b()
} catch (t: Throwable) {
logger.logForCrash(message, args)
throw t
}
}
}
@@ -70,4 +70,7 @@ interface Logger {

/** Log an assert exception. */
fun wtf(t: Throwable)

fun logForCrash(message: String)
fun logForCrash(message: String, vararg args: Any)
}
@@ -26,6 +26,8 @@ import app.tivi.data.repositories.followedshows.LocalFollowedShowsStore
import app.tivi.data.repositories.shows.LocalShowStore
import app.tivi.data.repositories.shows.ShowRepository
import app.tivi.trakt.TraktAuthState
import app.tivi.util.ExceptionLogger
import app.tivi.util.Logger
import app.tivi.utils.BaseDatabaseTest
import app.tivi.utils.followedShow1
import app.tivi.utils.followedShow1PendingDelete
@@ -69,13 +71,15 @@ class FollowedShowRepositoryTest : BaseDatabaseTest() {

traktDataSource = mock(FollowedShowsDataSource::class.java)

val exceptionLogger = ExceptionLogger(mock(Logger::class.java))
val txRunner = RoomTransactionRunner(db)
val entityInserter = EntityInserter(txRunner, exceptionLogger)

repository = FollowedShowsRepository(
testCoroutineDispatchers,
LocalFollowedShowsStore(txRunner, EntityInserter(txRunner),
db.followedShowsDao(), db.showDao(), db.lastRequestDao()),
LocalShowStore(EntityInserter(txRunner), db.showDao(), db.lastRequestDao(), txRunner),
LocalFollowedShowsStore(txRunner, entityInserter, db.followedShowsDao(), db.showDao(),
db.lastRequestDao(), exceptionLogger),
LocalShowStore(entityInserter, db.showDao(), db.lastRequestDao(), txRunner),
traktDataSource,
showRepository,
Provider { TraktAuthState.LOGGED_IN }
@@ -27,6 +27,8 @@ import app.tivi.data.repositories.episodes.LocalSeasonsEpisodesStore
import app.tivi.data.repositories.episodes.SeasonsEpisodesDataSource
import app.tivi.data.repositories.episodes.SeasonsEpisodesRepository
import app.tivi.trakt.TraktAuthState
import app.tivi.util.ExceptionLogger
import app.tivi.util.Logger
import app.tivi.utils.BaseDatabaseTest
import app.tivi.utils.insertShow
import app.tivi.utils.s1
@@ -75,9 +77,11 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
traktEpisodeDataSource = mock(EpisodeDataSource::class.java)
tmdbEpisodeDataSource = mock(EpisodeDataSource::class.java)

val exceptionLogger = ExceptionLogger(mock(Logger::class.java))
val txRunner = RoomTransactionRunner(db)
val entityInserter = EntityInserter(txRunner, exceptionLogger)

localStore = LocalSeasonsEpisodesStore(EntityInserter(txRunner), txRunner,
localStore = LocalSeasonsEpisodesStore(entityInserter, txRunner,
seasonsDao, episodesDao, episodeWatchDao, db.lastRequestDao())

repository = SeasonsEpisodesRepository(
@@ -18,24 +18,30 @@ package app.tivi.data.daos

import app.tivi.data.DatabaseTransactionRunner
import app.tivi.data.entities.TiviEntity
import app.tivi.util.ExceptionLogger
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class EntityInserter @Inject constructor(
private val transactionRunner: DatabaseTransactionRunner
private val transactionRunner: DatabaseTransactionRunner,
private val exceptionLogger: ExceptionLogger
) {
fun <E : TiviEntity> insertOrUpdate(dao: EntityDao<E>, entities: List<E>) = transactionRunner {
entities.forEach {
insertOrUpdate(dao, it)
}
}

fun <E : TiviEntity> insertOrUpdate(dao: EntityDao<E>, entity: E): Long = when {
entity.id == 0L -> dao.insert(entity)
else -> {
dao.update(entity)
entity.id
fun <E : TiviEntity> insertOrUpdate(dao: EntityDao<E>, entity: E): Long {
return exceptionLogger("insertOrUpdate. Entity: %s", entity) {
when {
entity.id == 0L -> dao.insert(entity)
else -> {
dao.update(entity)
entity.id
}
}
}
}
}
@@ -27,6 +27,7 @@ import app.tivi.data.entities.PendingAction
import app.tivi.data.entities.Request
import app.tivi.data.resultentities.FollowedShowEntryWithShow
import app.tivi.data.syncers.syncerForEntity
import app.tivi.util.ExceptionLogger
import io.reactivex.Flowable
import org.threeten.bp.temporal.TemporalAmount
import javax.inject.Inject
@@ -38,13 +39,18 @@ class LocalFollowedShowsStore @Inject constructor(
private val entityInserter: EntityInserter,
private val followedShowsDao: FollowedShowsDao,
private val showDao: TiviShowDao,
private val lastRequestDao: LastRequestDao
private val lastRequestDao: LastRequestDao,
private val exceptionLogger: ExceptionLogger
) {
var traktListId: Int? = null

private val syncer = syncerForEntity(
followedShowsDao,
{ showDao.getTraktIdForShowId(it.showId)!! },
{
exceptionLogger("getTraktIdForShowId. Show: %s", it) {
showDao.getTraktIdForShowId(it.showId)!!
}
},
{ entity, id -> entity.copy(id = id ?: 0) }
)

0 comments on commit 26c3acb

Please sign in to comment.
You can’t perform that action at this time.