From 418ab08c4b248fe119a83179b25b6aef43070014 Mon Sep 17 00:00:00 2001 From: Quentin Machu Date: Fri, 19 Feb 2016 14:53:52 -0500 Subject: [PATCH] api: adjust postLayer error codes - return 422 when layer could not be analyzed (extraction failed or layer unsupported) - return 404 if the parent is not found or the download path leads to a 404 page --- api/v1/routes.go | 20 ++++++++++++++++++++ worker/detectors/data.go | 2 ++ 2 files changed, 22 insertions(+) diff --git a/api/v1/routes.go b/api/v1/routes.go index 59be2b7d..60577d1e 100644 --- a/api/v1/routes.go +++ b/api/v1/routes.go @@ -51,6 +51,13 @@ const ( // maxBodySize restricts client request bodies to 1MiB. maxBodySize int64 = 1048576 + + // statusUnprocessableEntity represents the 422 (Unprocessable Entity) status code, which means + // the server understands the content type of the request entity + // (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the + // request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was + // unable to process the contained instructions. + statusUnprocessableEntity = 422 ) func decodeJSON(r *http.Request, v interface{}) error { @@ -97,11 +104,24 @@ func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format) if err != nil { + if err == cerrors.ErrNotFound || err == worker.ErrParentUnknown { + writeResponse(w, r, http.StatusNotFound, LayerEnvelope{Error: &Error{err.Error()}}) + return postLayerRoute, http.StatusNotFound + } + + if err == utils.ErrCouldNotExtract || + err == utils.ErrExtractedFileTooBig || + err == worker.ErrUnsupported { + writeResponse(w, r, statusUnprocessableEntity, LayerEnvelope{Error: &Error{err.Error()}}) + return postLayerRoute, statusUnprocessableEntity + } + _, badreq := err.(*cerrors.ErrBadRequest) if badreq || err == utils.ErrCouldNotExtract || err == utils.ErrExtractedFileTooBig { writeResponse(w, r, http.StatusBadRequest, LayerEnvelope{Error: &Error{err.Error()}}) return postLayerRoute, http.StatusBadRequest } + writeResponse(w, r, http.StatusInternalServerError, LayerEnvelope{Error: &Error{err.Error()}}) return postLayerRoute, http.StatusInternalServerError } diff --git a/worker/detectors/data.go b/worker/detectors/data.go index 144f8125..007dca3e 100644 --- a/worker/detectors/data.go +++ b/worker/detectors/data.go @@ -82,6 +82,8 @@ func DetectData(path string, format string, toExtract []string, maxFileSize int6 if err != nil { log.Warningf("could not download layer: %s", err) return nil, cerrors.ErrCouldNotDownload + if r.StatusCode == 404 { + return nil, cerrors.ErrNotFound } if math.Floor(float64(r.StatusCode/100)) != 2 { log.Warningf("could not download layer: got status code %d, expected 2XX", r.StatusCode)