User: rednesto Date: 10 Jan 24 16:08 Revision: adef27e1299b825efaafdf0c07e32a06b0531cdf Summary: Catch exceptions while parsing NBTT Can't really do much about users setting too large integer values TeamCity URL: https://ci.mcdev.io/viewModification.html?tab=vcsModificationFiles&modId=8973&personal=false Index: src/main/kotlin/nbt/NbtVirtualFile.kt =================================================================== --- src/main/kotlin/nbt/NbtVirtualFile.kt (revision bb8b39bf7b16f88a18cdd918c42c99ba4533332b) +++ src/main/kotlin/nbt/NbtVirtualFile.kt (revision adef27e1299b825efaafdf0c07e32a06b0531cdf) @@ -25,11 +25,13 @@ import com.demonwav.mcdev.nbt.editor.NbtToolbar import com.demonwav.mcdev.nbt.lang.NbttFile import com.demonwav.mcdev.nbt.lang.NbttLanguage +import com.demonwav.mcdev.util.loggerForTopLevel import com.demonwav.mcdev.util.runReadActionAsync import com.demonwav.mcdev.util.runWriteTaskLater import com.intellij.lang.Language import com.intellij.notification.Notification import com.intellij.notification.NotificationType +import com.intellij.openapi.diagnostic.getOrLogException import com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl import com.intellij.openapi.fileTypes.PlainTextLanguage import com.intellij.openapi.project.Project @@ -41,6 +43,8 @@ import java.util.concurrent.TimeUnit import java.util.zip.GZIPOutputStream +private val LOG = loggerForTopLevel() + fun NbtVirtualFile(backingFile: VirtualFile, project: Project): NbtVirtualFile { var language: Language = NbttLanguage @@ -99,8 +103,20 @@ return@runReadActionAsync } - val rootTag = nbttFile.getRootCompound()?.getRootCompoundTag() + val rootTagParseResult = runCatching { nbttFile.getRootCompound()?.getRootCompoundTag() } + if (rootTagParseResult.isFailure) { + val exception = rootTagParseResult.exceptionOrNull() + Notification( + "NBT Save Error", + MCDevBundle("nbt.file.save_notify.parse_exception.title"), + MCDevBundle("nbt.file.save_notify.parse_exception.content", backingFile.name, exception), + NotificationType.WARNING, + ).notify(project) + return@runReadActionAsync + } + + val rootTag = rootTagParseResult.getOrNull() if (rootTag == null) { Notification( "NBT Save Error", Index: src/main/resources/messages/MinecraftDevelopment.properties =================================================================== --- src/main/resources/messages/MinecraftDevelopment.properties (revision bb8b39bf7b16f88a18cdd918c42c99ba4533332b) +++ src/main/resources/messages/MinecraftDevelopment.properties (revision adef27e1299b825efaafdf0c07e32a06b0531cdf) @@ -176,6 +176,8 @@ nbt.file.save_notify.parse_error.title=Error saving NBT file nbt.file.save_notify.parse_error.content=Due to errors in the text representation, {0} could not be saved. +nbt.file.save_notify.parse_exception.title=Error saving NBT file +nbt.file.save_notify.parse_exception.content=An unexpected exception happened, {0} could not be saved: {1} intention.error.cannot.create.class.message=Cannot create class ''{0}''\n{1} intention.error.cannot.create.class.title=Failed to Create Class