Skip to content

Commit bb6de0d

Browse files
committed
Introduce new exception format specifier '%m'.
1 parent 3a3602c commit bb6de0d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/formatting/ErrorMessageFormatter.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
4848
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
49+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4950

5051
/**
5152
* Custom formatter adding Python-specific conversions often required in error messages.
@@ -94,6 +95,14 @@ public String format(GetLazyClassNode getClassNode, String format, Object... arg
9495
offset += name.length() - (m.end() - m.start());
9596
args[matchIdx] = REMOVED_MARKER;
9697
removedCnt++;
98+
} else if ("%m".equals(group) && args[matchIdx] instanceof Throwable) {
99+
// If the format arg is not a Throwable, 'String.format' will do the error handling
100+
// and throw an IllegalFormatException for us.
101+
String exceptionMessage = getMessage((Throwable) args[matchIdx]);
102+
sb.replace(m.start() + offset, m.end() + offset, exceptionMessage);
103+
offset += exceptionMessage.length() - (m.end() - m.start());
104+
args[matchIdx] = REMOVED_MARKER;
105+
removedCnt++;
97106
}
98107

99108
idx = m.end();
@@ -105,6 +114,11 @@ public String format(GetLazyClassNode getClassNode, String format, Object... arg
105114
return String.format(sb.toString(), compact(args, removedCnt));
106115
}
107116

117+
@TruffleBoundary
118+
private static String getMessage(Throwable exception) {
119+
return exception.getMessage();
120+
}
121+
108122
private static String getClassName(GetLazyClassNode getClassNode, Object obj) {
109123
if (getClassNode != null) {
110124
return GetNameNode.doSlowPath(getClassNode.execute(obj));

0 commit comments

Comments
 (0)