mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
catch request panics in goroutine
the async stuff uses carlos supervisor thing but in the normal request path we aren't catching any panics and returning a 500 to user (conn just gets closed & server dies). should catch any mistakes we might make, or any one of the 10000 libraries we're importing. closes #150
This commit is contained in:
@@ -14,12 +14,6 @@ import (
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/ccirello/supervisor"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"github.com/opentracing/opentracing-go/ext"
|
||||
"github.com/openzipkin/zipkin-go-opentracing"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/fnproject/fn/api"
|
||||
"github.com/fnproject/fn/api/datastore"
|
||||
"github.com/fnproject/fn/api/id"
|
||||
@@ -28,6 +22,12 @@ import (
|
||||
"github.com/fnproject/fn/api/mqs"
|
||||
"github.com/fnproject/fn/api/runner"
|
||||
"github.com/fnproject/fn/api/runner/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"github.com/opentracing/opentracing-go/ext"
|
||||
"github.com/openzipkin/zipkin-go-opentracing"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -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,19 @@ 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)
|
||||
c.Next()
|
||||
}
|
||||
|
||||
func loggerWrap(c *gin.Context) {
|
||||
ctx, _ := common.LoggerWithFields(c.Request.Context(), extractFields(c))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user