mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Improve event exception handling
This commit is contained in:
2 files changed
+31
-12
No files matched your search
@@ -22,6 +22,7 @@ package net.draycia.carbon.common.event;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import com.seiama.event.EventConfig;
|
||||
import com.seiama.event.EventSubscriber;
|
||||
import com.seiama.event.EventSubscription;
|
||||
import com.seiama.event.bus.EventBus;
|
||||
import com.seiama.event.bus.SimpleEventBus;
|
||||
@@ -32,6 +33,7 @@ import net.draycia.carbon.api.event.CarbonEvent;
|
||||
import net.draycia.carbon.api.event.CarbonEventHandler;
|
||||
import net.draycia.carbon.api.event.CarbonEventSubscriber;
|
||||
import net.draycia.carbon.api.event.CarbonEventSubscription;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@@ -44,16 +46,21 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@Singleton
|
||||
public final class CarbonEventHandlerImpl implements CarbonEventHandler {
|
||||
|
||||
@Inject
|
||||
private CarbonEventHandlerImpl() {
|
||||
private final Logger logger;
|
||||
|
||||
@Inject
|
||||
private CarbonEventHandlerImpl(final Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
private final EventRegistry<CarbonEvent> eventRegistry = new SimpleEventRegistry<>(CarbonEvent.class);
|
||||
private final EventBus<CarbonEvent> eventBus = new SimpleEventBus<>(this.eventRegistry, this::onException);
|
||||
|
||||
private <E> void onException(final EventSubscription<E> subscription, final E event, final Throwable throwable) {
|
||||
throwable.printStackTrace();
|
||||
final Object subscriber = subscription.subscriber() instanceof SubscriberWrapper<?> wrapped
|
||||
? wrapped.carbon
|
||||
: subscription.subscriber();
|
||||
this.logger.warn("Exception posting event '{}' to subscriber '{}'", event, subscriber, throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,7 +71,7 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
|
||||
return new CarbonEventSubscriptionImpl<>(
|
||||
eventClass,
|
||||
subscriber,
|
||||
this.eventRegistry.subscribe(eventClass, subscriber::on)
|
||||
this.eventRegistry.subscribe(eventClass, new SubscriberWrapper<>(subscriber, true))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -80,13 +87,7 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
|
||||
return new CarbonEventSubscriptionImpl<>(
|
||||
eventClass,
|
||||
subscriber,
|
||||
this.eventRegistry.subscribe(eventClass, eventConfig, event -> {
|
||||
// Our events implement seiama Cancellable; but API consumers won't be able to do that
|
||||
if (!acceptsCancelled && event instanceof Cancellable cancellable && cancellable.cancelled()) {
|
||||
return;
|
||||
}
|
||||
subscriber.on(event);
|
||||
})
|
||||
this.eventRegistry.subscribe(eventClass, eventConfig, new SubscriberWrapper<>(subscriber, acceptsCancelled))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -95,4 +96,20 @@ public final class CarbonEventHandlerImpl implements CarbonEventHandler {
|
||||
this.eventBus.post(event);
|
||||
}
|
||||
|
||||
private record SubscriberWrapper<T extends CarbonEvent>(
|
||||
CarbonEventSubscriber<T> carbon,
|
||||
boolean acceptsCancelled
|
||||
) implements EventSubscriber<T> {
|
||||
|
||||
@Override
|
||||
public void on(final T event) throws Throwable {
|
||||
// Our events implement seiama Cancellable; but API consumers won't be able to do that
|
||||
if (!this.acceptsCancelled && event instanceof Cancellable cancellable && cancellable.cancelled()) {
|
||||
return;
|
||||
}
|
||||
this.carbon.on(event);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user