xtwirp

package
v1.37.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Index

Constants

View Source
const DetailsMetaKey = "error_details"

DetailsMetaKey is a Twirp error metadata key that is used to pass protobuf-encoded error details.

Variables

This section is empty.

Functions

func ClientPassErrorDetails

func ClientPassErrorDetails() twirp.ClientOption

ClientPassErrorDetails converts Twirp errors to gRPC errors, if possible. This allows passing error details returned via gRPC/PSRPC by the backend server.

func ClientPassTimout

func ClientPassTimout() twirp.ClientOption

ClientPassTimout adds context timeout as a Twirp request header.

func DefaultClientOptions

func DefaultClientOptions() []twirp.ClientOption

DefaultClientOptions returns default Twirp client options.

func DefaultServerOptions

func DefaultServerOptions() []twirp.ServerOption

DefaultServerOptions returns default Twirp server options.

func ErrorCodeFromGRPC

func ErrorCodeFromGRPC(code codes.Code) twirp.ErrorCode

ErrorCodeFromGRPC converts gRPC error code to Twirp.

func GetHeaders

func GetHeaders(ctx context.Context) http.Header

GetHeaders returns Twirp headers from the context.

func PassHeadersHandler

func PassHeadersHandler(h http.Handler) http.Handler

PassHeadersHandler wraps Twirp server handler to allow passing HTTP headers in the context.

func RegisterServer

func RegisterServer(mux *http.ServeMux, s Server)

RegisterServer registers Twirp server on an HTTP mux. It also calls WrapHandler to add default middleware.

func ServerPassErrorDetails

func ServerPassErrorDetails() twirp.ServerOption

ServerPassErrorDetails converts gRPC errors to Twirp errors. It properly converts gRPC/PSRPC error codes and preserves custom error details.

func ServerPassTimeout

func ServerPassTimeout() twirp.ServerOption

ServerPassTimeout uses context timeout from Twirp request header. It requires that Twirp server handler is wrapped with PassHeadersHandler.

func StatusFromError

func StatusFromError(err error) (*status.Status, bool)

StatusFromError is an analog of gRPCs status.FromError, but it also considers error details encoded in Twirp metadata.

func ToError

func ToError(err error) twirp.Error

ToError converts any error to Twirp error, preserving error details.

func WithDetailsFrom

func WithDetailsFrom(dst twirp.Error, src error) twirp.Error

WithDetailsFrom sets gRPC/PSRPC error details from src as Twirp error metadata on dst.

If error details implement ErrorMeta, their custom metadata fields will be included as well.

func WithDetailsFromStatus

func WithDetailsFromStatus(dst twirp.Error, st *status.Status) twirp.Error

WithDetailsFromStatus sets gRPC error details from status as Twirp error metadata on dst.

If error details implement ErrorMeta, their custom metadata fields will be included as well.

func WrapHandler

func WrapHandler(s Server) http.Handler

WrapHandler wraps the Twirp server handler with our custom middleware.

Types

type ErrorMeta

type ErrorMeta interface {
	TwirpErrorMeta() map[string]string
}

ErrorMeta is an optional interface that allows attaching Twirp error metadata.

type Server

type Server interface {
	http.Handler
	PathPrefix() string
}

Server is a minimal interface for a Twirp server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳