api/worker: adjust error codes in postLayer

This commit is contained in:
Quentin Machu 2016-02-24 15:39:49 -05:00 committed by Jimmy Zelinskie
parent 136b907050
commit e78d076d02
2 changed files with 7 additions and 13 deletions

View File

@ -110,11 +110,6 @@ 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) err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format)
if err != nil { 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 || if err == utils.ErrCouldNotExtract ||
err == utils.ErrExtractedFileTooBig || err == utils.ErrExtractedFileTooBig ||
err == worker.ErrUnsupported { err == worker.ErrUnsupported {
@ -122,8 +117,7 @@ func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx
return postLayerRoute, statusUnprocessableEntity return postLayerRoute, statusUnprocessableEntity
} }
_, badreq := err.(*cerrors.ErrBadRequest) if _, badreq := err.(*cerrors.ErrBadRequest); badreq {
if badreq || err == utils.ErrCouldNotExtract || err == utils.ErrExtractedFileTooBig {
writeResponse(w, r, http.StatusBadRequest, LayerEnvelope{Error: &Error{err.Error()}}) writeResponse(w, r, http.StatusBadRequest, LayerEnvelope{Error: &Error{err.Error()}})
return postLayerRoute, http.StatusBadRequest return postLayerRoute, http.StatusBadRequest
} }

View File

@ -42,6 +42,9 @@ var (
dataDetectors = make(map[string]DataDetector) dataDetectors = make(map[string]DataDetector)
log = capnslog.NewPackageLogger("github.com/coreos/clair", "detectors") log = capnslog.NewPackageLogger("github.com/coreos/clair", "detectors")
// ErrCouldNotFindLayer is returned when we could not download or open the layer file.
ErrCouldNotFindLayer = cerrors.NewBadRequestError("could not find layer")
) )
// RegisterDataDetector provides a way to dynamically register an implementation of a // RegisterDataDetector provides a way to dynamically register an implementation of a
@ -73,20 +76,17 @@ func DetectData(path string, format string, toExtract []string, maxFileSize int6
r, err := http.Get(path) r, err := http.Get(path)
if err != nil { if err != nil {
log.Warningf("could not download layer: %s", err) log.Warningf("could not download layer: %s", err)
return nil, cerrors.ErrCouldNotDownload return nil, ErrCouldNotFindLayer
}
if r.StatusCode == 404 {
return nil, cerrors.ErrNotFound
} }
if math.Floor(float64(r.StatusCode/100)) != 2 { if math.Floor(float64(r.StatusCode/100)) != 2 {
log.Warningf("could not download layer: got status code %d, expected 2XX", r.StatusCode) log.Warningf("could not download layer: got status code %d, expected 2XX", r.StatusCode)
return nil, cerrors.ErrCouldNotDownload return nil, ErrCouldNotFindLayer
} }
layerReader = r.Body layerReader = r.Body
} else { } else {
layerReader, err = os.Open(path) layerReader, err = os.Open(path)
if err != nil { if err != nil {
return nil, cerrors.ErrNotFound return nil, ErrCouldNotFindLayer
} }
} }
defer layerReader.Close() defer layerReader.Close()