Skip to content

Commit 3954da9

Browse files
psiddhGithub Executorch
andauthored
Minor Refactor of ExecutorchRuntimeException (#16193)
- Add dedicated helper function to retreive detailed error logs - Make ExecutorchInvalidArgumentException extend ExecutorchRuntimeException ### 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 <github_executorch@arm.com>
1 parent 5033840 commit 3954da9

File tree

1 file changed

+26
-46
lines changed

1 file changed

+26
-46
lines changed

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

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -75,50 +75,32 @@ public class ExecutorchRuntimeException extends RuntimeException {
7575
}
7676

7777
static class ErrorHelper {
78-
private static final boolean ENABLE_READ_LOG_BUFFER_LOGS = true;
79-
// Reusable StringBuilder instance
80-
private static final StringBuilder sb = new StringBuilder();
81-
8278
static String formatMessage(int errorCode, String details) {
83-
synchronized (sb) {
84-
sb.setLength(0); // Clear the StringBuilder before use
79+
String baseMessage = ERROR_CODE_MESSAGES.get(errorCode);
80+
if (baseMessage == null) {
81+
baseMessage = "Unknown error code 0x" + Integer.toHexString(errorCode);
82+
}
8583

86-
String baseMessage = ERROR_CODE_MESSAGES.get(errorCode);
87-
if (baseMessage == null) {
88-
baseMessage = "Unknown error code 0x" + Integer.toHexString(errorCode);
89-
}
84+
String safeDetails = details != null ? details : "No details provided";
85+
return String.format(
86+
"[Executorch Error 0x%s] %s: %s",
87+
Integer.toHexString(errorCode), baseMessage, safeDetails);
88+
}
9089

91-
sb.append("[Executorch Error 0x")
92-
.append(Integer.toHexString(errorCode))
93-
.append("] ")
94-
.append(baseMessage)
95-
.append(": ")
96-
.append(details);
97-
if (ENABLE_READ_LOG_BUFFER_LOGS) {
98-
try {
99-
String[] logEntries = Module.readLogBufferStatic(); // JNI call
100-
if (logEntries != null && logEntries.length > 0) {
101-
sb.append("\n Detailed logs:\n");
102-
}
103-
formatLogEntries(sb, logEntries);
104-
} catch (Exception e) {
105-
sb.append("Failed to retrieve detailed logs: ").append(e.getMessage());
90+
static String getDetailedErrorLogs() {
91+
StringBuilder sb = new StringBuilder();
92+
try {
93+
String[] logEntries = Module.readLogBufferStatic(); // JNI call
94+
if (logEntries != null && logEntries.length > 0) {
95+
sb.append("\nDetailed logs:\n");
96+
for (String entry : logEntries) {
97+
sb.append(entry).append("\n");
10698
}
10799
}
108-
109-
return sb.toString();
110-
}
111-
}
112-
113-
// Append log entries to the provided StringBuilder
114-
private static void formatLogEntries(StringBuilder sb, String[] logEntries) {
115-
if (logEntries == null || logEntries.length == 0) {
116-
sb.append("No detailed logs available.");
117-
return;
118-
}
119-
for (String entry : logEntries) {
120-
sb.append(entry).append("\n");
100+
} catch (Exception e) {
101+
sb.append("Failed to retrieve detailed logs: ").append(e.getMessage());
121102
}
103+
return sb.toString();
122104
}
123105
}
124106

@@ -133,16 +115,14 @@ public int getErrorCode() {
133115
return errorCode;
134116
}
135117

136-
// Idiomatic Java exception for invalid arguments.
137-
public static class ExecutorchInvalidArgumentException extends IllegalArgumentException {
138-
private final int errorCode = INVALID_ARGUMENT;
118+
public String getDetailedError() {
119+
return ErrorHelper.getDetailedErrorLogs();
120+
}
139121

122+
// Idiomatic Java exception for invalid arguments - extends ExecutorchRuntimeException
123+
public static class ExecutorchInvalidArgumentException extends ExecutorchRuntimeException {
140124
public ExecutorchInvalidArgumentException(String details) {
141-
super(ErrorHelper.formatMessage(INVALID_ARGUMENT, details));
142-
}
143-
144-
public int getErrorCode() {
145-
return errorCode;
125+
super(INVALID_ARGUMENT, details);
146126
}
147127
}
148128

0 commit comments

Comments
 (0)