Skip to content

Commit dd26ada

Browse files
committed
Safe json load with fallback
1 parent 6e72d98 commit dd26ada

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

common/src/main/kotlin/com/lambda/config/AbstractSetting.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.config
1919

2020
import com.google.gson.JsonElement
21+
import com.lambda.Lambda.LOG
2122
import com.lambda.Lambda.gson
2223
import com.lambda.context.SafeContext
2324
import com.lambda.threading.runSafe
@@ -97,7 +98,12 @@ abstract class AbstractSetting<T : Any>(
9798
gson.toJsonTree(value, type)
9899

99100
override fun loadFromJson(serialized: JsonElement) {
100-
value = gson.fromJson(serialized, type)
101+
runCatching {
102+
value = gson.fromJson(serialized, type)
103+
}.onFailure {
104+
LOG.warn("Failed to load setting ${this.name} with value $serialized. Resetting to default value $defaultValue", it)
105+
value = defaultValue
106+
}
101107
}
102108

103109
/**

common/src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ abstract class Configurable(
7878

7979
override fun loadFromJson(serialized: JsonElement) {
8080
serialized.asJsonObject.entrySet().forEach { (name, value) ->
81-
settings.find {
82-
it.name == name
83-
}?.loadFromJson(value)
81+
settings.find { it.name == name }?.loadFromJson(value)
8482
?: LOG.warn("No saved setting found for $name with $value in ${this::class.simpleName}")
8583
}
8684
}

0 commit comments

Comments
 (0)