Package io.jooby.trpc

Class TrpcException

All Implemented Interfaces:
Serializable

public class TrpcException extends RuntimeException
A specialized runtime exception that encapsulates a tRPC error.

This exception is thrown when a tRPC procedure fails, either due to framework-level issues (like invalid JSON parsing or missing arguments) or user-thrown domain errors. It holds all the necessary context—the procedure name, the specific TrpcErrorCode, and the underlying cause—required to construct a compliant tRPC error response.

The frontend @trpc/client relies on a very specific JSON envelope to correctly reconstruct the TRPCClientError on the client side. The toMap() method handles translating this Java exception into that exact nested map structure.

See Also:
  • Constructor Details

    • TrpcException

      public TrpcException(String procedure, io.jooby.StatusCode code, Throwable cause)
      Constructs a new tRPC exception using a standard Jooby HTTP status code.
      Parameters:
      procedure - The name of the tRPC procedure that failed (e.g., "movies.getById").
      code - The Jooby HTTP status code, which will be mapped to its closest tRPC equivalent.
      cause - The underlying exception that caused this failure.
    • TrpcException

      public TrpcException(String procedure, TrpcErrorCode code, Throwable cause)
      Constructs a new tRPC exception using a specific tRPC error code.
      Parameters:
      procedure - The name of the tRPC procedure that failed.
      code - The explicit tRPC error code.
      cause - The underlying exception that caused this failure.
    • TrpcException

      public TrpcException(String procedure, TrpcErrorCode code)
      Constructs a new tRPC exception without an underlying cause.
      Parameters:
      procedure - The name of the tRPC procedure that failed.
      code - The explicit tRPC error code.
    • TrpcException

      public TrpcException(String procedure, io.jooby.StatusCode code)
      Constructs a new tRPC exception using a standard Jooby HTTP status code without an underlying cause.
      Parameters:
      procedure - The name of the tRPC procedure that failed.
      code - The Jooby HTTP status code.
  • Method Details

    • getStatusCode

      public io.jooby.StatusCode getStatusCode()
      Gets the HTTP status code associated with this error.
      Returns:
      The Jooby StatusCode to be set in the HTTP response headers.
    • getProcedure

      public String getProcedure()
      Gets the name of the tRPC procedure that threw this exception.
      Returns:
      The procedure name (e.g., "movies.getById").
    • toMap

      public Map<String,Object> toMap()
      Serializes the exception into the exact JSON structure expected by the tRPC protocol.

      The resulting map translates into the following JSON shape:

      
       {
       "error": {
       "message": "The descriptive error message",
       "code": -32600,
       "data": {
       "code": "BAD_REQUEST",
       "httpStatus": 400,
       "path": "movies.getById"
       }
       }
       }
       
      Returns:
      A nested map representing the JSON error envelope.