Skip to content

Commit 3ff3ef4

Browse files
committed
Extra error management for event handlers.
When CH or a CH extension receives an event it cannot handle due to being binary incompatible with a dependency, the error remained uncaught and was passed to the server software that fired the event. This commit makes sure those errors are caught and logged with useful additional details for a bug report.
1 parent 610827e commit 3ff3ef4

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

src/main/java/com/laytonsmith/core/events/EventUtils.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
package com.laytonsmith.core.events;
22

3+
import com.laytonsmith.PureUtilities.TermColors;
4+
import com.laytonsmith.abstraction.Implementation;
5+
import com.laytonsmith.abstraction.StaticLayer;
6+
import com.laytonsmith.core.Static;
37
import com.laytonsmith.core.constructs.CArray;
48
import com.laytonsmith.core.constructs.CString;
59
import com.laytonsmith.core.constructs.Construct;
610
import com.laytonsmith.core.constructs.Target;
711
import com.laytonsmith.core.events.BoundEvent.Priority;
812
import com.laytonsmith.core.exceptions.CRE.CREBindException;
913
import com.laytonsmith.core.exceptions.CRE.CREEventException;
14+
import com.laytonsmith.core.extensions.Extension;
15+
import com.laytonsmith.core.extensions.ExtensionManager;
16+
import com.laytonsmith.core.extensions.ExtensionTracker;
1017
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
1118
import com.laytonsmith.core.exceptions.EventException;
1219
import com.laytonsmith.core.exceptions.FunctionReturnException;
1320
import com.laytonsmith.core.exceptions.PrefilterNonMatchException;
21+
22+
import java.io.File;
1423
import java.util.EnumMap;
1524
import java.util.Iterator;
1625
import java.util.Map;
1726
import java.util.Set;
1827
import java.util.SortedSet;
1928
import java.util.TreeSet;
29+
import java.util.logging.Level;
2030

2131
/**
2232
*
@@ -200,6 +210,63 @@ public static SortedSet<BoundEvent> GetMatchingEvents(Driver type, String eventN
200210
//prefilter was configured improperly with bad runtime data.
201211
//We use the environment from the bound event.
202212
ConfigRuntimeException.HandleUncaughtException(ex, b.getEnvironment());
213+
} catch (NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError err) {
214+
// This happens when a CH extension depends on a not-included or binary outdated class.
215+
// Log the error and continue since there's nothing we can do about it.
216+
217+
String chBrand = Implementation.GetServerType().getBranding();
218+
String chVersion = Static.getVersion().toString();
219+
220+
String culprit = chBrand;
221+
outerLoop:
222+
for(ExtensionTracker tracker : ExtensionManager.getTrackers().values()) {
223+
for(Event event : tracker.getEvents()) {
224+
if(event.getName().equals(driver.getName())) {
225+
for(Extension extension : tracker.getExtensions()) {
226+
culprit = extension.getName();
227+
break outerLoop;
228+
}
229+
}
230+
}
231+
}
232+
233+
String modVersion;
234+
try{
235+
modVersion = StaticLayer.GetConvertor().GetServer().getAPIVersion();
236+
} catch(Exception ex){
237+
modVersion = Implementation.GetServerType().name();
238+
}
239+
240+
String extensionData = "";
241+
for(ExtensionTracker tracker : ExtensionManager.getTrackers().values()){
242+
for(Extension extension : tracker.getExtensions()){
243+
try {
244+
extensionData += TermColors.CYAN + extension.getName() + TermColors.RED
245+
+ " (" + TermColors.RESET + extension.getVersion() + TermColors.RED + ")\n";
246+
} catch(AbstractMethodError ex){
247+
// This happens with an old style extensions. Just skip it.
248+
extensionData += TermColors.CYAN + "Unknown Extension" + TermColors.RED + "\n";
249+
}
250+
}
251+
}
252+
if(extensionData.isEmpty()){
253+
extensionData = "NONE\n";
254+
}
255+
256+
String driverEventName = driver.getName();
257+
String jarName = new File(driver.getSourceJar().getFile()).getName();
258+
String emsg = TermColors.RED + "Uh oh! You've found an error in the eventhandler for event "
259+
+ TermColors.CYAN + driverEventName + TermColors.RED + ", implemented in "
260+
+ TermColors.CYAN + culprit + " (" + jarName + ")" + TermColors.RED + ".\n"
261+
+ "Please report this to the developers, and be sure to include the version numbers:\n"
262+
+ TermColors.CYAN + "Server" + TermColors.RED + " version: "
263+
+ TermColors.RESET + modVersion + TermColors.RED + ";\n"
264+
+ TermColors.CYAN + chBrand + TermColors.RED + " version: "
265+
+ TermColors.RESET + chVersion + TermColors.RED + ";\n"
266+
+ "Loaded extensions and versions:\n" + extensionData
267+
+ "Here's the stacktrace:\n" + TermColors.RESET + Static.GetStacktraceString(err);
268+
Static.getLogger().log(Level.SEVERE, emsg);
269+
continue; // If we can't match it, it's not a match.
203270
}
204271
if (b.getEventName().equals(eventName) && matches) {
205272
toRun.add(b);

0 commit comments

Comments
 (0)