Merge pull request #161 from fnproject/catch-panics

catch request panics in goroutine
This commit is contained in:
Reed Allman
2017-07-28 11:35:25 -07:00
committed by GitHub

View File

@@ -109,7 +109,7 @@ func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, logDB
setMachineId()
setTracer()
s.Router.Use(loggerWrap, traceWrap)
s.Router.Use(loggerWrap, traceWrap, panicWrap)
s.bindHandlers(ctx)
for _, opt := range opts {
@@ -203,6 +203,20 @@ func whoAmI() net.IP {
return nil
}
func panicWrap(c *gin.Context) {
defer func(c *gin.Context) {
if rec := recover(); rec != nil {
err, ok := rec.(error)
if !ok {
err = fmt.Errorf("fn: %v", rec)
}
handleErrorResponse(c, err)
c.Abort()
}
}(c)
c.Next()
}
func loggerWrap(c *gin.Context) {
ctx, _ := common.LoggerWithFields(c.Request.Context(), extractFields(c))
@@ -297,16 +311,12 @@ func (s *Server) startGears(ctx context.Context) {
}
const runHeader = `
____ __
/ __ \_________ ______/ /__
/ / / / ___/ __ / ___/ / _ \
/ /_/ / / / /_/ / /__/ / __/
\_________ \__,_/\___/_/\____
/ ____/_ __ ___ _____/ /_( )___ ____ _____
/ /_ / / / / __ \/ ___/ __/ / __ \/ __ \/ ___/
/ __/ / /_/ / / / / /__/ /_/ / /_/ / / / (__ )
/_/ \____/_/ /_/\___/\__/_/\____/_/ /_/____/
`
______
/ ____/___
/ /_ / __ \
/ __/ / / / /
/_/ /_/ /_/
`
fmt.Println(runHeader)
logrus.Infof("Serving Functions API on address `%s`", listen)