Export ServeJSON for serving error codes

This changeset provides a common http handler for serving errcodes. This should
unify http responses across webservices in the face of errors.

Several type assertions have been added, as well, to ensure the error interface
is implemented.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day
2015-07-16 11:35:02 -07:00
parent 92aa378df4
commit 86ffe515df
4 changed files with 65 additions and 44 deletions

View File

@@ -1,43 +1,10 @@
package handlers
import (
"encoding/json"
"io"
"net/http"
"github.com/docker/distribution/registry/api/errcode"
)
// serveJSON marshals v and sets the content-type header to
// 'application/json'. If a different status code is required, call
// 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 && len(errs) > 0 {
if err, ok := errs[0].(errcode.ErrorCoder); ok {
if sc2 := err.ErrorCode().Descriptor().HTTPStatusCode; sc2 != 0 {
sc = sc2
}
}
} else if err, ok := v.(errcode.ErrorCoder); ok {
if sc2 := err.ErrorCode().Descriptor().HTTPStatusCode; sc2 != 0 {
sc = sc2
}
}
w.WriteHeader(sc)
enc := json.NewEncoder(w)
if err := enc.Encode(v); err != nil {
return err
}
return nil
}
// closeResources closes all the provided resources after running the target
// handler.
func closeResources(handler http.Handler, closers ...io.Closer) http.Handler {