More consistent return from ErrorCode.Error()
To bring ErrorCode into liine with Go conventions, ErrorCode.Error() now returns the "nice" value of the error code. This ensures error message assembly works similar to commonly used Go conventions when directly using ErrorCode as an error. Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
parent
0189e8c218
commit
7dd03e12bb
@ -25,7 +25,8 @@ func (ec ErrorCode) ErrorCode() ErrorCode {
|
|||||||
|
|
||||||
// Error returns the ID/Value
|
// Error returns the ID/Value
|
||||||
func (ec ErrorCode) Error() string {
|
func (ec ErrorCode) Error() string {
|
||||||
return ec.Descriptor().Value
|
// NOTE(stevvooe): Cannot use message here since it may have unpopulated args.
|
||||||
|
return strings.ToLower(strings.Replace(ec.String(), "_", " ", -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Descriptor returns the descriptor for the error code.
|
// Descriptor returns the descriptor for the error code.
|
||||||
@ -104,9 +105,7 @@ func (e Error) ErrorCode() ErrorCode {
|
|||||||
|
|
||||||
// Error returns a human readable representation of the error.
|
// Error returns a human readable representation of the error.
|
||||||
func (e Error) Error() string {
|
func (e Error) Error() string {
|
||||||
return fmt.Sprintf("%s: %s",
|
return fmt.Sprintf("%s: %s", e.Code.Error(), e.Message)
|
||||||
strings.ToLower(strings.Replace(e.Code.String(), "_", " ", -1)),
|
|
||||||
e.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithDetail will return a new Error, based on the current one, but with
|
// WithDetail will return a new Error, based on the current one, but with
|
||||||
|
@ -4,9 +4,33 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TestErrorsManagement does a quick check of the Errors type to ensure that
|
||||||
|
// members are properly pushed and marshaled.
|
||||||
|
var ErrorCodeTest1 = Register("test.errors", ErrorDescriptor{
|
||||||
|
Value: "TEST1",
|
||||||
|
Message: "test error 1",
|
||||||
|
Description: `Just a test message #1.`,
|
||||||
|
HTTPStatusCode: http.StatusInternalServerError,
|
||||||
|
})
|
||||||
|
|
||||||
|
var ErrorCodeTest2 = Register("test.errors", ErrorDescriptor{
|
||||||
|
Value: "TEST2",
|
||||||
|
Message: "test error 2",
|
||||||
|
Description: `Just a test message #2.`,
|
||||||
|
HTTPStatusCode: http.StatusNotFound,
|
||||||
|
})
|
||||||
|
|
||||||
|
var ErrorCodeTest3 = Register("test.errors", ErrorDescriptor{
|
||||||
|
Value: "TEST3",
|
||||||
|
Message: "Sorry %q isn't valid",
|
||||||
|
Description: `Just a test message #3.`,
|
||||||
|
HTTPStatusCode: http.StatusNotFound,
|
||||||
|
})
|
||||||
|
|
||||||
// TestErrorCodes ensures that error code format, mappings and
|
// TestErrorCodes ensures that error code format, mappings and
|
||||||
// marshaling/unmarshaling. round trips are stable.
|
// marshaling/unmarshaling. round trips are stable.
|
||||||
func TestErrorCodes(t *testing.T) {
|
func TestErrorCodes(t *testing.T) {
|
||||||
@ -56,33 +80,15 @@ func TestErrorCodes(t *testing.T) {
|
|||||||
if ecUnmarshaled != ec {
|
if ecUnmarshaled != ec {
|
||||||
t.Fatalf("unexpected error code during error code marshal/unmarshal: %v != %v", ecUnmarshaled, ec)
|
t.Fatalf("unexpected error code during error code marshal/unmarshal: %v != %v", ecUnmarshaled, ec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expectedErrorString := strings.ToLower(strings.Replace(ec.Descriptor().Value, "_", " ", -1))
|
||||||
|
if ec.Error() != expectedErrorString {
|
||||||
|
t.Fatalf("unexpected return from %v.Error(): %q != %q", ec, ec.Error(), expectedErrorString)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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,
|
|
||||||
})
|
|
||||||
|
|
||||||
var ErrorCodeTest3 = Register("v2.errors", ErrorDescriptor{
|
|
||||||
Value: "TEST3",
|
|
||||||
Message: "Sorry %q isn't valid",
|
|
||||||
Description: `Just a test message #3.`,
|
|
||||||
HTTPStatusCode: http.StatusNotFound,
|
|
||||||
})
|
|
||||||
|
|
||||||
func TestErrorsManagement(t *testing.T) {
|
func TestErrorsManagement(t *testing.T) {
|
||||||
var errs Errors
|
var errs Errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user