-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial attempt at embedding the traceback into a ScriptException (#300)
commit-id:d1a78ea5
- Loading branch information
1 parent
89538f8
commit 426ff4c
Showing
4 changed files
with
250 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
93 changes: 93 additions & 0 deletions
93
larky/src/main/java/com/verygood/security/larky/jsr223/LarkyEvaluationScriptException.java
This file contains 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,93 @@ | ||
package com.verygood.security.larky.jsr223; | ||
|
||
import net.starlark.java.eval.EvalException; | ||
import net.starlark.java.eval.Starlark; | ||
import net.starlark.java.eval.StarlarkEvalWrapper; | ||
import net.starlark.java.syntax.Location; | ||
|
||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.script.ScriptException; | ||
|
||
public class LarkyEvaluationScriptException extends ScriptException { | ||
|
||
private final @NotNull String excToString; | ||
|
||
private LarkyEvaluationScriptException(Exception e) { | ||
super(e); // do not construct this exception directly. | ||
excToString = super.getMessage(); | ||
} | ||
|
||
private LarkyEvaluationScriptException(EvalException evalException) { | ||
this(evalException, null, -1, -1); // do not construct this exception directly. | ||
} | ||
|
||
private LarkyEvaluationScriptException(@NotNull EvalException message, String fileName, int lineNumber, int columnNumber) { | ||
super(message.getMessage(), fileName, lineNumber, columnNumber); // do not construct this exception directly. | ||
excToString = super.getMessage() + System.lineSeparator() + message.getMessageWithStack(); | ||
} | ||
|
||
@Contract("_ -> new") | ||
public static @NotNull LarkyEvaluationScriptException of(@NotNull Exception e) { | ||
if (e instanceof StarlarkEvalWrapper.Exc.RuntimeEvalException | ||
|| e instanceof Starlark.UncheckedEvalException) { | ||
return onUnchecked(e); | ||
} else if (e instanceof EvalException) { | ||
return onEvalException((EvalException) e); | ||
} else { | ||
return new LarkyEvaluationScriptException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Both {@link Starlark.UncheckedEvalException} and {@link StarlarkEvalWrapper.Exc.RuntimeEvalException} may not have | ||
* stacktraces as they are derivative of {@link RuntimeException}. | ||
* | ||
* As a result, {@link net.starlark.java.eval.StarlarkThread}'s stacktrace might be buried as the second frame instead | ||
* of the first one. | ||
*/ | ||
@Contract("_ -> new") | ||
private static @NotNull LarkyEvaluationScriptException onUnchecked(final @NotNull Exception e) { | ||
if ((e.getCause() instanceof EvalException)) { | ||
final EvalException cause = (EvalException) e.getCause(); | ||
final LarkyEvaluationScriptException scriptException = onEvalException(cause); | ||
// scriptException.fillInLarkyStackTrace(cause); | ||
return scriptException; | ||
} | ||
return new LarkyEvaluationScriptException(e); | ||
} | ||
|
||
@Contract("_ -> new") | ||
private static @NotNull LarkyEvaluationScriptException onEvalException(final @NotNull EvalException larkyException) { | ||
final LarkyEvaluationScriptException exception; | ||
final Location errorLoc = StarlarkEvalWrapper.Exc.getErrorLocation(larkyException); | ||
if (errorLoc != null) { | ||
exception = new LarkyEvaluationScriptException( | ||
larkyException, | ||
errorLoc.file(), | ||
errorLoc.line(), | ||
errorLoc.column() | ||
); | ||
} else { | ||
exception = new LarkyEvaluationScriptException(larkyException); | ||
} | ||
return exception; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return excToString; | ||
} | ||
|
||
/** | ||
* Helper method that helps fill in the stack trace from an external {@link EvalException}. This will just invoke | ||
* {@link StarlarkEvalWrapper.Exc#fillInLarkyStackTrace(EvalException, Throwable)} with the current exception. | ||
* | ||
* @param larkyException - The {@link EvalException} that contains the Larky stacktrace | ||
*/ | ||
public void fillInLarkyStackTrace(@NotNull EvalException larkyException) { | ||
StarlarkEvalWrapper.Exc.fillInLarkyStackTrace(larkyException, this); | ||
} | ||
|
||
} |
This file contains 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 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