allow pretty printing on http errors in query endpoint

pull/1527/head
Cory LaNou 2015-02-07 10:32:37 -07:00
parent 3723bf2e80
commit bcd9724ce0
1 changed files with 23 additions and 15 deletions

View File

@ -127,7 +127,7 @@ func (h *Handler) serveQuery(w http.ResponseWriter, r *http.Request, user *influ
// Parse query from query string. // Parse query from query string.
query, err := p.ParseQuery() query, err := p.ParseQuery()
if err != nil { if err != nil {
httpError(w, "error parsing query: "+err.Error(), http.StatusBadRequest) httpError(w, "error parsing query: "+err.Error(), pretty, http.StatusBadRequest)
return return
} }
@ -194,7 +194,7 @@ func (h *Handler) serveMetastore(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", `attachment; filename="meta"`) w.Header().Set("Content-Disposition", `attachment; filename="meta"`)
if err := h.server.CopyMetastore(w); err != nil { if err := h.server.CopyMetastore(w); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError) httpError(w, err.Error(), false, http.StatusInternalServerError)
} }
} }
@ -245,23 +245,23 @@ func (h *Handler) serveCreateDataNode(w http.ResponseWriter, r *http.Request) {
// Read in data node from request body. // Read in data node from request body.
var n dataNodeJSON var n dataNodeJSON
if err := json.NewDecoder(r.Body).Decode(&n); err != nil { if err := json.NewDecoder(r.Body).Decode(&n); err != nil {
httpError(w, err.Error(), http.StatusBadRequest) httpError(w, err.Error(), false, http.StatusBadRequest)
return return
} }
// Parse the URL. // Parse the URL.
u, err := url.Parse(n.URL) u, err := url.Parse(n.URL)
if err != nil { if err != nil {
httpError(w, "invalid data node url", http.StatusBadRequest) httpError(w, "invalid data node url", false, http.StatusBadRequest)
return return
} }
// Create the data node. // Create the data node.
if err := h.server.CreateDataNode(u); err == influxdb.ErrDataNodeExists { if err := h.server.CreateDataNode(u); err == influxdb.ErrDataNodeExists {
httpError(w, err.Error(), http.StatusConflict) httpError(w, err.Error(), false, http.StatusConflict)
return return
} else if err != nil { } else if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError) httpError(w, err.Error(), false, http.StatusInternalServerError)
return return
} }
@ -270,7 +270,7 @@ func (h *Handler) serveCreateDataNode(w http.ResponseWriter, r *http.Request) {
// Create a new replica on the broker. // Create a new replica on the broker.
if err := h.server.Client().CreateReplica(node.ID); err != nil { if err := h.server.Client().CreateReplica(node.ID); err != nil {
httpError(w, err.Error(), http.StatusBadGateway) httpError(w, err.Error(), false, http.StatusBadGateway)
return return
} }
@ -285,16 +285,16 @@ func (h *Handler) serveDeleteDataNode(w http.ResponseWriter, r *http.Request) {
// Parse node id. // Parse node id.
nodeID, err := strconv.ParseUint(r.URL.Query().Get(":id"), 10, 64) nodeID, err := strconv.ParseUint(r.URL.Query().Get(":id"), 10, 64)
if err != nil { if err != nil {
httpError(w, "invalid node id", http.StatusBadRequest) httpError(w, "invalid node id", false, http.StatusBadRequest)
return return
} }
// Delete the node. // Delete the node.
if err := h.server.DeleteDataNode(nodeID); err == influxdb.ErrDataNodeNotFound { if err := h.server.DeleteDataNode(nodeID); err == influxdb.ErrDataNodeNotFound {
httpError(w, err.Error(), http.StatusNotFound) httpError(w, err.Error(), false, http.StatusNotFound)
return return
} else if err != nil { } else if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError) httpError(w, err.Error(), false, http.StatusInternalServerError)
return return
} }
@ -331,10 +331,18 @@ func httpResults(w http.ResponseWriter, results influxdb.Results, pretty bool) {
} }
// httpError writes an error to the client in a standard format. // httpError writes an error to the client in a standard format.
func httpError(w http.ResponseWriter, error string, code int) { func httpError(w http.ResponseWriter, error string, pretty bool, code int) {
w.Header().Add("content-type", "application/json") w.Header().Add("content-type", "application/json")
w.WriteHeader(code) w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(influxdb.Results{Err: errors.New(error)})
results := influxdb.Results{Err: errors.New(error)}
var b []byte
if pretty {
b, _ = json.MarshalIndent(results, "", " ")
} else {
b, _ = json.Marshal(results)
}
w.Write(b)
} }
// Filters and filter helpers // Filters and filter helpers
@ -375,17 +383,17 @@ func authenticate(inner func(http.ResponseWriter, *http.Request, *influxdb.User)
if requireAuthentication && h.server.UserCount() > 0 { if requireAuthentication && h.server.UserCount() > 0 {
username, password, err := parseCredentials(r) username, password, err := parseCredentials(r)
if err != nil { if err != nil {
httpError(w, err.Error(), http.StatusUnauthorized) httpError(w, err.Error(), false, http.StatusUnauthorized)
return return
} }
if username == "" { if username == "" {
httpError(w, "username required", http.StatusUnauthorized) httpError(w, "username required", false, http.StatusUnauthorized)
return return
} }
user, err = h.server.Authenticate(username, password) user, err = h.server.Authenticate(username, password)
if err != nil { if err != nil {
httpError(w, err.Error(), http.StatusUnauthorized) httpError(w, err.Error(), false, http.StatusUnauthorized)
return return
} }
} }