Skip to content

Commit 0607a11

Browse files
Customizable uncaught exception handling
1 parent b984a89 commit 0607a11

File tree

7 files changed

+26
-20
lines changed

7 files changed

+26
-20
lines changed

invui/src/main/java/xyz/xenondevs/invui/InvUI.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import java.util.ArrayList;
1212
import java.util.List;
13-
import java.util.logging.Logger;
13+
import java.util.function.BiConsumer;
1414

1515
/**
1616
* Main class of InvUI, managing the plugin instance.
@@ -21,6 +21,7 @@ public final class InvUI implements Listener {
2121

2222
private final List<Runnable> disableHandlers = new ArrayList<>();
2323
private @Nullable Plugin plugin;
24+
private BiConsumer<? super String, ? super Throwable> uncaughtExceptionHandler = (msg, e) -> getPlugin().getComponentLogger().error(msg, e);
2425

2526
private InvUI() {}
2627

@@ -86,13 +87,23 @@ public void setPlugin(@Nullable Plugin plugin) {
8687
}
8788

8889
/**
89-
* Gets the logger of the configured plugin instance.
90+
* Sets a handler for uncaught exceptions in InvUI.
9091
*
91-
* @return The logger of the plugin instance.
92-
* @throws IllegalStateException If the plugin instance is not set and cannot be inferred.
92+
* @param uncaughtExceptionHandler The new uncaught exception handler.
93+
*/
94+
public void setUncaughtExceptionHandler(BiConsumer<? super String, ? super Throwable> uncaughtExceptionHandler) {
95+
this.uncaughtExceptionHandler = uncaughtExceptionHandler;
96+
}
97+
98+
/**
99+
* Handles an uncaught exception using the configured
100+
* {@link #setUncaughtExceptionHandler(BiConsumer) uncaught exception handler}.
101+
*
102+
* @param msg An additional message that provides more context.
103+
* @param t The uncaught exception that was thrown.
93104
*/
94-
public Logger getLogger() {
95-
return getPlugin().getLogger();
105+
public void handleUncaughtException(String msg, Throwable t) {
106+
uncaughtExceptionHandler.accept(msg, t);
96107
}
97108

98109
/**

invui/src/main/java/xyz/xenondevs/invui/gui/SlotElement.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.ArrayList;
1515
import java.util.Collections;
1616
import java.util.List;
17-
import java.util.logging.Level;
1817

1918
/**
2019
* Represents an element in a slot in a {@link Gui}.
@@ -66,7 +65,7 @@ record Item(xyz.xenondevs.invui.item.Item item) implements SlotElement {
6665
try {
6766
return item.getItemProvider(player).get(Languages.getInstance().getLocale(player));
6867
} catch (Throwable t) {
69-
InvUI.getInstance().getLogger().log(Level.SEVERE, "Failed to get item stack for item slot element", t);
68+
InvUI.getInstance().handleUncaughtException("Failed to get item stack for item slot element", t);
7069
}
7170

7271
return null;

invui/src/main/java/xyz/xenondevs/invui/internal/menu/CustomContainerMenu.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import java.util.List;
5757
import java.util.Map;
5858
import java.util.concurrent.ConcurrentHashMap;
59-
import java.util.logging.Level;
6059

6160
/**
6261
* A packet-based container menu.
@@ -606,7 +605,7 @@ protected void runInInteractionContext(Runnable run) {
606605
interactionContext.set(true);
607606
run.run();
608607
} catch (Throwable t) {
609-
InvUI.getInstance().getLogger().log(Level.SEVERE, "An exception occurred while handling a window interaction", t);
608+
InvUI.getInstance().handleUncaughtException("An exception occurred while handling a window interaction", t);
610609
} finally {
611610
interactionContext.set(false);
612611
}

invui/src/main/java/xyz/xenondevs/invui/internal/util/CollectionUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static <T> void forEachCatching(Iterable<? extends T> iterable, Consumer<
4343
action.accept(obj);
4444
i++;
4545
} catch (Throwable t) {
46-
InvUI.getInstance().getLogger().log(Level.SEVERE, message + " (" + i + ")", t);
46+
InvUI.getInstance().handleUncaughtException(message + " (" + i + ")", t);
4747
}
4848
}
4949
}

invui/src/main/java/xyz/xenondevs/invui/internal/util/FuncUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static <T> T getSafely(@NonNull Supplier<? extends T> supplier, T fallbac
2323
try {
2424
return supplier.get();
2525
} catch (Throwable t) {
26-
InvUI.getInstance().getLogger().log(Level.SEVERE, "Failed to get value from supplier", t);
26+
InvUI.getInstance().handleUncaughtException("Failed to get value from supplier", t);
2727
return fallback;
2828
}
2929
}

invui/src/main/java/xyz/xenondevs/invui/inventory/Inventory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ public boolean callClickEvent(int slot, Click click) {
451451
try {
452452
handler.accept(clickEvent);
453453
} catch (Throwable t) {
454-
InvUI.getInstance().getLogger().log(Level.SEVERE, "An exception occurred while handling an inventory event", t);
454+
InvUI.getInstance().handleUncaughtException("An exception occurred while handling an inventory event", t);
455455
}
456456
}
457457

@@ -478,7 +478,7 @@ public ItemPreUpdateEvent callPreUpdateEvent(@Nullable UpdateReason updateReason
478478
try {
479479
handler.accept(event);
480480
} catch (Throwable t) {
481-
InvUI.getInstance().getLogger().log(Level.SEVERE, "An exception occurred while handling an inventory event", t);
481+
InvUI.getInstance().handleUncaughtException("An exception occurred while handling an inventory event", t);
482482
}
483483
}
484484
return event;
@@ -503,7 +503,7 @@ public void callPostUpdateEvent(@Nullable UpdateReason updateReason, int slot, @
503503
try {
504504
handler.accept(event);
505505
} catch (Throwable t) {
506-
InvUI.getInstance().getLogger().log(Level.SEVERE, "An exception occurred while handling an inventory event", t);
506+
InvUI.getInstance().handleUncaughtException("An exception occurred while handling an inventory event", t);
507507
}
508508
}
509509
}

invui/src/main/java/xyz/xenondevs/invui/inventory/VirtualInventoryManager.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.io.FileOutputStream;
1212
import java.io.IOException;
1313
import java.util.*;
14-
import java.util.logging.Level;
1514

1615
/**
1716
* Automatically reads and writes {@link VirtualInventory VirtualInventories} to files when the server starts and stops.
@@ -156,8 +155,7 @@ private void deserializeAll() {
156155
VirtualInventory inventory = VirtualInventory.deserialize(in);
157156
inventories.put(inventory.getUuid(), inventory);
158157
} catch (IOException e) {
159-
InvUI.getInstance().getLogger().log(
160-
Level.SEVERE,
158+
InvUI.getInstance().handleUncaughtException(
161159
"Failed to deserialize a VirtualInventory from file " + file.getPath(),
162160
e
163161
);
@@ -176,8 +174,7 @@ private void serializeAll() {
176174
try (FileOutputStream out = new FileOutputStream(file)) {
177175
inventory.serialize(out);
178176
} catch (IOException e) {
179-
InvUI.getInstance().getLogger().log(
180-
Level.SEVERE,
177+
InvUI.getInstance().handleUncaughtException(
181178
"Failed to serialize a VirtualInventory to file " + file.getPath(),
182179
e
183180
);

0 commit comments

Comments
 (0)