-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The new parameter makes it possiblt to fail in a way that is more appropriate for user errors: No Starlark stack trace is prepended to the failure, instead the provided causes and their locations are appended to the error message.
- Loading branch information
Showing
4 changed files
with
79 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package net.starlark.java.eval; | ||
|
||
import net.starlark.java.annot.StarlarkBuiltin; | ||
import net.starlark.java.syntax.Location; | ||
|
||
/** | ||
* A {@link StarlarkValue} that can be used as a cause in calls to {@code link}. | ||
* | ||
* <p>Implementations should override {@link StarlarkValue#debugPrint(Printer, StarlarkSemantics)} | ||
* and ensure that, combined with {@link FailureCause#getLocation()}, this provides sufficient | ||
* information for users to understand the cause of a failure without a stack trace. | ||
*/ | ||
@StarlarkBuiltin(name = "failure_cause", | ||
doc = "Can be passed to the <code>cause</code> parameter of " | ||
+ "<a href=\"globals.html#fail\"><code>fail</code></a> to add context to an error message.") | ||
public interface FailureCause extends StarlarkValue { | ||
|
||
Location getLocation(); | ||
|
||
default void appendCauseTo(Printer printer, StarlarkSemantics semantics) { | ||
printer.append(Starlark.type(this)); | ||
Location location = getLocation(); | ||
printer.append(" in file \""); | ||
printer.append(location.file()); | ||
printer.append('\"'); | ||
if (location.line() != 0) { | ||
printer.append(", line "); | ||
printer.append(location.line()); | ||
if (location.column() != 0) { | ||
printer.append(", column "); | ||
printer.append(location.column()); | ||
} | ||
} | ||
printer.append(", with value:\n"); | ||
try (SilentCloseable ignored = printer.indent()) { | ||
debugPrint(printer, semantics); | ||
} | ||
} | ||
} |
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