2015-05-15 01:21:39 +00:00
|
|
|
package errcode
|
2014-12-10 05:25:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-05-27 01:16:45 +00:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
2014-12-10 05:25:54 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestErrorCodes ensures that error code format, mappings and
|
|
|
|
// marshaling/unmarshaling. round trips are stable.
|
|
|
|
func TestErrorCodes(t *testing.T) {
|
2015-05-27 01:16:45 +00:00
|
|
|
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)
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 01:16:45 +00:00
|
|
|
if idToDescriptors[desc.Value].Code != ec {
|
|
|
|
t.Fatalf("error code in idToDesc isn't correct, %q != %q", idToDescriptors[desc.Value].Code, ec)
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 01:16:45 +00:00
|
|
|
if ec.Message() != desc.Message {
|
|
|
|
t.Fatalf("ec.Message doesn't mtach desc.Message: %q != %q", ec.Message(), desc.Message)
|
|
|
|
}
|
2014-12-10 05:25:54 +00:00
|
|
|
|
2015-05-27 01:16:45 +00:00
|
|
|
// Test (de)serializing the ErrorCode
|
|
|
|
p, err := json.Marshal(ec)
|
2014-12-10 05:25:54 +00:00
|
|
|
if err != nil {
|
2015-05-27 01:16:45 +00:00
|
|
|
t.Fatalf("couldn't marshal ec %v: %v", ec, err)
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(p) <= 0 {
|
2015-05-27 01:16:45 +00:00
|
|
|
t.Fatalf("expected content in marshaled before for error code %v", ec)
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// First, unmarshal to interface and ensure we have a string.
|
|
|
|
var ecUnspecified interface{}
|
|
|
|
if err := json.Unmarshal(p, &ecUnspecified); err != nil {
|
2015-05-27 01:16:45 +00:00
|
|
|
t.Fatalf("error unmarshaling error code %v: %v", ec, err)
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := ecUnspecified.(string); !ok {
|
2015-05-27 01:16:45 +00:00
|
|
|
t.Fatalf("expected a string for error code %v on unmarshal got a %T", ec, ecUnspecified)
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now, unmarshal with the error code type and ensure they are equal
|
|
|
|
var ecUnmarshaled ErrorCode
|
|
|
|
if err := json.Unmarshal(p, &ecUnmarshaled); err != nil {
|
2015-05-27 01:16:45 +00:00
|
|
|
t.Fatalf("error unmarshaling error code %v: %v", ec, err)
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 01:16:45 +00:00
|
|
|
if ecUnmarshaled != ec {
|
|
|
|
t.Fatalf("unexpected error code during error code marshal/unmarshal: %v != %v", ecUnmarshaled, ec)
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-27 01:16:45 +00:00
|
|
|
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestErrorsManagement does a quick check of the Errors type to ensure that
|
|
|
|
// members are properly pushed and marshaled.
|
2015-05-27 01:16:45 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
2014-12-10 05:25:54 +00:00
|
|
|
func TestErrorsManagement(t *testing.T) {
|
|
|
|
var errs Errors
|
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
errs = append(errs, ErrorCodeTest1)
|
|
|
|
errs = append(errs, ErrorCodeTest2.WithDetail(
|
2015-05-27 01:16:45 +00:00
|
|
|
map[string]interface{}{"digest": "sometestblobsumdoesntmatter"}))
|
2014-12-10 05:25:54 +00:00
|
|
|
|
|
|
|
p, err := json.Marshal(errs)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error marashaling errors: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-03 13:52:39 +00:00
|
|
|
expectedJSON := "[{\"code\":\"TEST1\",\"message\":\"test error 1\"},{\"code\":\"TEST2\",\"message\":\"test error 2\",\"detail\":{\"digest\":\"sometestblobsumdoesntmatter\"}}]"
|
2014-12-10 05:25:54 +00:00
|
|
|
|
|
|
|
if string(p) != expectedJSON {
|
2015-06-03 13:52:39 +00:00
|
|
|
t.Fatalf("unexpected json:\ngot:\n%q\n\nexpected:\n%q", string(p), expectedJSON)
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 01:16:45 +00:00
|
|
|
// 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
|
2015-06-03 13:52:39 +00:00
|
|
|
errs = Errors{ErrorCodeUnknown}
|
|
|
|
expectedJSON = "[{\"code\":\"UNKNOWN\",\"message\":\"unknown error\"}]"
|
2014-12-10 05:25:54 +00:00
|
|
|
p, err = json.Marshal(errs)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error marashaling errors: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(p) != expectedJSON {
|
|
|
|
t.Fatalf("unexpected json: %q != %q", string(p), expectedJSON)
|
|
|
|
}
|
|
|
|
|
2015-05-27 01:16:45 +00:00
|
|
|
// Now test the reverse
|
|
|
|
unmarshaled = nil
|
2014-12-10 05:25:54 +00:00
|
|
|
if err := json.Unmarshal(p, &unmarshaled); err != nil {
|
|
|
|
t.Fatalf("unexpected error unmarshaling error envelope: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-27 01:16:45 +00:00
|
|
|
if !reflect.DeepEqual(unmarshaled, errs) {
|
|
|
|
t.Fatalf("errors not equal after round trip:\nunmarshaled:\n%#v\n\nerrs:\n%#v", unmarshaled, errs)
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|
2015-05-27 01:16:45 +00:00
|
|
|
|
2014-12-10 05:25:54 +00:00
|
|
|
}
|