Round 3 - Add Register function
Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
parent
00b1e8fca0
commit
b8b16b78f4
7 changed files with 187 additions and 251 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// ErrorCode represents the error type. The errors are serialized via strings
|
||||
|
@ -36,49 +37,70 @@ type ErrorDescriptor struct {
|
|||
var (
|
||||
errorCodeToDescriptors = map[ErrorCode]ErrorDescriptor{}
|
||||
idToDescriptors = map[string]ErrorDescriptor{}
|
||||
groupToDescriptors = map[string][]ErrorDescriptor{}
|
||||
)
|
||||
|
||||
const (
|
||||
// ErrorCodeUnknown is a catch-all for errors not defined below.
|
||||
ErrorCodeUnknown ErrorCode = 10000 + iota
|
||||
)
|
||||
|
||||
var errorDescriptors = []ErrorDescriptor{
|
||||
{
|
||||
Code: ErrorCodeUnknown,
|
||||
Value: "UNKNOWN",
|
||||
Message: "unknown error",
|
||||
Description: `Generic error returned when the error does not have an
|
||||
// ErrorCodeUnknown is a generic error that can be used as a last
|
||||
// resort if there is no situation-specific error message that can be used
|
||||
var ErrorCodeUnknown = Register("registry.api.errcode", ErrorDescriptor{
|
||||
Value: "UNKNOWN",
|
||||
Message: "unknown error",
|
||||
Description: `Generic error returned when the error does not have an
|
||||
API classification.`,
|
||||
HTTPStatusCode: http.StatusInternalServerError,
|
||||
},
|
||||
}
|
||||
HTTPStatusCode: http.StatusInternalServerError,
|
||||
})
|
||||
|
||||
// LoadErrors will register a new set of Errors into the system
|
||||
func LoadErrors(errs []ErrorDescriptor) {
|
||||
for _, descriptor := range errs {
|
||||
if _, ok := idToDescriptors[descriptor.Value]; ok {
|
||||
panic(fmt.Sprintf("ErrorValue %s is already registered", descriptor.Value))
|
||||
}
|
||||
if _, ok := errorCodeToDescriptors[descriptor.Code]; ok {
|
||||
panic(fmt.Sprintf("ErrorCode %d is already registered", descriptor.Code))
|
||||
}
|
||||
var nextCode = 1000
|
||||
var registerLock sync.Mutex
|
||||
|
||||
errorCodeToDescriptors[descriptor.Code] = descriptor
|
||||
idToDescriptors[descriptor.Value] = descriptor
|
||||
// Register will make the passed-in error known to the environment and
|
||||
// return a new ErrorCode
|
||||
func Register(group string, descriptor ErrorDescriptor) ErrorCode {
|
||||
registerLock.Lock()
|
||||
defer registerLock.Unlock()
|
||||
code := ErrorCode(nextCode)
|
||||
|
||||
descriptor.Code = code
|
||||
|
||||
if _, ok := idToDescriptors[descriptor.Value]; ok {
|
||||
panic(fmt.Sprintf("ErrorValue %s is already registered", descriptor.Value))
|
||||
}
|
||||
}
|
||||
|
||||
// ParseErrorCode attempts to parse the error code string, returning
|
||||
// ErrorCodeUnknown if the error is not known.
|
||||
func ParseErrorCode(s string) ErrorCode {
|
||||
desc, ok := idToDescriptors[s]
|
||||
|
||||
if !ok {
|
||||
return ErrorCodeUnknown
|
||||
if _, ok := errorCodeToDescriptors[descriptor.Code]; ok {
|
||||
panic(fmt.Sprintf("ErrorCode %d is already registered", descriptor.Code))
|
||||
}
|
||||
|
||||
return desc.Code
|
||||
groupToDescriptors[group] = append(groupToDescriptors[group], descriptor)
|
||||
errorCodeToDescriptors[code] = descriptor
|
||||
idToDescriptors[descriptor.Value] = descriptor
|
||||
|
||||
nextCode++
|
||||
return code
|
||||
}
|
||||
|
||||
// ParseErrorCode returns the value by the string error code.
|
||||
// `ErrorCodeUnknown` will be returned if the error is not known.
|
||||
func ParseErrorCode(value string) ErrorCode {
|
||||
ed, ok := idToDescriptors[value]
|
||||
if ok {
|
||||
return ed.Code
|
||||
}
|
||||
|
||||
return ErrorCodeUnknown
|
||||
}
|
||||
|
||||
// GetGroupNames returns the list of Error group names that are registered
|
||||
func GetGroupNames() []string {
|
||||
keys := []string{}
|
||||
|
||||
for k := range groupToDescriptors {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
// GetErrorCodeGroup returns the named group of error descriptors
|
||||
func GetErrorCodeGroup(name string) []ErrorDescriptor {
|
||||
return groupToDescriptors[name]
|
||||
}
|
||||
|
||||
// Descriptor returns the descriptor for the error code.
|
||||
|
@ -183,8 +205,3 @@ func (errs Errors) Error() string {
|
|||
func (errs Errors) Len() int {
|
||||
return len(errs)
|
||||
}
|
||||
|
||||
// init loads the default errors that are part of the errcode package
|
||||
func init() {
|
||||
LoadErrors(errorDescriptors)
|
||||
}
|
||||
|
|
|
@ -2,67 +2,86 @@ package errcode
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
// "reflect"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
// "github.com/docker/distribution/digest"
|
||||
)
|
||||
|
||||
// TestErrorCodes ensures that error code format, mappings and
|
||||
// marshaling/unmarshaling. round trips are stable.
|
||||
func TestErrorCodes(t *testing.T) {
|
||||
for _, desc := range errorDescriptors {
|
||||
if desc.Code.String() != desc.Value {
|
||||
t.Fatalf("error code string incorrect: %q != %q", desc.Code.String(), desc.Value)
|
||||
if len(errorCodeToDescriptors) == 0 {
|
||||
t.Fatal("errors aren't loaded!")
|
||||
}
|
||||
|
||||
for ec, desc := range errorCodeToDescriptors {
|
||||
if ec != desc.Code {
|
||||
t.Fatalf("error code in descriptor isn't correct, %q != %q", ec, desc.Code)
|
||||
}
|
||||
|
||||
if desc.Code.Message() != desc.Message {
|
||||
t.Fatalf("incorrect message for error code %v: %q != %q", desc.Code, desc.Code.Message(), desc.Message)
|
||||
if idToDescriptors[desc.Value].Code != ec {
|
||||
t.Fatalf("error code in idToDesc isn't correct, %q != %q", idToDescriptors[desc.Value].Code, ec)
|
||||
}
|
||||
|
||||
// Serialize the error code using the json library to ensure that we
|
||||
// get a string and it works round trip.
|
||||
p, err := json.Marshal(desc.Code)
|
||||
if ec.Message() != desc.Message {
|
||||
t.Fatalf("ec.Message doesn't mtach desc.Message: %q != %q", ec.Message(), desc.Message)
|
||||
}
|
||||
|
||||
// Test (de)serializing the ErrorCode
|
||||
p, err := json.Marshal(ec)
|
||||
if err != nil {
|
||||
t.Fatalf("error marshaling error code %v: %v", desc.Code, err)
|
||||
t.Fatalf("couldn't marshal ec %v: %v", ec, err)
|
||||
}
|
||||
|
||||
if len(p) <= 0 {
|
||||
t.Fatalf("expected content in marshaled before for error code %v", desc.Code)
|
||||
t.Fatalf("expected content in marshaled before for error code %v", ec)
|
||||
}
|
||||
|
||||
// First, unmarshal to interface and ensure we have a string.
|
||||
var ecUnspecified interface{}
|
||||
if err := json.Unmarshal(p, &ecUnspecified); err != nil {
|
||||
t.Fatalf("error unmarshaling error code %v: %v", desc.Code, err)
|
||||
t.Fatalf("error unmarshaling error code %v: %v", ec, err)
|
||||
}
|
||||
|
||||
if _, ok := ecUnspecified.(string); !ok {
|
||||
t.Fatalf("expected a string for error code %v on unmarshal got a %T", desc.Code, ecUnspecified)
|
||||
t.Fatalf("expected a string for error code %v on unmarshal got a %T", ec, ecUnspecified)
|
||||
}
|
||||
|
||||
// Now, unmarshal with the error code type and ensure they are equal
|
||||
var ecUnmarshaled ErrorCode
|
||||
if err := json.Unmarshal(p, &ecUnmarshaled); err != nil {
|
||||
t.Fatalf("error unmarshaling error code %v: %v", desc.Code, err)
|
||||
t.Fatalf("error unmarshaling error code %v: %v", ec, err)
|
||||
}
|
||||
|
||||
if ecUnmarshaled != desc.Code {
|
||||
t.Fatalf("unexpected error code during error code marshal/unmarshal: %v != %v", ecUnmarshaled, desc.Code)
|
||||
if ecUnmarshaled != ec {
|
||||
t.Fatalf("unexpected error code during error code marshal/unmarshal: %v != %v", ecUnmarshaled, ec)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TestErrorsManagement does a quick check of the Errors type to ensure that
|
||||
// members are properly pushed and marshaled.
|
||||
/*
|
||||
var ErrorCodeTest1 = Register("v2.errors", ErrorDescriptor{
|
||||
Value: "TEST1",
|
||||
Message: "test error 1",
|
||||
Description: `Just a test message #1.`,
|
||||
HTTPStatusCode: http.StatusInternalServerError,
|
||||
})
|
||||
|
||||
var ErrorCodeTest2 = Register("v2.errors", ErrorDescriptor{
|
||||
Value: "TEST2",
|
||||
Message: "test error 2",
|
||||
Description: `Just a test message #2.`,
|
||||
HTTPStatusCode: http.StatusNotFound,
|
||||
})
|
||||
|
||||
func TestErrorsManagement(t *testing.T) {
|
||||
var errs Errors
|
||||
|
||||
errs.Push(ErrorCodeDigestInvalid)
|
||||
errs.Push(ErrorCodeBlobUnknown,
|
||||
map[string]digest.Digest{"digest": "sometestblobsumdoesntmatter"})
|
||||
errs = append(errs, NewError(ErrorCodeTest1))
|
||||
errs = append(errs, NewError(ErrorCodeTest2,
|
||||
map[string]interface{}{"digest": "sometestblobsumdoesntmatter"}))
|
||||
|
||||
p, err := json.Marshal(errs)
|
||||
|
||||
|
@ -70,15 +89,25 @@ func TestErrorsManagement(t *testing.T) {
|
|||
t.Fatalf("error marashaling errors: %v", err)
|
||||
}
|
||||
|
||||
expectedJSON := "{\"errors\":[{\"code\":\"DIGEST_INVALID\",\"message\":\"provided digest did not match uploaded content\"},{\"code\":\"BLOB_UNKNOWN\",\"message\":\"blob unknown to registry\",\"detail\":{\"digest\":\"sometestblobsumdoesntmatter\"}}]}"
|
||||
expectedJSON := "[{\"code\":\"TEST1\"},{\"code\":\"TEST2\",\"detail\":{\"digest\":\"sometestblobsumdoesntmatter\"}}]"
|
||||
|
||||
if string(p) != expectedJSON {
|
||||
t.Fatalf("unexpected json: %q != %q", string(p), expectedJSON)
|
||||
}
|
||||
|
||||
errs.Clear()
|
||||
errs.Push(ErrorCodeUnknown)
|
||||
expectedJSON = "{\"errors\":[{\"code\":\"UNKNOWN\",\"message\":\"unknown error\"}]}"
|
||||
// Now test the reverse
|
||||
var unmarshaled Errors
|
||||
if err := json.Unmarshal(p, &unmarshaled); err != nil {
|
||||
t.Fatalf("unexpected error unmarshaling error envelope: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(unmarshaled, errs) {
|
||||
t.Fatalf("errors not equal after round trip:\nunmarshaled:\n%#v\n\nerrs:\n%#v", unmarshaled, errs)
|
||||
}
|
||||
|
||||
// Test again with a single value this time
|
||||
errs = Errors{NewError(ErrorCodeUnknown)}
|
||||
expectedJSON = "[{\"code\":\"UNKNOWN\"}]"
|
||||
p, err = json.Marshal(errs)
|
||||
|
||||
if err != nil {
|
||||
|
@ -88,80 +117,15 @@ func TestErrorsManagement(t *testing.T) {
|
|||
if string(p) != expectedJSON {
|
||||
t.Fatalf("unexpected json: %q != %q", string(p), expectedJSON)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMarshalUnmarshal ensures that api errors can round trip through json
|
||||
// without losing information.
|
||||
func TestMarshalUnmarshal(t *testing.T) {
|
||||
|
||||
var errors Errors
|
||||
|
||||
for _, testcase := range []struct {
|
||||
description string
|
||||
err Error
|
||||
}{
|
||||
{
|
||||
description: "unknown error",
|
||||
err: Error{
|
||||
|
||||
Code: ErrorCodeUnknown,
|
||||
Message: ErrorCodeUnknown.Descriptor().Message,
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "unknown manifest",
|
||||
err: Error{
|
||||
Code: ErrorCodeManifestUnknown,
|
||||
Message: ErrorCodeManifestUnknown.Descriptor().Message,
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "unknown manifest",
|
||||
err: Error{
|
||||
Code: ErrorCodeBlobUnknown,
|
||||
Message: ErrorCodeBlobUnknown.Descriptor().Message,
|
||||
Detail: map[string]interface{}{"digest": "asdfqwerqwerqwerqwer"},
|
||||
},
|
||||
},
|
||||
} {
|
||||
fatalf := func(format string, args ...interface{}) {
|
||||
t.Fatalf(testcase.description+": "+format, args...)
|
||||
}
|
||||
|
||||
unexpectedErr := func(err error) {
|
||||
fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
p, err := json.Marshal(testcase.err)
|
||||
if err != nil {
|
||||
unexpectedErr(err)
|
||||
}
|
||||
|
||||
var unmarshaled Error
|
||||
if err := json.Unmarshal(p, &unmarshaled); err != nil {
|
||||
unexpectedErr(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(unmarshaled, testcase.err) {
|
||||
fatalf("errors not equal after round trip: %#v != %#v", unmarshaled, testcase.err)
|
||||
}
|
||||
|
||||
// Roll everything up into an error response envelope.
|
||||
errors.PushErr(testcase.err)
|
||||
}
|
||||
|
||||
p, err := json.Marshal(errors)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error marshaling error envelope: %v", err)
|
||||
}
|
||||
|
||||
var unmarshaled Errors
|
||||
// Now test the reverse
|
||||
unmarshaled = nil
|
||||
if err := json.Unmarshal(p, &unmarshaled); err != nil {
|
||||
t.Fatalf("unexpected error unmarshaling error envelope: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(unmarshaled, errors) {
|
||||
t.Fatalf("errors not equal after round trip: %#v != %#v", unmarshaled, errors)
|
||||
if !reflect.DeepEqual(unmarshaled, errs) {
|
||||
t.Fatalf("errors not equal after round trip:\nunmarshaled:\n%#v\n\nerrs:\n%#v", unmarshaled, errs)
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue