rename msg to message for platform.Error json
parent
364b56f4fa
commit
f3bf670706
20
errors.go
20
errors.go
|
@ -50,10 +50,10 @@ const (
|
|||
// Err: err,
|
||||
// }.
|
||||
type Error struct {
|
||||
Code string `json:"code"` // Code is the machine-readable error code.
|
||||
Msg string `json:"message,omitempty"` // Msg is a human-readable message.
|
||||
Op string `json:"op,omitempty"` // Op describes the logical code operation during error.
|
||||
Err error `json:"error,omitempty"` // Err is a stack of additional errors.
|
||||
Code string
|
||||
Msg string
|
||||
Op string
|
||||
Err error
|
||||
}
|
||||
|
||||
// Error implement the error interface by outputing the Code and Err.
|
||||
|
@ -118,10 +118,10 @@ func ErrorMessage(err error) string {
|
|||
|
||||
// errEncode an JSON encoding helper that is needed to handle the recursive stack of errors.
|
||||
type errEncode struct {
|
||||
Code string `json:"code"` // Code is the machine-readable error code.
|
||||
Msg string `json:"msg,omitempty"` // Msg is a human-readable message.
|
||||
Op string `json:"op,omitempty"` // Op describes the logical code operation during error.
|
||||
Err interface{} `json:"err,omitempty"` // Err is a stack of additional errors.
|
||||
Code string `json:"code"` // Code is the machine-readable error code.
|
||||
Msg string `json:"message,omitempty"` // Msg is a human-readable message.
|
||||
Op string `json:"op,omitempty"` // Op describes the logical code operation during error.
|
||||
Err interface{} `json:"error,omitempty"` // Err is a stack of additional errors.
|
||||
}
|
||||
|
||||
// MarshalJSON recursively marshals the stack of Err.
|
||||
|
@ -165,13 +165,13 @@ func decodeInternalError(target interface{}) error {
|
|||
if code, ok := internalErrMap["code"].(string); ok {
|
||||
internalErr.Code = code
|
||||
}
|
||||
if msg, ok := internalErrMap["msg"].(string); ok {
|
||||
if msg, ok := internalErrMap["message"].(string); ok {
|
||||
internalErr.Msg = msg
|
||||
}
|
||||
if op, ok := internalErrMap["op"].(string); ok {
|
||||
internalErr.Op = op
|
||||
}
|
||||
internalErr.Err = decodeInternalError(internalErrMap["err"])
|
||||
internalErr.Err = decodeInternalError(internalErrMap["error"])
|
||||
return internalErr
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -196,7 +196,7 @@ func TestJSON(t *testing.T) {
|
|||
Op: "bolt/FindAuthorizationByID",
|
||||
Msg: fmt.Sprintf("with ID %d", 323),
|
||||
},
|
||||
encoded: `{"code":"not found","msg":"with ID 323","op":"bolt/FindAuthorizationByID"}`,
|
||||
encoded: `{"code":"not found","message":"with ID 323","op":"bolt/FindAuthorizationByID"}`,
|
||||
},
|
||||
{
|
||||
name: "with a third party error",
|
||||
|
@ -205,7 +205,7 @@ func TestJSON(t *testing.T) {
|
|||
Op: "cmd/fluxd.injectDeps",
|
||||
Err: errors.New("empty value"),
|
||||
},
|
||||
encoded: `{"code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","err":"empty value"}`,
|
||||
encoded: `{"code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","error":"empty value"}`,
|
||||
},
|
||||
{
|
||||
name: "with a internal error",
|
||||
|
@ -214,7 +214,7 @@ func TestJSON(t *testing.T) {
|
|||
Op: "cmd/fluxd.injectDeps",
|
||||
Err: &platform.Error{Code: platform.EEmptyValue, Op: "cmd/fluxd.getStrList"},
|
||||
},
|
||||
encoded: `{"code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","err":{"code":"empty value","op":"cmd/fluxd.getStrList"}}`,
|
||||
encoded: `{"code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","error":{"code":"empty value","op":"cmd/fluxd.getStrList"}}`,
|
||||
},
|
||||
{
|
||||
name: "with a deep internal error",
|
||||
|
@ -230,7 +230,7 @@ func TestJSON(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
encoded: `{"code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","err":{"code":"invalid","op":"cmd/fluxd.getStrList","err":{"code":"empty value","err":"an err"}}}`,
|
||||
encoded: `{"code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","error":{"code":"invalid","op":"cmd/fluxd.getStrList","error":{"code":"empty value","error":"an err"}}}`,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
|
|
|
@ -36,7 +36,7 @@ func TestAPIHandler_NotFound(t *testing.T) {
|
|||
body: `
|
||||
{
|
||||
"code": "not found",
|
||||
"msg": "path not found"
|
||||
"message": "path not found"
|
||||
}`,
|
||||
},
|
||||
},
|
||||
|
|
|
@ -174,7 +174,7 @@ func TestMacroService_handleGetMacro(t *testing.T) {
|
|||
wants: wants{
|
||||
statusCode: 400,
|
||||
contentType: "application/json; charset=utf-8",
|
||||
body: `{"code":"invalid","msg":"An internal error has occurred.","err":"id must have a length of 16 bytes"}`,
|
||||
body: `{"code":"invalid","message":"An internal error has occurred.","error":"id must have a length of 16 bytes"}`,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -30,8 +30,14 @@ func TestRouter_NotFound(t *testing.T) {
|
|||
wants wants
|
||||
}{
|
||||
{
|
||||
name: "path not found",
|
||||
fields: fields{},
|
||||
name: "path not found",
|
||||
fields: fields{
|
||||
method: "GET",
|
||||
path: "/ping",
|
||||
handlerFn: func(w http.ResponseWriter, r *http.Request) {
|
||||
encodeResponse(r.Context(), w, http.StatusOK, map[string]string{"message": "pong"})
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
method: "GET",
|
||||
path: "/404",
|
||||
|
@ -42,7 +48,7 @@ func TestRouter_NotFound(t *testing.T) {
|
|||
body: `
|
||||
{
|
||||
"code": "not found",
|
||||
"msg": "path not found"
|
||||
"message": "path not found"
|
||||
}`,
|
||||
},
|
||||
},
|
||||
|
@ -71,7 +77,7 @@ func TestRouter_NotFound(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests[1:] {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
router := NewRouter()
|
||||
router.HandlerFunc(tt.fields.method, tt.fields.path, tt.fields.handlerFn)
|
||||
|
@ -93,7 +99,6 @@ func TestRouter_NotFound(t *testing.T) {
|
|||
if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq {
|
||||
t.Errorf("%q. get\n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -162,8 +167,8 @@ func TestRouter_Panic(t *testing.T) {
|
|||
body: `
|
||||
{
|
||||
"code": "internal error",
|
||||
"msg": "a panic has occurred",
|
||||
"err": "not implemented"
|
||||
"message": "a panic has occurred",
|
||||
"error": "not implemented"
|
||||
}`,
|
||||
},
|
||||
},
|
||||
|
@ -237,7 +242,7 @@ func TestRouter_MethodNotAllowed(t *testing.T) {
|
|||
body: `
|
||||
{
|
||||
"code": "method not allowed",
|
||||
"msg": "allow: GET, OPTIONS"
|
||||
"message": "allow: GET, OPTIONS"
|
||||
}`,
|
||||
},
|
||||
},
|
||||
|
|
|
@ -31,7 +31,7 @@ func TestToStatus(t *testing.T) {
|
|||
Msg: "howdy",
|
||||
},
|
||||
wantCode: codes.Internal,
|
||||
wantMessage: `{"code":"internal error","msg":"howdy","op":"kit/grpc","err":"error"}`,
|
||||
wantMessage: `{"code":"internal error","message":"howdy","op":"kit/grpc","error":"error"}`,
|
||||
},
|
||||
{
|
||||
name: "encode not found error",
|
||||
|
@ -42,7 +42,7 @@ func TestToStatus(t *testing.T) {
|
|||
Msg: "howdy",
|
||||
},
|
||||
wantCode: codes.NotFound,
|
||||
wantMessage: `{"code":"not found","msg":"howdy","op":"kit/grpc","err":"error"}`,
|
||||
wantMessage: `{"code":"not found","message":"howdy","op":"kit/grpc","error":"error"}`,
|
||||
},
|
||||
{
|
||||
name: "encode invalid error",
|
||||
|
@ -53,7 +53,7 @@ func TestToStatus(t *testing.T) {
|
|||
Msg: "howdy",
|
||||
},
|
||||
wantCode: codes.InvalidArgument,
|
||||
wantMessage: `{"code":"invalid","msg":"howdy","op":"kit/grpc","err":"error"}`,
|
||||
wantMessage: `{"code":"invalid","message":"howdy","op":"kit/grpc","error":"error"}`,
|
||||
},
|
||||
{
|
||||
name: "encode unavailable error",
|
||||
|
@ -64,7 +64,7 @@ func TestToStatus(t *testing.T) {
|
|||
Msg: "howdy",
|
||||
},
|
||||
wantCode: codes.Unavailable,
|
||||
wantMessage: `{"code":"unavailable","msg":"howdy","op":"kit/grpc","err":"error"}`,
|
||||
wantMessage: `{"code":"unavailable","message":"howdy","op":"kit/grpc","error":"error"}`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
@ -107,7 +107,7 @@ func TestFromStatus(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "status message with embedded platform error",
|
||||
s: status.New(codes.Internal, `{"code":"unavailable","msg":"howdy","op":"kit/grpc","err":"error"}`),
|
||||
s: status.New(codes.Internal, `{"code":"unavailable","message":"howdy","op":"kit/grpc","error":"error"}`),
|
||||
want: &platform.Error{
|
||||
Err: fmt.Errorf("error"),
|
||||
Code: platform.EUnavailable,
|
||||
|
|
Loading…
Reference in New Issue