return type is JSONResponse for errors

This commit is contained in:
Parminder Singh 2018-03-04 21:07:32 +05:30
parent e6e81e526d
commit 62f9071217
2 changed files with 9 additions and 4 deletions

View file

@ -175,11 +175,11 @@ func Setup(
).Methods("GET", "POST", "OPTIONS") ).Methods("GET", "POST", "OPTIONS")
r0mux.Handle("/auth/{authType}/fallback/web?session={sessionID}", r0mux.Handle("/auth/{authType}/fallback/web?session={sessionID}",
common.MakeHtmlAPI("authfallback", func(w http.ResponseWriter, req *http.Request) util.JSONResponse { common.MakeHTMLAPI("authfallback", func(w http.ResponseWriter, req *http.Request) util.JSONResponse {
vars := mux.Vars(req) vars := mux.Vars(req)
return AuthFallback(w, req, vars["authType"], vars["sessionID"], cfg) return AuthFallback(w, req, vars["authType"], vars["sessionID"], cfg)
}), }),
).Methods("GET", "OPTIONS") ).Methods("GET", "POST", "OPTIONS")
r0mux.Handle("/pushrules/", r0mux.Handle("/pushrules/",
common.MakeExternalAPI("push_rules", func(req *http.Request) util.JSONResponse { common.MakeExternalAPI("push_rules", func(req *http.Request) util.JSONResponse {

View file

@ -41,12 +41,17 @@ func MakeExternalAPI(metricsName string, f func(*http.Request) util.JSONResponse
// MakeHTMLAPI adds Span metrics to the HTML Handler function // MakeHTMLAPI adds Span metrics to the HTML Handler function
// This is used to serve HTML template // This is used to serve HTML template
func MakeHtmlAPI(metricsName string, f func(w http.ResponseWriter, *http.Request)) http.Handler { func MakeHTMLAPI(metricsName string, f func(http.ResponseWriter, *http.Request) util.JSONResponse) http.Handler {
withSpan := func(w http.ResponseWriter, req *http.Request) { withSpan := func(w http.ResponseWriter, req *http.Request) {
span := opentracing.StartSpan(metricsName) span := opentracing.StartSpan(metricsName)
defer span.Finish() defer span.Finish()
req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span)) req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span))
f(w, req) if err := f(w, req); err {
h := util.MakeJSONAPI(util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
return err
}))
h.ServeHTTP(w, req)
}
} }
return prometheus.InstrumentHandler(metricsName, http.HandlerFunc(withSpan)) return prometheus.InstrumentHandler(metricsName, http.HandlerFunc(withSpan))