-
-
Notifications
You must be signed in to change notification settings - Fork 391
RuntimeErrorCatcher + Runtime Elements #7823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Absolutionism
wants to merge
13
commits into
SkriptLang:dev/feature
Choose a base branch
from
Absolutionism:dev/RuntimeLogHandler
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5611e73
Initial Commit
Absolutionism bd12b44
Fix Java 17
Absolutionism 7758d33
Consumer Usage
Absolutionism 99f317f
Update SecRuntime.java
Absolutionism 66694e0
Requested Changes
Absolutionism 7b54ff0
Update RuntimeErrorCatcher.java
Absolutionism e203ad3
Additional Changes
Absolutionism 4410ed6
Update SecCatchErrors.java
Absolutionism f33c15d
Update SecCatchErrors.java
Absolutionism 059dfb8
Merge remote-tracking branch 'upstream/dev/feature' into dev/RuntimeL…
Absolutionism 22f3881
Requested Changes
Absolutionism c40e1e6
Update SecCatchErrors.java
Absolutionism 00bef2b
Merge branch 'dev/feature' into dev/RuntimeLogHandler
Absolutionism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/ch/njol/skript/expressions/ExprCaughtErrors.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Example; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.ExpressionType; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.lang.util.SimpleExpression; | ||
import ch.njol.util.Kleenean; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@Name("Last Caught Errors") | ||
@Description("Gets the last caught runtime errors from a 'catch runtime errors' section.") | ||
@Example(""" | ||
catch runtime errors: | ||
set worldborder center of {_border} to location(0, 0, NaN value) | ||
if last caught runtime errors contains "Your location can't have a NaN value as one of its components": | ||
set worldborder center of {_border} to location(0, 0, 0) | ||
""") | ||
@Since("INSERT VERSION") | ||
public class ExprCaughtErrors extends SimpleExpression<String> { | ||
|
||
static { | ||
Skript.registerExpression(ExprCaughtErrors.class, String.class, ExpressionType.SIMPLE, | ||
"[the] last caught [run[ ]time] errors"); | ||
} | ||
|
||
public static String[] lastErrors; | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected String @Nullable [] get(Event event) { | ||
return lastErrors; | ||
} | ||
|
||
@Override | ||
public boolean isSingle() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Class<? extends String> getReturnType() { | ||
return String.class; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return "last caught runtime errors"; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package ch.njol.skript.sections; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.config.SectionNode; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Example; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.expressions.ExprCaughtErrors; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.Section; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.lang.TriggerItem; | ||
import ch.njol.skript.lang.parser.ParserInstance; | ||
import ch.njol.skript.registrations.Feature; | ||
import ch.njol.util.Kleenean; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.lang.experiment.ExperimentSet; | ||
import org.skriptlang.skript.lang.experiment.ExperimentalSyntax; | ||
import org.skriptlang.skript.log.runtime.RuntimeError; | ||
import org.skriptlang.skript.log.runtime.RuntimeErrorCatcher; | ||
|
||
import java.util.List; | ||
|
||
@Name("Catch Runtime Errors") | ||
@Description("Catch any runtime errors produced by code within the section. This is an in progress feature.") | ||
@Example(""" | ||
catch runtime errors: | ||
set worldborder center of {_border} to location(0, 0, NaN value) | ||
if last caught runtime errors contains "Your location can't have a NaN value as one of its components": | ||
set worldborder center of {_border} to location(0, 0, 0) | ||
""") | ||
@Since("INSERT VERSION") | ||
public class SecCatchErrors extends Section implements ExperimentalSyntax { | ||
|
||
static { | ||
Skript.registerSection(SecCatchErrors.class, "catch [run[ ]time] error[s]"); | ||
} | ||
|
||
@Override | ||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult, SectionNode sectionNode, List<TriggerItem> triggerItems) { | ||
if (sectionNode.isEmpty()) { | ||
Skript.error("A catch errors section must contain code."); | ||
return false; | ||
} | ||
ParserInstance parser = getParser(); | ||
Kleenean previousDelay = parser.getHasDelayBefore(); | ||
parser.setHasDelayBefore(Kleenean.FALSE); | ||
loadCode(sectionNode); | ||
if (parser.getHasDelayBefore().isTrue()) { | ||
Skript.error("Delays can't be used within a catch errors section."); | ||
return false; | ||
} | ||
parser.setHasDelayBefore(previousDelay); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isSatisfiedBy(ExperimentSet experimentSet) { | ||
return experimentSet.hasExperiment(Feature.CATCH_ERRORS); | ||
} | ||
|
||
@Override | ||
protected @Nullable TriggerItem walk(Event event) { | ||
RuntimeErrorCatcher catcher = new RuntimeErrorCatcher().start(); | ||
last.setNext(null); | ||
TriggerItem.walk(first, event); | ||
ExprCaughtErrors.lastErrors = catcher.getCachedErrors().stream().map(RuntimeError::error).toArray(String[]::new); | ||
catcher.clearCachedErrors() | ||
.clearCachedFrames() | ||
.stop(); | ||
return walk(event, false); | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return "catch runtime errors"; | ||
} | ||
|
||
} |
119 changes: 119 additions & 0 deletions
119
src/main/java/org/skriptlang/skript/log/runtime/RuntimeErrorCatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package org.skriptlang.skript.log.runtime; | ||
|
||
import ch.njol.skript.log.SkriptLogger; | ||
import org.jetbrains.annotations.UnmodifiableView; | ||
import org.skriptlang.skript.log.runtime.Frame.FrameOutput; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map.Entry; | ||
import java.util.logging.Level; | ||
|
||
/** | ||
* A {@link RuntimeErrorConsumer} to be used in {@link RuntimeErrorManager} to catch {@link RuntimeError}s. | ||
* This should always be used with {@link #start()} and {@link #stop()}. | ||
*/ | ||
public class RuntimeErrorCatcher implements RuntimeErrorConsumer { | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private List<RuntimeErrorConsumer> storedConsumers = new ArrayList<>(); | ||
|
||
private final List<RuntimeError> cachedErrors = new ArrayList<>(); | ||
|
||
private final List<Entry<FrameOutput, Level>> cachedFrames = new ArrayList<>(); | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public RuntimeErrorCatcher() {} | ||
|
||
/** | ||
* Gets the {@link RuntimeErrorManager}. | ||
*/ | ||
private RuntimeErrorManager getManager() { | ||
return RuntimeErrorManager.getInstance(); | ||
} | ||
|
||
/** | ||
* Starts this {@link RuntimeErrorCatcher}, removing all {@link RuntimeErrorConsumer}s from {@link RuntimeErrorManager} | ||
* and storing them in {@link #storedConsumers}. | ||
* Makes this {@link RuntimeErrorCatcher} the only {@link RuntimeErrorConsumer} in {@link RuntimeErrorManager} | ||
* to catch {@link RuntimeError}s. | ||
* @return This {@link RuntimeErrorCatcher} | ||
*/ | ||
public RuntimeErrorCatcher start() { | ||
storedConsumers = getManager().removeAllConsumers(); | ||
getManager().addConsumer(this); | ||
return this; | ||
} | ||
|
||
/** | ||
* Stops this {@link RuntimeErrorCatcher}, removing from {@link RuntimeErrorManager} and restoring the previous | ||
* {@link RuntimeErrorConsumer}s from {@link #storedConsumers}. | ||
* Prints all cached {@link RuntimeError}s, {@link #cachedErrors}, and cached {@link FrameOutput}s, {@link #cachedFrames}. | ||
*/ | ||
public void stop() { | ||
if (!getManager().removeConsumer(this)) { | ||
SkriptLogger.LOGGER.severe("[Skript] A 'RuntimeErrorCatcher' was stopped incorrectly."); | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
getManager().addConsumers(storedConsumers.toArray(RuntimeErrorConsumer[]::new)); | ||
for (RuntimeError runtimeError : cachedErrors) | ||
storedConsumers.forEach(consumer -> consumer.printError(runtimeError)); | ||
for (Entry<FrameOutput, Level> entry : cachedFrames) | ||
storedConsumers.forEach(consumer -> consumer.printFrameOutput(entry.getKey(), entry.getValue())); | ||
} | ||
|
||
/** | ||
* Gets all the cached {@link RuntimeError}s. | ||
*/ | ||
public @UnmodifiableView List<RuntimeError> getCachedErrors() { | ||
return Collections.unmodifiableList(cachedErrors); | ||
} | ||
|
||
/** | ||
* Gets all cached {@link FrameOutput}s stored with its corresponding {@link Level} in an {@link Entry} | ||
*/ | ||
public @UnmodifiableView List<Entry<FrameOutput, Level>> getCachedFrames() { | ||
return Collections.unmodifiableList(cachedFrames); | ||
} | ||
|
||
/** | ||
* Clear all cached {@link RuntimeError}s. | ||
*/ | ||
public RuntimeErrorCatcher clearCachedErrors() { | ||
cachedErrors.clear(); | ||
return this; | ||
} | ||
|
||
/** | ||
* Clears all cached {@link FrameOutput}s. | ||
*/ | ||
public RuntimeErrorCatcher clearCachedFrames() { | ||
cachedFrames.clear(); | ||
return this; | ||
} | ||
|
||
@Override | ||
public void printError(RuntimeError error) { | ||
cachedErrors.add(error); | ||
} | ||
|
||
@Override | ||
public void printFrameOutput(FrameOutput output, Level level) { | ||
cachedFrames.add(new Entry<FrameOutput, Level>() { | ||
@Override | ||
public FrameOutput getKey() { | ||
return output; | ||
} | ||
|
||
@Override | ||
public Level getValue() { | ||
return level; | ||
} | ||
|
||
@Override | ||
public Level setValue(Level value) { | ||
return null; | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.