Skip to content

Commit

Permalink
Make panic messages short (#1528)
Browse files Browse the repository at this point in the history
  • Loading branch information
kustosz authored Mar 1, 2021
1 parent fea8856 commit 3a88be5
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,6 @@ class ModuleManagementTest extends AnyFlatSpec with Matchers {
)
val exception =
the[PolyglotException] thrownBy mod2.getAssociatedConstructor
exception.getMessage shouldEqual "org.enso.interpreter.runtime.error.PanicException: Module_Does_Not_Exist Test.Main"
exception.getMessage shouldEqual "Module_Does_Not_Exist"
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.enso.interpreter.runtime.error;

import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.exception.AbstractTruffleException;
import com.oracle.truffle.api.interop.ExceptionType;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.*;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.SourceSection;
import org.enso.interpreter.node.expression.builtin.text.util.TypeToDisplayTextNode;
import org.enso.interpreter.node.expression.builtin.text.util.TypeToDisplayTextNodeGen;
import org.enso.interpreter.runtime.data.text.Text;

/** An exception type for user thrown panic exceptions. */
@ExportLibrary(value = InteropLibrary.class, delegateTo = "payload")
Expand Down Expand Up @@ -36,7 +39,17 @@ public Object getPayload() {

@Override
public String getMessage() {
return payload.toString();
InteropLibrary library = InteropLibrary.getUncached();
try {
return library.asString(library.getExceptionMessage(this));
} catch (UnsupportedMessageException e) {
return TypeToDisplayTextNodeGen.getUncached().execute(payload);
}
}

@Override
public String toString() {
return getMessage();
}

@ExportMessage
Expand All @@ -49,6 +62,26 @@ RuntimeException throwException() {
throw this;
}

@ExportMessage
boolean hasExceptionMessage() {
return true;
}

@ExportMessage
Object getExceptionMessage(
@CachedLibrary(limit = "5") InteropLibrary payloads,
@CachedLibrary(limit = "3") InteropLibrary strings,
@Cached TypeToDisplayTextNode typeToDisplayTextNode) {
try {
return Text.create(strings.asString(payloads.invokeMember(payload, "to_display_text")));
} catch (UnsupportedTypeException
| UnsupportedMessageException
| ArityException
| UnknownIdentifierException e) {
return Text.create(typeToDisplayTextNode.execute(payload));
}
}

@ExportMessage
ExceptionType getExceptionType() {
return ExceptionType.RUNTIME_ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class ReplTest extends InterpreterTest with BeforeAndAfter with EitherValues {
}
eval(code)
val errorMsg =
"Compile_Error Variable `undefined` is not defined."
"Compile error: Variable `undefined` is not defined."
evalResult.left.value.getMessage shouldEqual errorMsg
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3169,7 +3169,7 @@ class RuntimeServerTest
contextId,
Seq(
Api.ExecutionResult.Diagnostic.error(
"Not_Invokable_Error 42",
"Type error: expected a function, but got 42 (Integer).",
Some(mainFile),
Some(model.Range(model.Position(0, 7), model.Position(0, 24))),
None,
Expand Down Expand Up @@ -3237,7 +3237,7 @@ class RuntimeServerTest
contextId,
Seq(
Api.ExecutionResult.Diagnostic.error(
"No_Such_Method_Error UnresolvedSymbol<x> UnresolvedSymbol<+>",
"Method `+` of x (Unresolved_Symbol) could not be found.",
Some(mainFile),
Some(model.Range(model.Position(2, 14), model.Position(2, 23))),
None,
Expand Down Expand Up @@ -3313,7 +3313,7 @@ class RuntimeServerTest
contextId,
Seq(
Api.ExecutionResult.Diagnostic.error(
"Type_Error Text 2 str",
"Type error: expected `str` to be Text, but got 2 (Integer).",
None,
None,
None,
Expand Down Expand Up @@ -3391,7 +3391,7 @@ class RuntimeServerTest
contextId,
Seq(
Api.ExecutionResult.Diagnostic.error(
"No_Such_Method_Error Number UnresolvedSymbol<pi>",
"Method `pi` of Number could not be found.",
Some(mainFile),
Some(model.Range(model.Position(2, 7), model.Position(2, 16))),
None,
Expand Down Expand Up @@ -3470,7 +3470,7 @@ class RuntimeServerTest
contextId,
Seq(
Api.ExecutionResult.Diagnostic.error(
"Type_Error Number UnresolvedSymbol<quux> that",
"Type error: expected `that` to be Number, but got quux (Unresolved_Symbol).",
None,
None,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CaseTest extends InterpreterTest {
|""".stripMargin

val msg =
"Compile_Error Cons2 is not visible in this scope, Nil2 is not visible in this scope"
"Compile error: Cons2 is not visible in this scope, Nil2 is not visible in this scope"
the[InterpreterException] thrownBy eval(code) should have message msg
}

Expand All @@ -40,7 +40,8 @@ class CaseTest extends InterpreterTest {
| Cons a -> a
|""".stripMargin

val msg = "Compile_Error Cannot match on Cons using 1 field (expecting 2)"
val msg =
"Compile error: Cannot match on Cons using 1 field (expecting 2)"
the[InterpreterException] thrownBy eval(code) should have message msg
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ConstructorsTest extends InterpreterTest {
| Cons h t -> 0
""".stripMargin
the[InterpreterException] thrownBy eval(testCode)
.call() should have message "Inexhaustive_Pattern_Match_Error Nil"
.call() should have message "Inexhaustive pattern match: no branch matches Nil."
}

"be usable in code, with arbitrary definition order" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ImportsTest extends PackageTest {
"Overloaded methods" should "not be visible when not imported" in {
the[InterpreterException] thrownBy evalTestProject(
"TestNonImportedOverloads"
) should have message "No_Such_Method_Error (X 1) UnresolvedSymbol<method>"
) should have message "Method `method` of X could not be found."
}

"Symbols from imported modules" should "not be visible when imported qualified" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class MethodsTest extends InterpreterTest {
|""".stripMargin
the[InterpreterException] thrownBy eval(
code
) should have message "No_Such_Method_Error 7 UnresolvedSymbol<foo>"
) should have message "Method `foo` of 7 (Integer) could not be found."
}

"be callable for any type when defined on Any" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class PatternMatchTest extends InterpreterTest {
| f MyAtom
|""".stripMargin

val msg = "Inexhaustive_Pattern_Match_Error MyAtom"
val msg = "Inexhaustive pattern match: no branch matches MyAtom."
the[InterpreterException] thrownBy eval(code) should have message msg
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SystemProcessTest extends InterpreterTest with OsSpec {
|""".stripMargin

val error = the[InterpreterException] thrownBy eval(code)
error.getMessage should include("nonexistentcommandxyz")
error.getMessage should include("a polyglot object")
consumeOut shouldEqual List()
consumeErr shouldEqual List()
}
Expand Down

0 comments on commit 3a88be5

Please sign in to comment.