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 NewServerLogger ¶ added in v0.0.250
func NewServerLogger(logs *zap.Logger) connect.ServerInterceptor
NewServerLogger returns the connect-go v2 interceptor that puts a logger for each RPC in the context, with fields that describe the RPC:
- "procedure", the RPC's procedure, such as "/acme.v1.ThingService/GetThing";
- "rpc.service" and "rpc.method", its parts, such as "acme.v1.ThingService" and "GetThing", named as in OpenTelemetry's semantic conventions;
- "rpc.idempotency", its idempotency level, such as "no_side_effects" for a read;
- "rpc.protocol" and "rpc.codec", the wire protocol and codec of the call, such as "grpc" and "proto", when the transport reports them.
It builds on the logger already in the context, such as the request's logger that github.com/advdv/stdgo/stdhttpware puts there, and on logs for calls that come with none, such as in-process ones.
stdtx, and so github.com/advdv/stdgo/fx/stdpgxtxfx/stdpgxtxconnect.CTx, log to it, and panic without one. Install it outermost, so the interceptors inside it, such as NewServerErrors, log to it too.
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.