Skip to content

Commit

Permalink
[core] Fix: 缓存可能因为计算机硬碟或程式不稳定造成损毁。 (#2841)
Browse files Browse the repository at this point in the history
* Fix: 缓存可能因为计算机硬碟或程式不稳定造成损毁。

* Apply suggestions from code review

---------

Co-authored-by: Him188 <Him188@mamoe.net>
  • Loading branch information
zhaodice and Him188 authored Mar 3, 2024
1 parent eeb3217 commit 8768058
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions mirai-core-utils/src/commonMain/kotlin/Serialization.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
}
}

0 comments on commit 8768058

Please sign in to comment.