Move ErrorCode logic to new errcode package

Make HTTP status codes match the ErrorCode by looking it up in the Descriptors

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis 2015-05-14 18:21:39 -07:00
parent 5f553b3cfc
commit f565d6abb7
16 changed files with 444 additions and 405 deletions

View file

@ -2,6 +2,7 @@ package handlers
import (
"encoding/json"
"github.com/docker/distribution/registry/api/errcode"
"io"
"net/http"
)
@ -11,6 +12,17 @@ import (
// ResponseWriter.WriteHeader before this function.
func serveJSON(w http.ResponseWriter, v interface{}) error {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
sc := http.StatusInternalServerError
if errs, ok := v.(errcode.Errors); ok && errs.Len() > 0 {
sc = errs.Errors[0].Code.Descriptor().HTTPStatusCode
if sc == 0 {
sc = http.StatusInternalServerError
}
}
w.WriteHeader(sc)
enc := json.NewEncoder(w)
if err := enc.Encode(v); err != nil {