-
-
Save voodoo-dn/6af6cf54b8145cc3d4d87d888c4bf907 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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