Skip to content

Commit f9ce98f

Browse files
psiddhGithub Executorch
andauthored
Summary: Add detailed error message to exception flow (#13929)
First PR to enhance the triaging and debuggability of Android ET related issues Test Plan: Build & CI Reviewers: Subscribers: Tasks: Tags: ### Summary [PLEASE REMOVE] See [CONTRIBUTING.md's Pull Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests) for ExecuTorch PR guidelines. [PLEASE REMOVE] If this PR closes an issue, please add a `Fixes #<issue-id>` line. [PLEASE REMOVE] If this PR introduces a fix or feature that should be the upcoming release notes, please add a "Release notes: <area>" label. For a list of available release notes labels, check out [CONTRIBUTING.md's Pull Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests). ### Test plan [PLEASE REMOVE] How did you test this PR? Please write down any manual commands you used and note down tests that you have written if applicable. Co-authored-by: Github Executorch <[email protected]>
1 parent f6d90ef commit f9ce98f

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

extension/android/executorch_android/src/main/java/org/pytorch/executorch/ExecutorchRuntimeException.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,49 @@ public class ExecutorchRuntimeException extends RuntimeException {
7575
}
7676

7777
static class ErrorHelper {
78+
// Reusable StringBuilder instance
79+
private static final StringBuilder sb = new StringBuilder();
80+
7881
static String formatMessage(int errorCode, String details) {
79-
String baseMessage = ERROR_CODE_MESSAGES.get(errorCode);
80-
if (baseMessage == null) {
81-
baseMessage = "Unknown error code 0x" + Integer.toHexString(errorCode);
82+
synchronized (sb) {
83+
sb.setLength(0); // Clear the StringBuilder before use
84+
85+
String baseMessage = ERROR_CODE_MESSAGES.get(errorCode);
86+
if (baseMessage == null) {
87+
baseMessage = "Unknown error code 0x" + Integer.toHexString(errorCode);
88+
}
89+
90+
sb.append("[Executorch Error 0x")
91+
.append(Integer.toHexString(errorCode))
92+
.append("] ")
93+
.append(baseMessage)
94+
.append(": ")
95+
.append(details)
96+
.append("\nDetailed Logs:\n");
97+
98+
try {
99+
String[] logEntries = readLogBuffer(); // JNI call
100+
formatLogEntries(sb, logEntries);
101+
} catch (Exception e) {
102+
sb.append("Failed to retrieve detailed logs: ").append(e.getMessage());
103+
}
104+
105+
return sb.toString();
106+
}
107+
}
108+
109+
// Native JNI method declaration
110+
private static native String[] readLogBuffer();
111+
112+
// Append log entries to the provided StringBuilder
113+
private static void formatLogEntries(StringBuilder sb, String[] logEntries) {
114+
if (logEntries == null || logEntries.length == 0) {
115+
sb.append("No detailed logs available.");
116+
return;
117+
}
118+
for (String entry : logEntries) {
119+
sb.append(entry).append("\n");
82120
}
83-
return "[Executorch Error 0x"
84-
+ Integer.toHexString(errorCode)
85-
+ "] "
86-
+ baseMessage
87-
+ ": "
88-
+ details;
89121
}
90122
}
91123

0 commit comments

Comments
 (0)