Skip to content

Commit

Permalink
Merge pull request #2114 from zhulongcheng/rename-msg
Browse files Browse the repository at this point in the history
rename msg to message for platform.Error json
  • Loading branch information
desa committed Dec 27, 2018
2 parents 364b56f + f3bf670 commit f5b0bde
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 29 deletions.
20 changes: 10 additions & 10 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion http/api_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAPIHandler_NotFound(t *testing.T) {
body: `
{
"code": "not found",
"msg": "path not found"
"message": "path not found"
}`,
},
},
Expand Down
2 changes: 1 addition & 1 deletion http/macro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`,
},
},
}
Expand Down
21 changes: 13 additions & 8 deletions http/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -42,7 +48,7 @@ func TestRouter_NotFound(t *testing.T) {
body: `
{
"code": "not found",
"msg": "path not found"
"message": "path not found"
}`,
},
},
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

})
}
}
Expand Down Expand Up @@ -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"
}`,
},
},
Expand Down Expand Up @@ -237,7 +242,7 @@ func TestRouter_MethodNotAllowed(t *testing.T) {
body: `
{
"code": "method not allowed",
"msg": "allow: GET, OPTIONS"
"message": "allow: GET, OPTIONS"
}`,
},
},
Expand Down
10 changes: 5 additions & 5 deletions kit/grpc/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f5b0bde

Please sign in to comment.