mirror of
https://github.com/distribution/distribution.git
synced 2025-09-02 07:25:25 +00:00
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:
@@ -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 {
|
||||
|
Reference in New Issue
Block a user