diff --git a/extension/android/executorch_android/src/main/java/org/pytorch/executorch/ExecutorchRuntimeException.java b/extension/android/executorch_android/src/main/java/org/pytorch/executorch/ExecutorchRuntimeException.java index b7cce18f063..102b96ab686 100644 --- a/extension/android/executorch_android/src/main/java/org/pytorch/executorch/ExecutorchRuntimeException.java +++ b/extension/android/executorch_android/src/main/java/org/pytorch/executorch/ExecutorchRuntimeException.java @@ -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(); } } @@ -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); } }