Skip to content

Instantly share code, notes, and snippets.

@voodoo-dn
Last active January 21, 2020 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voodoo-dn/6af6cf54b8145cc3d4d87d888c4bf907 to your computer and use it in GitHub Desktop.
Save voodoo-dn/6af6cf54b8145cc3d4d87d888c4bf907 to your computer and use it in GitHub Desktop.
func NewLoggerInterceptor(logger *log.Logger) HandlerInterceptor {
return func(ctx context.Context, methodName string, arg interface{}, handlerFunc HandlerFunc) (result interface{}, err error) {
handlerResult, handlerError := handlerFunc(ctx, arg)
logger.Println(json.Marshal(struct {
MethodName string `json:"method_name"`
Argument interface{} `json:"argument"`
Result interface{} `json:"result"`
}{
MethodName: methodName,
Argument: arg,
Result: handlerResult,
}))
return handlerResult, handlerError
}
}
func NewPanicInterceptor(logger *log.Logger) HandlerInterceptor {
return func(ctx context.Context, methodName string, arg interface{}, handlerFunc HandlerFunc) (result interface{}, err error) {
defer func() {
if rec := recover(); rec != nil {
logger.Printf("panic in handler", rec)
err = thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "Internal server error")
}
}()
return handlerFunc(ctx, arg)
}
}
var DummyProcessor = new DummyProcessor(new DummyHandler(), thrift.NewHandlerInterceptorOption(new ChainedHandlerInterceptor(
NewPanicInterceptor(logger),
NewLoggerInterceptor(logger),
)))
someProcessor.Process(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment