api/worker: adjust error codes in postLayer
This commit is contained in:
parent
136b907050
commit
e78d076d02
@ -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)
|
||||
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 {
|
||||
@ -122,8 +117,7 @@ func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx
|
||||
return postLayerRoute, statusUnprocessableEntity
|
||||
}
|
||||
|
||||
_, badreq := err.(*cerrors.ErrBadRequest)
|
||||
if badreq || err == utils.ErrCouldNotExtract || err == utils.ErrExtractedFileTooBig {
|
||||
if _, badreq := err.(*cerrors.ErrBadRequest); badreq {
|
||||
writeResponse(w, r, http.StatusBadRequest, LayerEnvelope{Error: &Error{err.Error()}})
|
||||
return postLayerRoute, http.StatusBadRequest
|
||||
}
|
||||
|
@ -42,6 +42,9 @@ var (
|
||||
dataDetectors = make(map[string]DataDetector)
|
||||
|
||||
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
|
||||
@ -73,20 +76,17 @@ func DetectData(path string, format string, toExtract []string, maxFileSize int6
|
||||
r, err := http.Get(path)
|
||||
if err != nil {
|
||||
log.Warningf("could not download layer: %s", err)
|
||||
return nil, cerrors.ErrCouldNotDownload
|
||||
}
|
||||
if r.StatusCode == 404 {
|
||||
return nil, cerrors.ErrNotFound
|
||||
return nil, ErrCouldNotFindLayer
|
||||
}
|
||||
if math.Floor(float64(r.StatusCode/100)) != 2 {
|
||||
log.Warningf("could not download layer: got status code %d, expected 2XX", r.StatusCode)
|
||||
return nil, cerrors.ErrCouldNotDownload
|
||||
return nil, ErrCouldNotFindLayer
|
||||
}
|
||||
layerReader = r.Body
|
||||
} else {
|
||||
layerReader, err = os.Open(path)
|
||||
if err != nil {
|
||||
return nil, cerrors.ErrNotFound
|
||||
return nil, ErrCouldNotFindLayer
|
||||
}
|
||||
}
|
||||
defer layerReader.Close()
|
||||
|
Loading…
Reference in New Issue
Block a user