Skip to content

Commit

Permalink
improve error messages in notification
Browse files Browse the repository at this point in the history
  • Loading branch information
amir1376 committed Jul 10, 2024
1 parent 5f4ab58 commit 0abc203
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import ir.amirab.downloader.utils.ExceptionUtils
import ir.amirab.downloader.utils.OnDuplicateStrategy
import com.abdownloadmanager.integration.Integration
import com.abdownloadmanager.integration.IntegrationResult
import ir.amirab.downloader.exception.TooManyErrorException
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -274,43 +275,11 @@ class AppComponent(
)
)
}

init {
downloadSystem
.downloadEvents
.onEach {
if (it.context[ResumedBy]?.by !is User){
//only notify events that is started by user
return@onEach
}
// or
// val qm = downloadSystem.queueManager
// val queueId = qm.findItemInQueue(it.downloadItem.id)
// if (queueId != null) {
// return@onEach
// // skip download events when download is triggered by queue
//// if (qm.getQueue(queue).isQueueActive){
//// return@onEach
//// }
// }
if (it is DownloadManagerEvents.OnJobCanceled) {
if (!ExceptionUtils.isNormalCancellation(it.e)) {
sendNotification(
"downloadId=${it.downloadItem.id}",
title = it.downloadItem.name,
description = "Error" + it.e.localizedMessage?.let { " $it" }.orEmpty(),
type = NotificationType.Error,
)
}
}
if (it is DownloadManagerEvents.OnJobCompleted) {
sendNotification(
tag = "downloadId=${it.downloadItem.id}",
title = it.downloadItem.name,
description = "Finished",
type = NotificationType.Success,
)
}
onNewDownloadEvent(it)
}
.launchIn(scope)
// IntegrationPortBroadcaster.cleanOnClose()
Expand All @@ -337,6 +306,57 @@ class AppComponent(
}.launchIn(scope)
}

private fun onNewDownloadEvent(it: DownloadManagerEvents) {
if (it.context[ResumedBy]?.by !is User){
//only notify events that is started by user
return
}
// or
// val qm = downloadSystem.queueManager
// val queueId = qm.findItemInQueue(it.downloadItem.id)
// if (queueId != null) {
// return@onEach
// // skip download events when download is triggered by queue
//// if (qm.getQueue(queue).isQueueActive){
//// return@onEach
//// }
// }
if (it is DownloadManagerEvents.OnJobCanceled) {
val exception = it.e
if (ExceptionUtils.isNormalCancellation(exception)) {
return
}
var isMaxTryReachedError = false
val actualCause = if (exception is TooManyErrorException){
isMaxTryReachedError=true
exception.findActualDownloadErrorCause()
}else exception
if (ExceptionUtils.isNormalCancellation(actualCause)) {
return
}
val prefix = if (isMaxTryReachedError) {
"Too Many Error: "
}else{
"Error: "
}
val reason = actualCause.message?:"Unknown"
sendNotification(
"downloadId=${it.downloadItem.id}",
title = it.downloadItem.name,
description = prefix + reason,
type = NotificationType.Error,
)
}
if (it is DownloadManagerEvents.OnJobCompleted) {
sendNotification(
tag = "downloadId=${it.downloadItem.id}",
title = it.downloadItem.name,
description = "Finished",
type = NotificationType.Success,
)
}
}

override fun openAddDownloadDialog(
links: List<DownloadCredentials>
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ package ir.amirab.downloader.exception
import ir.amirab.downloader.part.Part

class PartTooManyErrorException(
part: Part
) : Exception("this part $part have too many exception")
part: Part,
override val cause: Throwable
) : Exception(
"this part $part have too many errors",
cause,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package ir.amirab.downloader.exception

class TooManyErrorException(
lastException: Throwable,
override val cause: Throwable
) : Exception(
"Download is stopped because all parts exceeds max retries",
lastException,
)
) {
fun findActualDownloadErrorCause(): Throwable {
return when (cause) {
is PartTooManyErrorException -> cause.cause
else -> cause
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class PartDownloader(
@Volatile
internal var tries = 0

// make sure to not lake resource in this exception
@Volatile
private var lastCriticalException: Throwable? = null

// make sure to not lake resource in this exception
@Volatile
private var lastException: Throwable? = null

Expand All @@ -100,6 +105,7 @@ class PartDownloader(
}
scope.launch {
tries = 0
lastCriticalException = null
lastException = null
val result = kotlin.runCatching {
while (coroutineContext.isActive || !stop) {
Expand All @@ -110,7 +116,10 @@ class PartDownloader(
if (haveToManyErrors()) {
// println("tell them we have error!")
iCantRetryAnymore(
PartTooManyErrorException(part)
PartTooManyErrorException(
part,
lastException?:Exception("BUG : if you see me please report it to the developer! when we encounter error so it have to be a least one last exception"),
)
)
}
if (part.isCompleted) {
Expand Down Expand Up @@ -190,7 +199,7 @@ class PartDownloader(

lateinit var onTooManyErrors: ((Throwable) -> Unit)
private fun iCantRetryAnymore(throwable: Throwable) {
lastException = throwable
lastCriticalException = throwable
GlobalScope.launch {
onTooManyErrors(throwable)
}
Expand All @@ -201,7 +210,7 @@ class PartDownloader(
}

private fun haveCriticalError(): Boolean {
return lastException != null
return lastCriticalException != null
}

internal fun injured(): Boolean {
Expand Down Expand Up @@ -333,6 +342,7 @@ class PartDownloader(
}

private fun onCanceled(e: Throwable) {
lastException = e
val canceled = PartDownloadStatus.Canceled(e)
onNewStatus(canceled)
e.printStackIfNOtUsual()
Expand Down

0 comments on commit 0abc203

Please sign in to comment.