Documentation
¶
Overview ¶
Package stdcrpcintercept holds re-usable connect rpc interceptors. They take the connect-go version their signatures name: NewValidateResponse connect-go v1 (connectrpc.com/connect), and NewServerErrors connect-go v2 (connectrpc.com/connect/v2). For connect-go v2, validate responses with the interceptor of connectrpc.com/validate and its WithValidateResponses option instead.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewServerErrors ¶ added in v0.0.249
func NewServerErrors(logs *zap.Logger, mappers ...ErrorMapper) connect.ServerInterceptor
NewServerErrors returns the interceptor that turns every error of the RPCs it wraps into the Connect error the client receives. Handlers return a *connect.Error for the outcomes a client should see, such as NOT_FOUND, and wrap anything else. The interceptor maps an error with the first rule that applies:
- a *connect.Error in the error's chain is the handler's own verdict, and is returned as is, unwrapped from the rest of the chain; a remote one is another server's verdict, from a client call the handler made, and is not passed on;
- each of mappers in turn, until one returns an error;
- context.Canceled and context.DeadlineExceeded become CANCELED and DEADLINE_EXCEEDED;
- anything else is logged and becomes INTERNAL, with a message that does not reveal it.
The default rules keep the error as the cause, for errors.Is and errors.As on the server; Connect never sends the cause to the client. The interceptor complements github.com/advdv/stdgo/fx/stdpgxtxfx/stdpgxtxconnect.CTx, which maps the database errors it knows inside a transaction, and returns other errors as they are.
It logs, and gives mappers, the logger in the context, or logs when the context has none. Install it outside the interceptors whose errors it should map, such as the one that validates requests.
func NewValidateResponse ¶
func NewValidateResponse(val protovalidate.Validator) connect.UnaryInterceptorFunc
NewValidateResponse creates a Connect interceptor that validates responses. Useful for test environments.
Types ¶
type ErrorMapper ¶ added in v0.0.249
ErrorMapper maps the errors of an RPC that it knows to the Connect error the client receives, such as a domain error to ALREADY_EXISTS. It returns nil for the errors it leaves to the next mapper, or to the default mapping of NewServerErrors. It logs to logs what it keeps from the client.