瀏覽代碼

[core] Fix: 缓存可能因为计算机硬碟或程式不稳定造成损毁。 (#2841)

* Fix: 缓存可能因为计算机硬碟或程式不稳定造成损毁。

* Apply suggestions from code review

---------

Co-authored-by: Him188 <Him188@mamoe.net>
赵怡然 1 年之前
父節點
當前提交
876805895b
共有 1 個文件被更改,包括 12 次插入3 次删除
  1. 12 3
      mirai-core-utils/src/commonMain/kotlin/Serialization.kt

+ 12 - 3
mirai-core-utils/src/commonMain/kotlin/Serialization.kt

@@ -72,7 +72,12 @@ public fun <T> MiraiFile.loadNotBlankAs(
     if (!this.exists() || this.length == 0L) {
         return null
     }
-    return stringFormat.decodeFromString(serializer, this.readText())
+    return try {
+        stringFormat.decodeFromString(serializer, this.readText())
+    } catch (e: Throwable) { //broken file
+        e.printStackTrace()
+        null
+    }
 }
 
 public fun <T> MiraiFile.loadNotBlankAs(
@@ -82,6 +87,10 @@ public fun <T> MiraiFile.loadNotBlankAs(
     if (!this.exists() || this.length == 0L) {
         return null
     }
-    return binaryFormat.decodeFromByteArray(serializer, this.readBytes())
+    return try {
+        binaryFormat.decodeFromByteArray(serializer, this.readBytes())
+    } catch (e: Throwable) { //broken file
+        e.printStackTrace()
+        null
+    }
 }
-