Merge pull request #199 from vieux/log_errors

log all API errors
This commit is contained in:
Victor Vieux 2014-12-22 16:22:22 -08:00
commit a58043d354
1 changed files with 17 additions and 12 deletions

View File

@ -70,7 +70,7 @@ func getVersion(c *context, w http.ResponseWriter, r *http.Request) {
// GET /containers/json // GET /containers/json
func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) { func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) httpError(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -111,12 +111,12 @@ func getContainerJSON(c *context, w http.ResponseWriter, r *http.Request) {
if container != nil { if container != nil {
resp, err := http.Get(container.Node().Addr + "/containers/" + container.Id + "/json") resp, err := http.Get(container.Node().Addr + "/containers/" + container.Id + "/json")
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) httpError(w, err.Error(), http.StatusInternalServerError)
return return
} }
data, err := ioutil.ReadAll(resp.Body) data, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) httpError(w, err.Error(), http.StatusInternalServerError)
return return
} }
w.Write(bytes.Replace(data, []byte("\"HostIp\":\"0.0.0.0\""), []byte(fmt.Sprintf("\"HostIp\":%q", container.Node().IP)), -1)) w.Write(bytes.Replace(data, []byte("\"HostIp\":\"0.0.0.0\""), []byte(fmt.Sprintf("\"HostIp\":%q", container.Node().IP)), -1))
@ -132,23 +132,23 @@ func postContainersCreate(c *context, w http.ResponseWriter, r *http.Request) {
) )
if err := json.NewDecoder(r.Body).Decode(&config); err != nil { if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) httpError(w, err.Error(), http.StatusBadRequest)
return return
} }
if config.AttachStdout || config.AttachStdin || config.AttachStderr { if config.AttachStdout || config.AttachStdin || config.AttachStderr {
http.Error(w, "Attach is not supported in clustering mode, use -d.", http.StatusInternalServerError) httpError(w, "Attach is not supported in clustering mode, use -d.", http.StatusInternalServerError)
return return
} }
if container := c.cluster.Container(name); container != nil { if container := c.cluster.Container(name); container != nil {
http.Error(w, fmt.Sprintf("Conflict, The name %s is already assigned to %s. You have to delete (or rename) that container to be able to assign %s to a container again.", name, container.Id, name), http.StatusConflict) httpError(w, fmt.Sprintf("Conflict, The name %s is already assigned to %s. You have to delete (or rename) that container to be able to assign %s to a container again.", name, container.Id, name), http.StatusConflict)
return return
} }
container, err := c.scheduler.CreateContainer(&config, name) container, err := c.scheduler.CreateContainer(&config, name)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) httpError(w, err.Error(), http.StatusInternalServerError)
return return
} }
fmt.Fprintf(w, "{%q:%q}", "Id", container.Id) fmt.Fprintf(w, "{%q:%q}", "Id", container.Id)
@ -158,7 +158,7 @@ func postContainersCreate(c *context, w http.ResponseWriter, r *http.Request) {
// DELETE /containers/{name:.*} // DELETE /containers/{name:.*}
func deleteContainer(c *context, w http.ResponseWriter, r *http.Request) { func deleteContainer(c *context, w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) httpError(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -166,11 +166,11 @@ func deleteContainer(c *context, w http.ResponseWriter, r *http.Request) {
force := r.Form.Get("force") == "1" force := r.Form.Get("force") == "1"
container := c.cluster.Container(name) container := c.cluster.Container(name)
if container == nil { if container == nil {
http.Error(w, fmt.Sprintf("Container %s not found", name), http.StatusNotFound) httpError(w, fmt.Sprintf("Container %s not found", name), http.StatusNotFound)
return return
} }
if err := c.scheduler.RemoveContainer(container, force); err != nil { if err := c.scheduler.RemoveContainer(container, force); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) httpError(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -217,7 +217,7 @@ func proxyContainer(c *context, w http.ResponseWriter, r *http.Request) {
log.Debugf("[PROXY] --> %s %s", r.Method, r.URL) log.Debugf("[PROXY] --> %s %s", r.Method, r.URL)
resp, err := client.Do(r) resp, err := client.Do(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) httpError(w, err.Error(), http.StatusInternalServerError)
return return
} }
w.WriteHeader(resp.StatusCode) w.WriteHeader(resp.StatusCode)
@ -227,7 +227,7 @@ func proxyContainer(c *context, w http.ResponseWriter, r *http.Request) {
// Default handler for methods not supported by clustering. // Default handler for methods not supported by clustering.
func notImplementedHandler(c *context, w http.ResponseWriter, r *http.Request) { func notImplementedHandler(c *context, w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not supported in clustering mode.", http.StatusNotImplemented) httpError(w, "Not supported in clustering mode.", http.StatusNotImplemented)
} }
func optionsHandler(c *context, w http.ResponseWriter, r *http.Request) { func optionsHandler(c *context, w http.ResponseWriter, r *http.Request) {
@ -240,6 +240,11 @@ func writeCorsHeaders(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS") w.Header().Add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS")
} }
func httpError(w http.ResponseWriter, err string, status int) {
log.Error(err)
http.Error(w, err, status)
}
func createRouter(c *context, enableCors bool) (*mux.Router, error) { func createRouter(c *context, enableCors bool) (*mux.Router, error) {
r := mux.NewRouter() r := mux.NewRouter()
m := map[string]map[string]handler{ m := map[string]map[string]handler{