Skip to content

exceptions

Class info

Classes

Name Children Inherits
InvalidResponseError
aclientp.exceptions
The peer answered a request with a result that does not match the method's schema.
    ProtocolVersionError
    aclientp.exceptions
    The peer answered ``initialize`` with a protocol version this side does not speak.
      RequestError
      aclientp.exceptions
      Raised when a JSON-RPC request fails.

      🛈 DocStrings

      ACP-related exceptions.

      InvalidResponseError

      Bases: RequestError

      The peer answered a request with a result that does not match the method's schema.

      Raised by the typed v2 calls instead of a bare pydantic.ValidationError, which is kept as __cause__. Code -32603 (Internal error); data holds the method and the validation errors.

      Source code in src/aclientp/exceptions.py
      class InvalidResponseError(RequestError):
          """The peer answered a request with a result that does not match the method's schema.
      
          Raised by the typed v2 calls instead of a bare ``pydantic.ValidationError``, which
          is kept as ``__cause__``. Code ``-32603`` (Internal error); ``data`` holds the
          method and the validation errors.
          """
      
          def __init__(self, method: str, errors: list[ErrorDetails]) -> None:
              super().__init__(-32603, "Invalid response", {"method": method, "errors": errors})
              self.method = method
              """The method whose response was invalid."""
              self.errors = errors
              """The validation errors (pydantic's ``errors()``, without URLs and contexts)."""
      
          @classmethod
          def from_validation_error(cls, method: str, exc: ValidationError) -> InvalidResponseError:
              """Build the error from a pydantic validation error."""
              return cls(method, exc.errors(include_url=False, include_context=False))
      

      errors instance-attribute

      errors = errors
      

      The validation errors (pydantic's errors(), without URLs and contexts).

      method instance-attribute

      method = method
      

      The method whose response was invalid.

      from_validation_error classmethod

      from_validation_error(method: str, exc: ValidationError) -> InvalidResponseError
      

      Build the error from a pydantic validation error.

      Source code in src/aclientp/exceptions.py
      @classmethod
      def from_validation_error(cls, method: str, exc: ValidationError) -> InvalidResponseError:
          """Build the error from a pydantic validation error."""
          return cls(method, exc.errors(include_url=False, include_context=False))
      

      ProtocolVersionError

      Bases: RequestError

      The peer answered initialize with a protocol version this side does not speak.

      Raised by clients before the rest of the response is validated, so an agent that only speaks another version (e.g. a v1 agent answering a v2 client) is reported as such instead of as a malformed response.

      Source code in src/aclientp/exceptions.py
      class ProtocolVersionError(RequestError):
          """The peer answered ``initialize`` with a protocol version this side does not speak.
      
          Raised by clients before the rest of the response is validated, so an agent that
          only speaks another version (e.g. a v1 agent answering a v2 client) is reported
          as such instead of as a malformed response.
          """
      
          def __init__(self, expected: int, received: Any, result: Any = None) -> None:
              super().__init__(-32600, "Invalid request", {"expected": expected, "received": received})
              self.expected = expected
              """The protocol version this side speaks."""
              self.received = received
              """The ``protocolVersion`` of the response (any JSON value, as sent by the peer)."""
              self.result = result
              """The raw ``initialize`` result, e.g. to show the peer's name."""
      

      expected instance-attribute

      expected = expected
      

      The protocol version this side speaks.

      received instance-attribute

      received = received
      

      The protocolVersion of the response (any JSON value, as sent by the peer).

      result instance-attribute

      result = result
      

      The raw initialize result, e.g. to show the peer's name.

      RequestError

      Bases: Exception

      Raised when a JSON-RPC request fails.

      Source code in src/aclientp/exceptions.py
      class RequestError(Exception):
          """Raised when a JSON-RPC request fails."""
      
          def __init__(self, code: int, message: str, data: Any | None = None) -> None:
              super().__init__(message)
              self.code = code
              self.data = data
      
          @classmethod
          def parse_error(cls, data: dict[str, Any] | None = None) -> Self:
              return cls(-32700, "Parse error", data)
      
          @classmethod
          def invalid_request(cls, data: dict[str, Any] | None = None) -> Self:
              return cls(-32600, "Invalid request", data)
      
          @classmethod
          def method_not_found(cls, method: str) -> Self:
              return cls(-32601, "Method not found", {"method": method})
      
          @classmethod
          def invalid_params(cls, data: dict[str, Any] | None = None) -> Self:
              return cls(-32602, "Invalid params", data)
      
          @classmethod
          def internal_error(cls, data: dict[str, Any] | None = None) -> Self:
              return cls(-32603, "Internal error", data)
      
          @classmethod
          def request_cancelled(cls, data: dict[str, Any] | None = None) -> Self:
              return cls(-32800, "Request cancelled", data)
      
          @classmethod
          def resource_not_found(cls, uri: str | None = None) -> Self:
              data = {"uri": uri} if uri is not None else None
              return cls(-32002, "Resource not found", data)
      
          @classmethod
          def auth_required(cls, data: dict[str, Any] | None = None) -> Self:
              return cls(-32000, "Authentication required", data)
      
          @classmethod
          def session_not_found(cls, session_id: str) -> Self:
              """``-32002`` for a request naming a session that does not exist or is not active."""
              return cls(-32002, "Resource not found", {"sessionId": session_id})
      
          @classmethod
          def unsupported_protocol_version(cls, requested: int, supported: list[int]) -> Self:
              """``-32022``: no supported protocol version is at or below the requested one."""
              data = {"requested": requested, "supported": supported}
              return cls(-32022, "Unsupported protocol version", data)
      
          @classmethod
          def from_error_obj(cls, error: Any) -> Self:
              """Build an error from a peer's JSON-RPC error object.
      
              Peers are not trusted to send a well-formed object: ``data`` is optional per
              JSON-RPC 2.0 and ``code`` / ``message`` may be missing or have the wrong type.
              """
              if not isinstance(error, dict):
                  return cls.internal_error({"details": "Malformed error response", "error": error})
              code = error.get("code")
              message = error.get("message")
              return cls(
                  code if isinstance(code, int) and not isinstance(code, bool) else -32603,
                  message if isinstance(message, str) and message else "Error",
                  error.get("data"),
              )
      
          def to_error_obj(self) -> dict[str, Any]:
              return {"code": self.code, "message": str(self), "data": self.data}
      

      from_error_obj classmethod

      from_error_obj(error: Any) -> Self
      

      Build an error from a peer's JSON-RPC error object.

      Peers are not trusted to send a well-formed object: data is optional per JSON-RPC 2.0 and code / message may be missing or have the wrong type.

      Source code in src/aclientp/exceptions.py
      @classmethod
      def from_error_obj(cls, error: Any) -> Self:
          """Build an error from a peer's JSON-RPC error object.
      
          Peers are not trusted to send a well-formed object: ``data`` is optional per
          JSON-RPC 2.0 and ``code`` / ``message`` may be missing or have the wrong type.
          """
          if not isinstance(error, dict):
              return cls.internal_error({"details": "Malformed error response", "error": error})
          code = error.get("code")
          message = error.get("message")
          return cls(
              code if isinstance(code, int) and not isinstance(code, bool) else -32603,
              message if isinstance(message, str) and message else "Error",
              error.get("data"),
          )
      

      session_not_found classmethod

      session_not_found(session_id: str) -> Self
      

      -32002 for a request naming a session that does not exist or is not active.

      Source code in src/aclientp/exceptions.py
      @classmethod
      def session_not_found(cls, session_id: str) -> Self:
          """``-32002`` for a request naming a session that does not exist or is not active."""
          return cls(-32002, "Resource not found", {"sessionId": session_id})
      

      unsupported_protocol_version classmethod

      unsupported_protocol_version(requested: int, supported: list[int]) -> Self
      

      -32022: no supported protocol version is at or below the requested one.

      Source code in src/aclientp/exceptions.py
      @classmethod
      def unsupported_protocol_version(cls, requested: int, supported: list[int]) -> Self:
          """``-32022``: no supported protocol version is at or below the requested one."""
          data = {"requested": requested, "supported": supported}
          return cls(-32022, "Unsupported protocol version", data)