stdcrpcintercept

package
v0.0.260 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

type ErrorMapper func(ctx context.Context, logs *zap.Logger, err error) *connect.Error

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.

Directories

Path Synopsis
internal
v1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL