Skip to content

Commit

Permalink
In case the chapter is not present, log a warning and filter out the …
Browse files Browse the repository at this point in the history
…book instead of crashing.
  • Loading branch information
PaulWoitaschek committed Jul 16, 2022
1 parent 2daeb49 commit d3ed964
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 10 additions & 4 deletions data/src/main/kotlin/voice/data/repo/BookRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import voice.data.Book
import voice.data.BookContent
import voice.logging.core.Logger
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -35,7 +36,7 @@ class BookRepository
return contentRepo.flow()
.map { contents ->
contents.filter { it.isActive }
.map { content ->
.mapNotNull { content ->
content.book()
}
}
Expand All @@ -44,7 +45,7 @@ class BookRepository
suspend fun all(): List<Book> {
return contentRepo.all()
.filter { it.isActive }
.map { it.book() }
.mapNotNull { it.book() }
}

fun flow(id: Book.Id): Flow<Book?> {
Expand All @@ -62,12 +63,17 @@ class BookRepository
contentRepo.put(updated)
}

private suspend fun BookContent.book(): Book {
private suspend fun BookContent.book(): Book? {
warmUp()
return Book(
content = this,
chapters = chapters.map { chapterId ->
chapterRepo.get(chapterId)!!
val chapter = chapterRepo.get(chapterId)
if (chapter == null) {
Logger.e("Missing chapter with id=$chapterId for $this")
return null
}
chapter
}
)
}
Expand Down
4 changes: 4 additions & 0 deletions logging/core/src/main/kotlin/voice/logging/core/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ object Logger {
log(severity = Severity.Warn, message = message, throwable = throwable)
}

fun e(message: String) {
log(severity = Severity.Error, message = message, throwable = null)
}

fun e(throwable: Throwable, message: String) {
log(severity = Severity.Error, message = message, throwable = throwable)
}
Expand Down

0 comments on commit d3ed964

Please sign in to comment.