More nullability for errors
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2023-06-29 15:35:15 +02:00
parent 8665f69228
commit 7b6c0fb93f
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 8 additions and 9 deletions

View File

@ -42,7 +42,7 @@ public class LocationalException extends RuntimeException {
}
public PrettyPrintError asPrintable() {
return PrettyPrintError.builder(location)
return (location.source() == null ? PrettyPrintError.builder() : PrettyPrintError.builder(location))
.setMessage(super.getMessage())
.setCallStack(callStack)
.build();

View File

@ -10,10 +10,10 @@ import java.util.*;
* Can be generated from a LocationalException with asPrintable
*
* @param message The error message
* @param start The location of the start of this error. Both it and its components must not be null.
* @param start The location of the start of this error. May be null.
* @param end The location of the end of this error. May be null.
*/
public record PrettyPrintError(String message, Location start, @Nullable Location end, List<? extends StackFrame> callStack) {
public record PrettyPrintError(String message, @Nullable Location start, @Nullable Location end, List<? extends StackFrame> callStack) {
/**
* A location in the source code
*
@ -22,8 +22,8 @@ public record PrettyPrintError(String message, Location start, @Nullable Locatio
* @param row The row of this line, starting with 1
*/
public record Location(String line, int column, int row) {
public static Location create(String source, int index) {
if (index < 0) return new Location("", -1, -1);
public static Location create(@Nullable String source, int index) {
if (source == null || index < 0) return new Location("", -1, -1);
int lineStart = source.lastIndexOf('\n', index);
int lineEnd = source.indexOf('\n', index);
if (lineEnd == -1) lineEnd = source.length();
@ -56,7 +56,7 @@ public record PrettyPrintError(String message, Location start, @Nullable Locatio
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (start.column < 0) sb.append("Error at unknown location: ").append(message);
if (start == null || start.column < 0) sb.append("Error at unknown location: ").append(message);
else {
sb.append("Error at ").append(start).append(": ").append(message).append("\n");
if (end == null) sb.append(start.prettyPrint()).append("Here");
@ -96,12 +96,11 @@ public record PrettyPrintError(String message, Location start, @Nullable Locatio
}
public Builder setLocation(int chStart) {
return setLocation(Location.create(Objects.requireNonNull(source, "source is required to set location"), chStart), null);
return setLocation(Location.create(source, chStart), null);
}
public Builder setLocation(int chStart, int chEnd) {
if (chEnd == chStart) return setLocation(chStart);
Objects.requireNonNull(source, "source is required to set location");
if (chEnd < chStart) {
int a = chEnd;
chEnd = chStart;
@ -120,7 +119,7 @@ public record PrettyPrintError(String message, Location start, @Nullable Locatio
this.callStack = callStack.stream()
.map(frame -> frame instanceof StackFrame.Lined lined
? lined
: ((StackFrame.Raw) frame).lined(Objects.requireNonNull(source, "source is required to set call stack")))
: source == null ? frame : ((StackFrame.Raw) frame).lined(source))
.toList();
return this;
}