Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,50 +75,32 @@ public class ExecutorchRuntimeException extends RuntimeException {
}

static class ErrorHelper {
private static final boolean ENABLE_READ_LOG_BUFFER_LOGS = true;
// Reusable StringBuilder instance
private static final StringBuilder sb = new StringBuilder();

static String formatMessage(int errorCode, String details) {
synchronized (sb) {
sb.setLength(0); // Clear the StringBuilder before use
String baseMessage = ERROR_CODE_MESSAGES.get(errorCode);
if (baseMessage == null) {
baseMessage = "Unknown error code 0x" + Integer.toHexString(errorCode);
}

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

sb.append("[Executorch Error 0x")
.append(Integer.toHexString(errorCode))
.append("] ")
.append(baseMessage)
.append(": ")
.append(details);
if (ENABLE_READ_LOG_BUFFER_LOGS) {
try {
String[] logEntries = Module.readLogBufferStatic(); // JNI call
if (logEntries != null && logEntries.length > 0) {
sb.append("\n Detailed logs:\n");
}
formatLogEntries(sb, logEntries);
} catch (Exception e) {
sb.append("Failed to retrieve detailed logs: ").append(e.getMessage());
static String getDetailedErrorLogs() {
StringBuilder sb = new StringBuilder();
try {
String[] logEntries = Module.readLogBufferStatic(); // JNI call
if (logEntries != null && logEntries.length > 0) {
sb.append("\nDetailed logs:\n");
for (String entry : logEntries) {
sb.append(entry).append("\n");
}
}

return sb.toString();
}
}

// Append log entries to the provided StringBuilder
private static void formatLogEntries(StringBuilder sb, String[] logEntries) {
if (logEntries == null || logEntries.length == 0) {
sb.append("No detailed logs available.");
return;
}
for (String entry : logEntries) {
sb.append(entry).append("\n");
} catch (Exception e) {
sb.append("Failed to retrieve detailed logs: ").append(e.getMessage());
}
return sb.toString();
}
}

Expand All @@ -133,16 +115,14 @@ public int getErrorCode() {
return errorCode;
}

// Idiomatic Java exception for invalid arguments.
public static class ExecutorchInvalidArgumentException extends IllegalArgumentException {
private final int errorCode = INVALID_ARGUMENT;
public String getDetailedError() {
return ErrorHelper.getDetailedErrorLogs();
}

// Idiomatic Java exception for invalid arguments - extends ExecutorchRuntimeException
public static class ExecutorchInvalidArgumentException extends ExecutorchRuntimeException {
public ExecutorchInvalidArgumentException(String details) {
super(ErrorHelper.formatMessage(INVALID_ARGUMENT, details));
}

public int getErrorCode() {
return errorCode;
super(INVALID_ARGUMENT, details);
}
}

Expand Down
Loading