Skip to content

Commit

Permalink
api/worker: adjust error codes in postLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-M authored and jzelinskie committed Feb 24, 2016
1 parent 136b907 commit e78d076
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
8 changes: 1 addition & 7 deletions api/v1/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,14 @@ 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 {
if _, badreq := err.(*cerrors.ErrBadRequest); badreq {
writeResponse(w, r, http.StatusBadRequest, LayerEnvelope{Error: &Error{err.Error()}})
return postLayerRoute, http.StatusBadRequest
}
Expand Down
12 changes: 6 additions & 6 deletions worker/detectors/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e78d076

Please sign in to comment.