Skip to content

Commit

Permalink
Add option to skip malformed components individually
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Feb 11, 2024
1 parent 1f7fcb3 commit 87f8232
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ open class PolymorphicListAsMapSerializer<T : Any>(
// We need primary constructor to be a single serializer for generic serialization to work, use of() if manually creating
private var prefix: String = ""
private var onMissingSerializer: OnMissing = OnMissing.WARN
private var skipMalformedComponents: Boolean = true


val polymorphicSerializer = serializer as? PolymorphicSerializer<T> ?: error("Serializer is not polymorphic")
Expand Down Expand Up @@ -53,7 +54,17 @@ open class PolymorphicListAsMapSerializer<T : Any>(
key
)
}.onSuccess { componentSerializer ->
components += compositeDecoder.decodeMapValue(componentSerializer)
runCatching {
compositeDecoder.decodeMapValue(componentSerializer)
}.onSuccess { components += it }.onFailure {
if (skipMalformedComponents) {
geary.logger.w("Malformed component $key, ignoring")
it.stackTraceToString()
.lineSequence()
.joinToString("\n", limit = 10, truncated = "...")
.let(geary.logger::w)
} else throw it
}
}.onFailure {
when (onMissingSerializer) {
OnMissing.ERROR -> throw it
Expand Down

0 comments on commit 87f8232

Please sign in to comment.