Class PanamaErrorMapper

java.lang.Object
ai.tegmentum.wasmtime4j.panama.util.PanamaErrorMapper

public final class PanamaErrorMapper extends Object
Utility class for mapping native error codes to appropriate Java exceptions in Panama.

This class translates native error codes (as defined by WasmErrorCode) into the appropriate exception types from the public API exception hierarchy. It ensures that Panama callers produce meaningful, categorized exceptions instead of raw error code integers.

  • Method Details

    • retrieveNativeErrorMessage

      public static String retrieveNativeErrorMessage()
      Retrieves the last error message from the native library and clears it.
      Returns:
      the error message, or null if no error
    • mapNativeError

      public static ai.tegmentum.wasmtime4j.exception.WasmException mapNativeError(int errorCode, String context)
      Maps a native error code and context message to an appropriate Java exception.

      Delegates to the shared ErrorMapper for consistent error categorization across both JNI and Panama implementations.

      Parameters:
      errorCode - the native error code
      context - a description of the operation that failed
      Returns:
      the appropriate Java exception
    • mapNativeError

      public static ai.tegmentum.wasmtime4j.exception.WasmException mapNativeError(int errorCode)
      Maps a native error code to an appropriate Java exception with no additional context.
      Parameters:
      errorCode - the native error code
      Returns:
      the appropriate Java exception
    • writeErrorMessage

      public static void writeErrorMessage(MemorySegment errorMsgPtr, int errorMsgLen, String message)
      Writes an error message to a native error buffer.

      Truncates the message if it exceeds the buffer size, and always null-terminates.

      Parameters:
      errorMsgPtr - pointer to the error message buffer
      errorMsgLen - size of the error message buffer
      message - the error message to write
    • getErrorDescription

      public static String getErrorDescription(int errorCode)
      Returns a human-readable description for a native error code.
      Parameters:
      errorCode - the native error code
      Returns:
      the error description, or a default message for unknown codes