diff options
| author | 2022-12-07 20:08:19 +0100 | |
|---|---|---|
| committer | 2022-12-07 19:08:19 +0000 | |
| commit | e0efb9892240f26f3688e923b555fc8077fd9a74 (patch) | |
| tree | 695b81cf08b9119bf65176241dd05eb3ce7c83fe | |
| parent | Don't save empty classes (#474) (diff) | |
| download | enigma-e0efb9892240f26f3688e923b555fc8077fd9a74.tar.gz enigma-e0efb9892240f26f3688e923b555fc8077fd9a74.tar.xz enigma-e0efb9892240f26f3688e923b555fc8077fd9a74.zip | |
Improve error handling in some places (#464)
* Improve error handling in some places
* Address PR feedback
* Address PR feedback (2)
Co-authored-by: modmuss50 <modmuss50@gmail.com>
* Address PR feedback (3)
* Fix checkstyle
Co-authored-by: modmuss50 <modmuss50@gmail.com>
6 files changed, 29 insertions, 19 deletions
diff --git a/enigma/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java b/enigma/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java index 60864ba5..336ad58c 100644 --- a/enigma/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java +++ b/enigma/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java | |||
| @@ -84,7 +84,11 @@ public class JarIndex implements JarIndexer { | |||
| 84 | progress.step(2, I18n.translate("progress.jar.indexing.references")); | 84 | progress.step(2, I18n.translate("progress.jar.indexing.references")); |
| 85 | 85 | ||
| 86 | for (String className : classNames) { | 86 | for (String className : classNames) { |
| 87 | classProvider.get(className).accept(new IndexReferenceVisitor(this, entryIndex, inheritanceIndex, Enigma.ASM_VERSION)); | 87 | try { |
| 88 | classProvider.get(className).accept(new IndexReferenceVisitor(this, entryIndex, inheritanceIndex, Enigma.ASM_VERSION)); | ||
| 89 | } catch (Exception e) { | ||
| 90 | throw new RuntimeException("Exception while indexing class: " + className, e); | ||
| 91 | } | ||
| 88 | } | 92 | } |
| 89 | 93 | ||
| 90 | progress.step(3, I18n.translate("progress.jar.indexing.methods")); | 94 | progress.step(3, I18n.translate("progress.jar.indexing.methods")); |
diff --git a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingParseException.java b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingParseException.java index 6f5d7d6e..ec05dabd 100644 --- a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingParseException.java +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingParseException.java | |||
| @@ -12,27 +12,32 @@ | |||
| 12 | package cuchaz.enigma.translation.mapping.serde; | 12 | package cuchaz.enigma.translation.mapping.serde; |
| 13 | 13 | ||
| 14 | import java.io.File; | 14 | import java.io.File; |
| 15 | import java.util.function.Supplier; | 15 | import java.nio.file.Path; |
| 16 | 16 | ||
| 17 | public class MappingParseException extends Exception { | 17 | public class MappingParseException extends Exception { |
| 18 | private int line; | 18 | private int line; |
| 19 | private String message; | 19 | private String message; |
| 20 | private String filePath; | 20 | private Path filePath; |
| 21 | 21 | ||
| 22 | public MappingParseException(File file, int line, String message) { | 22 | public MappingParseException(File file, int line, String message) { |
| 23 | this(file.toPath(), line, message); | ||
| 24 | } | ||
| 25 | |||
| 26 | public MappingParseException(Path filePath, int line, String message) { | ||
| 23 | this.line = line; | 27 | this.line = line; |
| 24 | this.message = message; | 28 | this.message = message; |
| 25 | filePath = file.getAbsolutePath(); | 29 | this.filePath = filePath; |
| 26 | } | 30 | } |
| 27 | 31 | ||
| 28 | public MappingParseException(Supplier<String> filenameProvider, int line, String message) { | 32 | public MappingParseException(Path filePath, int line, Throwable cause) { |
| 33 | super(cause); | ||
| 29 | this.line = line; | 34 | this.line = line; |
| 30 | this.message = message; | 35 | this.message = cause.toString(); |
| 31 | filePath = filenameProvider.get(); | 36 | this.filePath = filePath; |
| 32 | } | 37 | } |
| 33 | 38 | ||
| 34 | @Override | 39 | @Override |
| 35 | public String getMessage() { | 40 | public String getMessage() { |
| 36 | return "Line " + line + ": " + message + " in file " + filePath; | 41 | return "Line " + line + ": " + message + " in file " + filePath.toString(); |
| 37 | } | 42 | } |
| 38 | } | 43 | } |
diff --git a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/enigma/EnigmaMappingsReader.java b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/enigma/EnigmaMappingsReader.java index 1f8b7558..54613426 100644 --- a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/enigma/EnigmaMappingsReader.java +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/enigma/EnigmaMappingsReader.java | |||
| @@ -4,6 +4,7 @@ import java.io.IOException; | |||
| 4 | import java.nio.file.FileSystem; | 4 | import java.nio.file.FileSystem; |
| 5 | import java.nio.file.FileSystems; | 5 | import java.nio.file.FileSystems; |
| 6 | import java.nio.file.Files; | 6 | import java.nio.file.Files; |
| 7 | import java.nio.file.NotDirectoryException; | ||
| 7 | import java.nio.file.Path; | 8 | import java.nio.file.Path; |
| 8 | import java.util.ArrayDeque; | 9 | import java.util.ArrayDeque; |
| 9 | import java.util.Arrays; | 10 | import java.util.Arrays; |
| @@ -52,8 +53,11 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 52 | DIRECTORY { | 53 | DIRECTORY { |
| 53 | @Override | 54 | @Override |
| 54 | public EntryTree<EntryMapping> read(Path root, ProgressListener progress, MappingSaveParameters saveParameters) throws IOException, MappingParseException { | 55 | public EntryTree<EntryMapping> read(Path root, ProgressListener progress, MappingSaveParameters saveParameters) throws IOException, MappingParseException { |
| 55 | EntryTree<EntryMapping> mappings = new HashEntryTree<>(); | 56 | if (!Files.isDirectory(root)) { |
| 57 | throw new NotDirectoryException(root.toString()); | ||
| 58 | } | ||
| 56 | 59 | ||
| 60 | EntryTree<EntryMapping> mappings = new HashEntryTree<>(); | ||
| 57 | List<Path> files = Files.walk(root).filter(f -> !Files.isDirectory(f)).filter(f -> f.toString().endsWith(".mapping")).toList(); | 61 | List<Path> files = Files.walk(root).filter(f -> !Files.isDirectory(f)).filter(f -> f.toString().endsWith(".mapping")).toList(); |
| 58 | 62 | ||
| 59 | progress.init(files.size(), I18n.translate("progress.mappings.enigma_directory.loading")); | 63 | progress.init(files.size(), I18n.translate("progress.mappings.enigma_directory.loading")); |
| @@ -132,8 +136,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 132 | mappingStack.push(pair); | 136 | mappingStack.push(pair); |
| 133 | } | 137 | } |
| 134 | } catch (Throwable t) { | 138 | } catch (Throwable t) { |
| 135 | t.printStackTrace(); | 139 | throw new MappingParseException(path, lineNumber + 1, t); |
| 136 | throw new MappingParseException(path::toString, lineNumber + 1, t.toString()); | ||
| 137 | } | 140 | } |
| 138 | } | 141 | } |
| 139 | 142 | ||
| @@ -183,7 +186,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 183 | 186 | ||
| 184 | for (int i = 0; i < line.length(); i++) { | 187 | for (int i = 0; i < line.length(); i++) { |
| 185 | if (line.charAt(i) == ' ') { | 188 | if (line.charAt(i) == ' ') { |
| 186 | throw new MappingParseException(path::toString, lineNumber + 1, "Spaces must not be used to indent lines!"); | 189 | throw new MappingParseException(path, lineNumber + 1, "Spaces must not be used to indent lines!"); |
| 187 | } | 190 | } |
| 188 | 191 | ||
| 189 | if (line.charAt(i) != '\t') { | 192 | if (line.charAt(i) != '\t') { |
diff --git a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/proguard/ProguardMappingsReader.java b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/proguard/ProguardMappingsReader.java index fc550d4d..b839c079 100644 --- a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/proguard/ProguardMappingsReader.java +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/proguard/ProguardMappingsReader.java | |||
| @@ -62,7 +62,7 @@ public class ProguardMappingsReader implements MappingsReader { | |||
| 62 | String targetName = fieldMatcher.group(3); | 62 | String targetName = fieldMatcher.group(3); |
| 63 | 63 | ||
| 64 | if (currentClass == null) { | 64 | if (currentClass == null) { |
| 65 | throw new MappingParseException(path::toString, lineNumber, "field mapping not inside class: " + line); | 65 | throw new MappingParseException(path, lineNumber, "field mapping not inside class: " + line); |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | mappings.insert(new FieldEntry(currentClass, name, new TypeDescriptor(getDescriptor(type))), new EntryMapping(targetName)); | 68 | mappings.insert(new FieldEntry(currentClass, name, new TypeDescriptor(getDescriptor(type))), new EntryMapping(targetName)); |
| @@ -73,12 +73,12 @@ public class ProguardMappingsReader implements MappingsReader { | |||
| 73 | String targetName = methodMatcher.group(4); | 73 | String targetName = methodMatcher.group(4); |
| 74 | 74 | ||
| 75 | if (currentClass == null) { | 75 | if (currentClass == null) { |
| 76 | throw new MappingParseException(path::toString, lineNumber, "method mapping not inside class: " + line); | 76 | throw new MappingParseException(path, lineNumber, "method mapping not inside class: " + line); |
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | mappings.insert(new MethodEntry(currentClass, name, new MethodDescriptor(getDescriptor(returnType, parameterTypes))), new EntryMapping(targetName)); | 79 | mappings.insert(new MethodEntry(currentClass, name, new MethodDescriptor(getDescriptor(returnType, parameterTypes))), new EntryMapping(targetName)); |
| 80 | } else { | 80 | } else { |
| 81 | throw new MappingParseException(path::toString, lineNumber, "invalid mapping line: " + line); | 81 | throw new MappingParseException(path, lineNumber, "invalid mapping line: " + line); |
| 82 | } | 82 | } |
| 83 | } | 83 | } |
| 84 | 84 | ||
diff --git a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/tiny/TinyMappingsReader.java b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/tiny/TinyMappingsReader.java index e08c8673..534a567c 100644 --- a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/tiny/TinyMappingsReader.java +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/tiny/TinyMappingsReader.java | |||
| @@ -50,8 +50,7 @@ public enum TinyMappingsReader implements MappingsReader { | |||
| 50 | MappingPair<?, EntryMapping> mapping = parseLine(line); | 50 | MappingPair<?, EntryMapping> mapping = parseLine(line); |
| 51 | mappings.insert(mapping.getEntry(), mapping.getMapping()); | 51 | mappings.insert(mapping.getEntry(), mapping.getMapping()); |
| 52 | } catch (Throwable t) { | 52 | } catch (Throwable t) { |
| 53 | t.printStackTrace(); | 53 | throw new MappingParseException(path, lineNumber, t); |
| 54 | throw new MappingParseException(path::toString, lineNumber, t.toString()); | ||
| 55 | } | 54 | } |
| 56 | } | 55 | } |
| 57 | 56 | ||
diff --git a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/tinyv2/TinyV2Reader.java b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/tinyv2/TinyV2Reader.java index 61bc41da..eecbc35b 100644 --- a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/tinyv2/TinyV2Reader.java +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/tinyv2/TinyV2Reader.java | |||
| @@ -187,8 +187,7 @@ public final class TinyV2Reader implements MappingsReader { | |||
| 187 | unsupportKey(parts); | 187 | unsupportKey(parts); |
| 188 | } | 188 | } |
| 189 | } catch (Throwable t) { | 189 | } catch (Throwable t) { |
| 190 | t.printStackTrace(); | 190 | throw new MappingParseException(path, lineNumber + 1, t); |
| 191 | throw new MappingParseException(path::toString, lineNumber + 1, t.toString()); | ||
| 192 | } | 191 | } |
| 193 | } | 192 | } |
| 194 | 193 | ||