Skip to content

Commit

Permalink
api: adjust postLayer error codes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Quentin-M authored and jzelinskie committed Feb 24, 2016
1 parent 4f4dbd5 commit 418ab08
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions api/v1/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions worker/detectors/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 418ab08

Please sign in to comment.