2019-05-16 10:39:25 -04:00
|
|
|
package mid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2019-05-20 22:16:58 -05:00
|
|
|
"runtime/debug"
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
|
|
|
"github.com/pkg/errors"
|
2019-05-25 08:26:37 -05:00
|
|
|
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
|
2019-05-16 10:39:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Panics recovers from panics and converts the panic to an error so it is
|
|
|
|
// reported in Metrics and handled in Errors.
|
|
|
|
func Panics() web.Middleware {
|
|
|
|
|
|
|
|
// This is the actual middleware function to be executed.
|
|
|
|
f := func(after web.Handler) web.Handler {
|
|
|
|
|
|
|
|
// Wrap this handler around the next one provided.
|
|
|
|
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) (err error) {
|
2019-05-23 19:40:29 -05:00
|
|
|
span, ctx := tracer.StartSpanFromContext(ctx, "internal.mid.Panics")
|
|
|
|
defer span.Finish()
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
// Defer a function to recover from a panic and set the err return variable
|
|
|
|
// after the fact. Using the errors package will generate a stack trace.
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2019-05-20 22:16:58 -05:00
|
|
|
err = errors.Errorf("panic: %+v %s", r, string(debug.Stack()))
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Call the next Handler and set its return value in the err variable.
|
|
|
|
return after(ctx, w, r, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|