| 1 | package net.minecraft.server;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.io.FileNotFoundException;
|
| 5 | import java.io.IOException;
|
| 6 | import java.util.List;
|
| 7 | import org.apache.commons.lang3.StringUtils;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | public class ChainedJsonException extends IOException {
|
| 11 | private final List<ChainedJsonException.Entry> entries = Lists.newArrayList();
|
| 12 | private final String message;
|
| 13 |
|
| 14 | public ChainedJsonException(final String message) {
|
| 15 | this.entries.add(new ChainedJsonException.Entry());
|
| 16 | this.message = message;
|
| 17 | }
|
| 18 |
|
| 19 | public ChainedJsonException(final String message, final Throwable cause) {
|
| 20 | super(cause);
|
| 21 | this.entries.add(new ChainedJsonException.Entry());
|
| 22 | this.message = message;
|
| 23 | }
|
| 24 |
|
| 25 | public void prependJsonKey(final String key) {
|
| 26 | this.entries.get(0).addJsonKey(key);
|
| 27 | }
|
| 28 |
|
| 29 | public void setFilenameAndFlush(final String filename) {
|
| 30 | this.entries.get(0).filename = filename;
|
| 31 | this.entries.add(0, new ChainedJsonException.Entry());
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public String getMessage() {
|
| 36 | return "Invalid " + this.entries.get(this.entries.size() - 1) + ": " + this.message;
|
| 37 | }
|
| 38 |
|
| 39 | public static ChainedJsonException forException(final Exception e) {
|
| 40 | if (e instanceof ChainedJsonException chainedJsonException) {
|
| 41 | return chainedJsonException;
|
| 42 | } else {
|
| 43 | String message = e.getMessage();
|
| 44 | if (e instanceof FileNotFoundException) {
|
| 45 | message = "File not found";
|
| 46 | }
|
| 47 |
|
| 48 | return new ChainedJsonException(message, e);
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | public static class Entry {
|
| 53 | private @Nullable String filename;
|
| 54 | private final List<String> jsonKeys = Lists.newArrayList();
|
| 55 |
|
| 56 | private Entry() {
|
| 57 | }
|
| 58 |
|
| 59 | private void addJsonKey(final String name) {
|
| 60 | this.jsonKeys.add(0, name);
|
| 61 | }
|
| 62 |
|
| 63 | public @Nullable String getFilename() {
|
| 64 | return this.filename;
|
| 65 | }
|
| 66 |
|
| 67 | public String getJsonKeys() {
|
| 68 | return StringUtils.join(this.jsonKeys, "->");
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public String toString() {
|
| 73 | if (this.filename != null) {
|
| 74 | return this.jsonKeys.isEmpty() ? this.filename : this.filename + " " + this.getJsonKeys();
|
| 75 | } else {
|
| 76 | return this.jsonKeys.isEmpty() ? "(Unknown file)" : "(Unknown file) " + this.getJsonKeys();
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|