Skip to content

Commit

Permalink
GH-548 Format Failures
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Aug 19, 2020
1 parent 6d5c44c commit de4613b
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 79 deletions.
2 changes: 1 addition & 1 deletion examples/tests/current_test.panda
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ main {
CustomThread thread = new CustomThread()
thread.start()

// Runnable runnable = { log 'x' }
Runnable runnable = { log 'x' }
}

// simple interface
Expand Down
24 changes: 24 additions & 0 deletions panda-framework/src/main/java/org/panda_lang/language/Failure.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.panda_lang.language;

import org.jetbrains.annotations.Nullable;
import org.panda_lang.language.interpreter.source.IndicatedSource;
import org.panda_lang.utilities.commons.function.Option;

Expand All @@ -38,4 +39,27 @@ public interface Failure {
*/
Option<String> getNote();

/**
* Returns the cause of this throwable or {@code null} if the
* cause is nonexistent or unknown. (The cause is the throwable that
* caused this throwable to get thrown.)
*
* @return the cause
*/
@Nullable Throwable getCause();

/**
* Get failure message
*
* @return the failure message
*/
String getMessage();

/**
* Get failure stacktrace
*
* @return the array of stack trace elements
*/
StackTraceElement[] getStackTrace();

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
package org.panda_lang.language.interpreter.logging;

import org.panda_lang.language.Failure;
import org.panda_lang.language.PandaFrameworkConstants;
import org.panda_lang.language.interpreter.source.IndicatedSource;
import org.panda_lang.language.interpreter.source.Location;
import org.panda_lang.language.interpreter.token.Snippet;
import org.panda_lang.language.interpreter.token.Snippetable;
import org.panda_lang.utilities.commons.StackTraceUtils;
import org.panda_lang.utilities.commons.StringUtils;
import org.panda_lang.utilities.commons.console.Colored;
import org.panda_lang.utilities.commons.console.Effect;

public final class SystemLogger implements Logger {
Expand Down Expand Up @@ -49,26 +56,72 @@ public void exception(Throwable throwable) {
StackTraceElement[] stackTrace = StackTraceUtils.startsWith(throwable.getStackTrace(), element -> element.toString().contains("org.junit"));

if (throwable instanceof Failure) {
Failure failure = (Failure) throwable;

}
error("");
error("&b- - ~ ~< Failure >~ ~ - -&r");
error("");
error("&1" + failure.getMessage());
error("");

error("");
error("&b- - ~ ~< Exception >~ ~ - -&r");
error("");
error("Given:");
error(" Message:&1 " + throwable.getMessage());
error(" In:&1 " + stackTrace[0].toString());
error(" By:&1 " + throwable.getClass());
error("");
error("Stacktrace:");
failure.getNote().peek(note -> {
error("Note:");
error(" &1" + note);
error("");
});

IndicatedSource indicatedSource = failure.getIndicatedSource();
Location location = indicatedSource.getIndicated().getLocation();

String source = getCurrentLine(indicatedSource.getSource(), indicatedSource.getIndicated()).toString();
String element = getCurrentLine(indicatedSource.getIndicated(), indicatedSource.getIndicated()).toString();

int elementIndex = source.indexOf(element);
int endIndex = elementIndex + element.length();

for (StackTraceElement element : stackTrace) {
error(" at " + element.toString());
String content = elementIndex < 0 ? source : source.substring(0, elementIndex)
+ Colored.on(source.substring(elementIndex, endIndex)).effect(Effect.RED)
+ source.substring(endIndex);


error("Source:");
error(" " + content);
error(" " + StringUtils.buildSpace(source.indexOf(element)) + Colored.on("^").effect(Effect.BOLD));
error("Location:");
error(" Panda: &b" + location.getSource().getId() + "&r at line &1" + location.getDisplayLine() + "&r:&1" + location.getIndex());
error("");
error("Stacktrace:");

for (int index = 0; index < 2 && index < stackTrace.length; index++) {
StackTraceElement stackTraceElement = stackTrace[index];
error(" at " + stackTraceElement.toString());
}
}
else {
error("");
error("&b- - ~ ~< Exception >~ ~ - -&r");
error("");
error("Given:");
error(" Message:&1 " + throwable.getMessage());
error(" In:&1 " + stackTrace[0].toString());
error(" By:&1 " + throwable.getClass());
error("");
error("Stacktrace:");

for (StackTraceElement stackTraceElement : stackTrace) {
error(" at " + stackTraceElement);
}
}

error("");
error("Environment:");
error(" Panda: " + PandaFrameworkConstants.VERSION);
error(" Java: " + System.getProperty("java.version") + " (" + System.getProperty("os.name") + ")");
error("");
}

private Snippet getCurrentLine(Snippetable source, Snippetable indicated) {
return source.toSnippet().getLine(indicated.toSnippet().getFirst().getLocation().getLine());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ public ExpressionTransaction parse(Context context, Streamable streamable, Expre
// if something went wrong
if (worker.hasError()) {
transaction.rollback();
throw new PandaParserFailure(expressionContext, worker.getError().getErrorSource(), worker.getError().getErrorMessage());
throw new PandaParserFailure(
expressionContext, worker.getError().getErrorSource(),
worker.getError().getErrorMessage(),
"マルセルへの注意 \uD83E\uDD20"
);
}

// if context does not contain any results
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public boolean isMutable() {
@Override
public String toString() {
return ContentJoiner.on(" ")
.join(tokens, representation -> representation.getToken().toString())
.join(tokens, Object::toString)
.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public int hashCode() {

@Override
public String toString() {
return token.getValue();
return token.toString();
}

public static TokenInfo of(TokenType type, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,17 @@ default Snippet getLine(int line) {

for (TokenInfo tokenInfo : getTokensRepresentations()) {
if (tokenInfo.getType() == TokenTypes.SECTION) {
selected.addAll(tokenInfo.toToken(Section.class).getContent().getLine(line).getTokensRepresentations());
continue;
Snippet content = tokenInfo.toToken(Section.class).getContent();
Snippet selectedContent = content.getLine(line);

if (!selectedContent.isEmpty()) {
if (!content.equals(selectedContent)) {
return selectedContent;
}

selected.add(tokenInfo);
continue;
}
}

if (tokenInfo.getLocation().getLine() < line) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Section(TokenInfo openingSeparator, Snippet content, TokenInfo closingSep

@Override
public String getValue() {
return content.toString();
return getOpeningSeparator().toString() + getClosingSeparator().toString();
}

@Override
Expand All @@ -63,7 +63,7 @@ public Separator getSeparator() {

@Override
public String toString() {
return getSeparator().toString() + " " + super.toString() + " " + getSeparator().getOpposite().toString();
return getOpeningSeparator().toString() + " " + getContent() + " " + getClosingSeparator().toString();
}

}

0 comments on commit de4613b

Please sign in to comment.