Skip to content

Commit

Permalink
修复导入数据源类型错误时闪退, close #1265
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Dec 20, 2024
1 parent 441d3cf commit e00ac6d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package me.him188.ani.app.domain.mediasource.codec

import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import me.him188.ani.datasources.api.source.FactoryId
Expand All @@ -33,12 +34,13 @@ abstract class MediaSourceCodec<T : MediaSourceArguments>(
* 考虑版本兼容性. 潜在地会转换不兼容数据的为当前支持的
* [ExportedMediaSourceData.factoryId] must match current [factoryId]
*/
@Throws(UnsupportedVersionException::class)
@Throws(MediaSourceDecodeException::class)
abstract fun MediaSourceCodecContext.decode(data: ExportedMediaSourceData): T

/**
* 直接使用 [KSerializer] 反序列化 [data]
*/
@Throws(MediaSourceDecodeException::class)
abstract fun MediaSourceCodecContext.deserialize(data: JsonElement): T

override fun toString(): String {
Expand All @@ -56,6 +58,10 @@ class UnsupportedVersionException(
currentVersion: Int,
) : MediaSourceDecodeException(message = "Current version $currentVersion < $unexpectedVersion")

class InvalidMediaSourceContentException(
override val cause: Throwable?,
) : MediaSourceDecodeException(cause = cause)

open class DefaultMediaSourceCodec<T : MediaSourceArguments>(
factoryId: FactoryId,
forClass: KClass<T>,
Expand All @@ -80,7 +86,11 @@ open class DefaultMediaSourceCodec<T : MediaSourceArguments>(
}

override fun MediaSourceCodecContext.deserialize(data: JsonElement): T {
return json.decodeFromJsonElement(serializer, data)
return try {
json.decodeFromJsonElement(serializer, data)
} catch (e: SerializationException) {
throw InvalidMediaSourceContentException(e)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.text.AnnotatedString
import me.him188.ani.app.domain.mediasource.codec.FactoryNotFoundException
import me.him188.ani.app.domain.mediasource.codec.InvalidMediaSourceContentException
import me.him188.ani.app.domain.mediasource.codec.MediaSourceArguments
import me.him188.ani.app.domain.mediasource.codec.MediaSourceCodecManager
import me.him188.ani.app.domain.mediasource.codec.MediaSourceDecodeException
Expand Down Expand Up @@ -86,6 +87,7 @@ class ImportMediaSourceState<T : MediaSourceArguments>(
parseResult = when (e) {
is UnsupportedVersionException -> ParseResult.UnsupportedVersion
is FactoryNotFoundException -> ParseResult.UnsupportedFactory
is InvalidMediaSourceContentException -> ParseResult.InvalidContent
}
return
}
Expand Down Expand Up @@ -191,7 +193,7 @@ fun <T : MediaSourceArguments> MediaSourceConfigurationDefaults.DropdownMenuImpo
when (error) {
ParseResult.EmptyContent -> Text("剪贴板内容为空")
ParseResult.HasMoreThanOneArgument -> Text("剪贴板内容包含多个数据源配置,当前导入功能只支持单个配置")
ParseResult.InvalidContent -> Text("剪贴板内容无效")
ParseResult.InvalidContent -> Text("剪贴板内容无效,请检查数据源类型 (Selector 还是 RSS) 以及导出时使用的 Ani 的版本")
ParseResult.UnsupportedFactory -> Text("数据源类型不受支持,请先升级软件")
ParseResult.UnsupportedVersion -> Text("数据源版本不受支持,请先升级软件")
}
Expand Down

0 comments on commit e00ac6d

Please sign in to comment.