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
pull/71/head
Quentin Machu 8 years ago committed by Jimmy Zelinskie
parent 4f4dbd5b61
commit 418ab08c4b

@ -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
}

@ -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)

Loading…
Cancel
Save