Class ErrorResultException

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
org.machanism.machai.ai.tools.ErrorResultException
All Implemented Interfaces:
Serializable

public class ErrorResultException extends RuntimeException
A runtime exception used to signal a tool or processing error, with a structured error payload that is serialized to JSON and included in the exception message.

This exception is typically thrown by function tools (e.g., @Tool-annotated methods) to report structured error results, such as error codes, descriptions, or additional context, back to the caller (e.g., an AI provider or orchestration layer) in a machine-readable format rather than plain text.

The provided error payload (via the message parameter) is serialized to a JSON string using a default ObjectMapper instance. If serialization fails, the payload is converted to a string using Objects.toString(Object).

Since:
1.3.0
See Also:
  • Field Details

  • Constructor Details

    • ErrorResultException

      public ErrorResultException(Object message)
      Creates a new exception with a message that is the JSON representation of the given object.

      This constructor is used when the error details are provided as an object, which will be serialized to a JSON string using a default ObjectMapper. The resulting JSON string becomes the exception's message.

      Parameters:
      message - the object describing the error result; serialized to JSON and used as this exception's message
    • ErrorResultException

      public ErrorResultException(String message)
      Creates a new exception using the given string directly as the message, without JSON serialization.

      Use this constructor when the error message is already a plain string (e.g., already JSON-formatted, or intentionally plain text) and does not need to be serialized via ObjectMapper.

      Parameters:
      message - the exception message, used as-is
    • ErrorResultException

      public ErrorResultException(Object message, Exception e)
      Creates a new exception with a message that is the JSON representation of the given object, combined with additional error details from another exception.

      This constructor is used when an error occurs during processing and you want to include both the structured error result (serialized to JSON) and the details of the underlying exception.

      Parameters:
      message - the object describing the error result; serialized to JSON and included in the exception message
      e - the underlying exception whose details are included in the exception message
  • Method Details

    • message

      private static String message(Object message, Exception e)
      Constructs a detailed error message by combining the provided message object and exception details.

      If the message is a String, it is used as-is. Otherwise, the method attempts to serialize the message object to a JSON string using a default ObjectMapper. If serialization fails, the message object is converted to a string using Objects.toString(Object).

      If an exception is provided, its details are also appended to the resulting message.

      Parameters:
      message - the object describing the error result; can be a string or any serializable object
      e - the exception whose details are included in the error message (optional)
      Returns:
      a formatted error message containing the serialized error result and exception details