panichandler

package
v1.0.1-beta Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2020 License: MIT Imports: 6 Imported by: 0

README

panichandler

panichandler is an interceptor to protect a process from aborting by panic and return Internal error as status code.

Usage

import (
	"github.com/mercari/go-grpc-interceptor/panichandler"
)

func main() {
	uIntOpt := grpc.UnaryInterceptor(panichandler.UnaryServerInterceptor)
	sIntOpt := grpc.StreamInterceptor(panichandler.StreamServerInterceptor)
	grpc.NewServer(uIntOpt, sIntOpt)
}

Custom Panic Handler

You can write custom panic handler in case of panic. Use InstallPanicHandler.

func main() {
	panichandler.InstallPanicHandler(func(ctx context.Context, r interface{}) {
		fmt.Printf("panic happened: %v", r)
	}
}
Built-in custom panic handler
  • panichandler.LogPanicDump
  • debug.Stack() to stderr
  • glog.LogPanicStackMultiLine
    • show stack trace in multi line by glog
  • zap.LogPanicWithStackTrace
    • use zap.Logger in context and log panic with stack trace

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InstallPanicHandler

func InstallPanicHandler(handler PanicHandler)

func LogPanicDump

func LogPanicDump(ctx context.Context, r interface{})

LogPanicDump is a PanicHandler which dumps stack trace.

func StreamServerInterceptor

func StreamServerInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error)

func UnaryServerInterceptor

func UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error)

Types

type PanicHandler

type PanicHandler func(context.Context, interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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