2019-05-16 10:39:25 -04:00
|
|
|
package mid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2019-07-13 12:16:28 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// Errors handles errors coming out of the call chain. It detects normal
|
|
|
|
// application errors which are used to respond to the client in a uniform way.
|
|
|
|
// Unexpected errors (status >= 500) are logged.
|
|
|
|
func Errors(log *log.Logger) web.Middleware {
|
|
|
|
|
|
|
|
// This is the actual middleware function to be executed.
|
|
|
|
f := func(before web.Handler) web.Handler {
|
|
|
|
|
|
|
|
// Create the handler that will be attached in the middleware chain.
|
|
|
|
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
2019-05-23 19:40:29 -05:00
|
|
|
span, ctx := tracer.StartSpanFromContext(ctx, "internal.mid.Errors")
|
|
|
|
defer span.Finish()
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
if err := before(ctx, w, r, params); err != nil {
|
|
|
|
|
|
|
|
// Log the error.
|
2019-05-23 19:40:29 -05:00
|
|
|
log.Printf("%d : ERROR : %+v", span.Context().TraceID(), err)
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
// Respond to the error.
|
2019-06-26 20:21:00 -08:00
|
|
|
if web.RequestIsJson(r) {
|
|
|
|
if err := web.RespondJsonError(ctx, w, err); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := web.RespondError(ctx, w, err); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we receive the shutdown err we need to return it
|
|
|
|
// back to the base handler to shutdown the service.
|
|
|
|
if ok := web.IsShutdown(err); ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The error has been handled so we can stop propagating it.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|