handled better errors and stack traces for more (all) cases

This commit is contained in:
gnembon committed 2020-01-27 22:47:29 -04:00
1 parent 6c1d3dd5f6
commit 9430fcc052
6 files changed
+88 -25

No files matched your search

@@ -276,16 +276,19 @@ public class ScriptCommand
} }
private static CarpetScriptHost getHost(CommandContext<ServerCommandSource> context) private static CarpetScriptHost getHost(CommandContext<ServerCommandSource> context)
{ {
CarpetScriptHost host;
try try
{ {
String name = StringArgumentType.getString(context, "app").toLowerCase(Locale.ROOT); String name = StringArgumentType.getString(context, "app").toLowerCase(Locale.ROOT);
CarpetScriptHost parentHost = CarpetServer.scriptServer.modules.getOrDefault(name, CarpetServer.scriptServer.globalHost); CarpetScriptHost parentHost = CarpetServer.scriptServer.modules.getOrDefault(name, CarpetServer.scriptServer.globalHost);
return parentHost.retrieveForExecution(context.getSource()); host = parentHost.retrieveForExecution(context.getSource());
} }
catch (IllegalArgumentException ignored) catch (IllegalArgumentException ignored)
{ {
return CarpetServer.scriptServer.globalHost; host = CarpetServer.scriptServer.globalHost;
} }
host.setChatErrorSnooper(context.getSource());
return host;
} }
private static Collection<String> suggestFunctionCalls(CommandContext<ServerCommandSource> c) private static Collection<String> suggestFunctionCalls(CommandContext<ServerCommandSource> c)
{ {
@@ -324,7 +327,6 @@ public class ScriptCommand
Messenger.m(source, "gi ----------------"); Messenger.m(source, "gi ----------------");
return; return;
} }
CarpetSettings.LOG.error("printing function "+fun.getPrettyString());
Expression expr = fun.getExpression(); Expression expr = fun.getExpression();
Tokenizer.Token tok = fun.getToken(); Tokenizer.Token tok = fun.getToken();
List<String> snippet = ExpressionInspector.Expression_getExpressionSnippet(tok, expr); List<String> snippet = ExpressionInspector.Expression_getExpressionSnippet(tok, expr);
@@ -375,10 +377,9 @@ public class ScriptCommand
} }
catch (CarpetExpressionException e) catch (CarpetExpressionException e)
{ {
e.printStack(source); host.handleErrorWithStack("Error while evaluating expression", e);
Messenger.m(source, "r Exception while evaluating expression at "+new BlockPos(source.getPosition())+": "+e.getMessage());
} }
host.resetErrorSnooper(); //host.resetErrorSnooper(); // lets say no need to reset the snooper in case something happens on the way
} }
private static int invoke(CommandContext<ServerCommandSource> context, String call, BlockPos pos1, BlockPos pos2, String args) private static int invoke(CommandContext<ServerCommandSource> context, String call, BlockPos pos1, BlockPos pos2, String args)
@@ -457,8 +458,7 @@ public class ScriptCommand
} }
catch (CarpetExpressionException exc) catch (CarpetExpressionException exc)
{ {
exc.printStack(source); host.handleErrorWithStack("Error while processing command", exc);
Messenger.m(source, "r Error while processing command: "+exc);
return 0; return 0;
} }
finally finally
@@ -505,8 +505,7 @@ public class ScriptCommand
} }
catch (CarpetExpressionException e) catch (CarpetExpressionException e)
{ {
e.printStack(source); host.handleErrorWithStack("Exception while filling the area", e);
Messenger.m(source, "r Exception while filling the area:\n","l "+e.getMessage());
return 0; return 0;
} }
catch (ArithmeticException e) catch (ArithmeticException e)
@@ -32,6 +32,7 @@ import static java.lang.Math.max;
public class CarpetScriptHost extends ScriptHost public class CarpetScriptHost extends ScriptHost
{ {
private final CarpetScriptServer scriptServer; private final CarpetScriptServer scriptServer;
ServerCommandSource responsibleSource;
private Tag globalState; private Tag globalState;
private int saveTimeout; private int saveTimeout;
@@ -66,8 +67,7 @@ public class CarpetScriptHost extends ScriptHost
} }
catch (CarpetExpressionException e) catch (CarpetExpressionException e)
{ {
e.printStack(source); host.handleErrorWithStack("Error while evaluating expression", e);
Messenger.m(source, "r Exception while evaluating expression at " + new BlockPos(source.getPosition()) + ": " + e.getMessage());
host.resetErrorSnooper(); host.resetErrorSnooper();
return null; return null;
} }
@@ -129,16 +129,33 @@ public class CarpetScriptHost extends ScriptHost
public CarpetScriptHost retrieveForExecution(ServerCommandSource source) public CarpetScriptHost retrieveForExecution(ServerCommandSource source)
{ {
if (!perUser) CarpetScriptHost host = this;
return this; if (perUser)
{
try
{
ServerPlayerEntity player = source.getPlayer();
host = (CarpetScriptHost) retrieveForExecution(player.getName().getString());
}
catch (CommandSyntaxException e)
{
host = (CarpetScriptHost) retrieveForExecution((String) null);
}
}
if (host.errorSnooper == null) host.setChatErrorSnooper(source);
return host;
}
public String handleCommand(ServerCommandSource source, String call, List<Integer> coords, String arg)
{
try try
{ {
ServerPlayerEntity player = source.getPlayer(); return call(source, call, coords, arg);
return (CarpetScriptHost) retrieveForExecution(player.getName().getString());
} }
catch (CommandSyntaxException e) catch (CarpetExpressionException exc)
{ {
return (CarpetScriptHost) retrieveForExecution((String)null); handleErrorWithStack("Error while running custom command", exc);
return "";
} }
} }
@@ -236,7 +253,7 @@ public class CarpetScriptHost extends ScriptHost
} }
catch (ExpressionException e) catch (ExpressionException e)
{ {
return e.getMessage(); throw new CarpetExpressionException(e.getMessage(), e.stack);
} }
} }
@@ -259,7 +276,7 @@ public class CarpetScriptHost extends ScriptHost
} }
catch (ExpressionException e) catch (ExpressionException e)
{ {
CarpetSettings.LOG.error("Callback failed: "+e.getMessage()); handleExpressionException("Callback failed", e);
} }
return Value.NULL; return Value.NULL;
} }
@@ -361,6 +378,7 @@ public class CarpetScriptHost extends ScriptHost
public void setChatErrorSnooper(ServerCommandSource source) public void setChatErrorSnooper(ServerCommandSource source)
{ {
responsibleSource = source;
errorSnooper = (expr, /*Nullable*/ token, message) -> errorSnooper = (expr, /*Nullable*/ token, message) ->
{ {
try try
@@ -412,4 +430,31 @@ public class CarpetScriptHost extends ScriptHost
return new ArrayList<>(); return new ArrayList<>();
}; };
} }
@Override
public void resetErrorSnooper()
{
responsibleSource = null;
super.resetErrorSnooper();
}
public void handleErrorWithStack(String intro, CarpetExpressionException exception)
{
if (responsibleSource != null)
{
exception.printStack(responsibleSource);
String message = exception.getMessage();
Messenger.m(responsibleSource, "r "+intro+(message.isEmpty()?"":": "+message));
}
else
{
CarpetSettings.LOG.error(intro+": "+exception.getMessage());
}
}
@Override
public void handleExpressionException(String message, ExpressionException exc)
{
handleErrorWithStack(message, new CarpetExpressionException(exc.getMessage(), exc.stack));
}
} }
@@ -220,7 +220,7 @@ public class CarpetScriptServer
executes( (c) -> executes( (c) ->
{ {
String response = modules.get(hostName).retrieveForExecution(c.getSource()). String response = modules.get(hostName).retrieveForExecution(c.getSource()).
call(c.getSource(),"__command", null, ""); handleCommand(c.getSource(),"__command", null, "");
if (!response.isEmpty()) Messenger.m(c.getSource(), "gi "+response); if (!response.isEmpty()) Messenger.m(c.getSource(), "gi "+response);
return 1; return 1;
}); });
@@ -232,14 +232,14 @@ public class CarpetScriptServer
requires((player) -> modules.containsKey(hostName) && modules.get(hostName).getFunction(function) != null). requires((player) -> modules.containsKey(hostName) && modules.get(hostName).getFunction(function) != null).
executes( (c) -> { executes( (c) -> {
String response = modules.get(hostName).retrieveForExecution(c.getSource()). String response = modules.get(hostName).retrieveForExecution(c.getSource()).
call(c.getSource(), function,null,""); handleCommand(c.getSource(), function,null,"");
if (!response.isEmpty()) Messenger.m(c.getSource(),"gi "+response); if (!response.isEmpty()) Messenger.m(c.getSource(),"gi "+response);
return 1; return 1;
}). }).
then(argument("args...", StringArgumentType.greedyString()). then(argument("args...", StringArgumentType.greedyString()).
executes( (c) -> { executes( (c) -> {
String response = modules.get(hostName).retrieveForExecution(c.getSource()). String response = modules.get(hostName).retrieveForExecution(c.getSource()).
call(c.getSource(), function,null, StringArgumentType.getString(c, "args...")); handleCommand(c.getSource(), function,null, StringArgumentType.getString(c, "args..."));
if (!response.isEmpty()) Messenger.m(c.getSource(), "gi "+response); if (!response.isEmpty()) Messenger.m(c.getSource(), "gi "+response);
return 1; return 1;
}))); })));
@@ -1,6 +1,7 @@
package carpet.script; package carpet.script;
import carpet.script.bundled.Module; import carpet.script.bundled.Module;
import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import carpet.script.value.FunctionValue; import carpet.script.value.FunctionValue;
import carpet.script.value.Value; import carpet.script.value.Value;
@@ -313,6 +314,12 @@ public abstract class ScriptHost
return userHost; return userHost;
} }
public void handleExpressionException(String msg, ExpressionException exc)
{
System.out.println(msg+": "+exc);
}
protected abstract ScriptHost duplicate(); protected abstract ScriptHost duplicate();
public void onClose() { } public void onClose() { }
@@ -68,7 +68,7 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
return (name.equals("_")?"<lambda>":name) +"("+String.join(", ",stringArgs)+")"; return (name.equals("_")?"<lambda>":name) +"("+String.join(", ",stringArgs)+")";
} }
public String fullName() {return expression.module == null?name:name+"["+expression.module.getName()+"]";} public String fullName() {return (name.equals("_")?"<lambda>":name)+(expression.module == null?"":"["+expression.module.getName()+"]");}
@Override @Override
public boolean getBoolean() public boolean getBoolean()
@@ -4,6 +4,7 @@ import carpet.script.Context;
import carpet.script.Expression; import carpet.script.Expression;
import carpet.script.LazyValue; import carpet.script.LazyValue;
import carpet.script.Tokenizer; import carpet.script.Tokenizer;
import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import java.util.HashMap; import java.util.HashMap;
@@ -29,7 +30,18 @@ public class ThreadValue extends Value
{ {
id = sequence++; id = sequence++;
taskFuture = CompletableFuture.supplyAsync( taskFuture = CompletableFuture.supplyAsync(
() -> function.lazyEval(ctx, Context.NONE, expr, token, args).evalValue(ctx), () ->
{
try
{
return function.lazyEval(ctx, Context.NONE, expr, token, args).evalValue(ctx);
}
catch (ExpressionException exc)
{
ctx.host.handleExpressionException("Thread failed", exc);
return Value.NULL;
}
},
executorServices.computeIfAbsent(pool, (v) -> (ThreadPoolExecutor)Executors.newCachedThreadPool()) executorServices.computeIfAbsent(pool, (v) -> (ThreadPoolExecutor)Executors.newCachedThreadPool())
); );
Thread.yield(); Thread.yield();