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:
parent
5f553b3cfc
commit
f565d6abb7
16 changed files with 444 additions and 405 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue