agent: replace goto with local function

This commit is contained in:
Frank Schroeder 2017-05-11 19:37:47 +02:00
parent 242ad8bb3a
commit ab1a94977d
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
1 changed files with 23 additions and 25 deletions

View File

@ -295,12 +295,11 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) {
} else if s.agent.config.EnableUI { } else if s.agent.config.EnableUI {
s.mux.Handle("/ui/", http.StripPrefix("/ui/", http.FileServer(assetFS()))) s.mux.Handle("/ui/", http.StripPrefix("/ui/", http.FileServer(assetFS())))
} }
} }
// wrap is used to wrap functions to make them more convenient // wrap is used to wrap functions to make them more convenient
func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Request) (interface{}, error)) func(resp http.ResponseWriter, req *http.Request) { func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Request) (interface{}, error)) http.HandlerFunc {
f := func(resp http.ResponseWriter, req *http.Request) { return func(resp http.ResponseWriter, req *http.Request) {
setHeaders(resp, s.agent.config.HTTPAPIResponseHeaders) setHeaders(resp, s.agent.config.HTTPAPIResponseHeaders)
setTranslateAddr(resp, s.agent.config.TranslateWanAddrs) setTranslateAddr(resp, s.agent.config.TranslateWanAddrs)
@ -322,6 +321,17 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque
} }
} }
handleErr := func(err error) {
s.logger.Printf("[ERR] http: Request %s %v, error: %v from=%s", req.Method, logURL, err, req.RemoteAddr)
code := http.StatusInternalServerError // 500
errMsg := err.Error()
if strings.Contains(errMsg, "Permission denied") || strings.Contains(errMsg, "ACL not found") {
code = http.StatusForbidden // 403
}
resp.WriteHeader(code)
fmt.Fprint(resp, errMsg)
}
// TODO (slackpad) We may want to consider redacting prepared // TODO (slackpad) We may want to consider redacting prepared
// query names/IDs here since they are proxies for tokens. But, // query names/IDs here since they are proxies for tokens. But,
// knowing one only gives you read access to service listings // knowing one only gives you read access to service listings
@ -337,34 +347,22 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque
s.logger.Printf("[DEBUG] http: Request %s %v (%v) from=%s", req.Method, logURL, time.Now().Sub(start), req.RemoteAddr) s.logger.Printf("[DEBUG] http: Request %s %v (%v) from=%s", req.Method, logURL, time.Now().Sub(start), req.RemoteAddr)
}() }()
obj, err := handler(resp, req) obj, err := handler(resp, req)
// Check for an error
HAS_ERR:
if err != nil { if err != nil {
s.logger.Printf("[ERR] http: Request %s %v, error: %v from=%s", req.Method, logURL, err, req.RemoteAddr) handleErr(err)
code := http.StatusInternalServerError // 500 return
errMsg := err.Error() }
if strings.Contains(errMsg, "Permission denied") || strings.Contains(errMsg, "ACL not found") { if obj == nil {
code = http.StatusForbidden // 403
}
resp.WriteHeader(code)
fmt.Fprint(resp, err.Error())
return return
} }
if obj != nil { buf, err := s.marshalJSON(req, obj)
var buf []byte if err != nil {
buf, err = s.marshalJSON(req, obj) handleErr(err)
if err != nil { return
goto HAS_ERR
}
resp.Header().Set("Content-Type", "application/json")
resp.Write(buf)
} }
resp.Header().Set("Content-Type", "application/json")
resp.Write(buf)
} }
return f
} }
// marshalJSON marshals the object into JSON, respecting the user's pretty-ness // marshalJSON marshals the object into JSON, respecting the user's pretty-ness