recovery

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package recovery is a middleware that recovers from panics and logs the panic message.

`recovery` are interceptors that recover from gRPC handler panics.

Server Side Recovery Middleware

By default a panic will be converted into a gRPC error with `code.Internal`.

Handling can be customised by providing an alternate recovery function.

Please see examples for simple examples of use.

Example (Initialization)

Initialization shows an initialization sequence with a custom recovery handler func.

// Copyright 2017 David Ackroyd. All Rights Reserved.
// See LICENSE for licensing terms.

package main

import (
	"github.com/lnyyj/go-grpc-middleware/interceptors/recovery"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

var (
	customFunc recovery.RecoveryHandlerFunc
)

// Initialization shows an initialization sequence with a custom recovery handler func.
func main() {
	// Define customfunc to handle panic
	customFunc = func(p any) (err error) {
		return status.Errorf(codes.Unknown, "panic triggered: %v", p)
	}
	// Shared options for the logger, with a custom gRPC code to log level function.
	opts := []recovery.Option{
		recovery.WithRecoveryHandler(customFunc),
	}
	// Create a server. Recovery handlers should typically be last in the chain so that other middleware
	// (e.g. logging) can operate on the recovered state instead of being directly affected by any panic
	_ = grpc.NewServer(
		grpc.UnaryInterceptor(
			recovery.UnaryServerInterceptor(opts...),
		),
		grpc.StreamInterceptor(
			recovery.StreamServerInterceptor(opts...),
		),
	)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamServerInterceptor

func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor

StreamServerInterceptor returns a new streaming server interceptor for panic recovery.

func UnaryServerInterceptor

func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptor for panic recovery.

Types

type Option

type Option func(*options)

func WithRecoveryHandler

func WithRecoveryHandler(f RecoveryHandlerFunc) Option

WithRecoveryHandler customizes the function for recovering from a panic.

func WithRecoveryHandlerContext

func WithRecoveryHandlerContext(f RecoveryHandlerFuncContext) Option

WithRecoveryHandlerContext customizes the function for recovering from a panic.

type PanicError

type PanicError struct {
	Panic any
	Stack []byte
}

func (*PanicError) Error

func (e *PanicError) Error() string

type RecoveryHandlerFunc

type RecoveryHandlerFunc func(p any) (err error)

RecoveryHandlerFunc is a function that recovers from the panic `p` by returning an `error`.

type RecoveryHandlerFuncContext

type RecoveryHandlerFuncContext func(ctx context.Context, p any) (err error)

RecoveryHandlerFuncContext is a function that recovers from the panic `p` by returning an `error`. The context can be used to extract request scoped metadata and context values.

Jump to

Keyboard shortcuts

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