Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: improve error codes for internal server errors #25678

Merged
merged 19 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions rpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const (
errcodeNotificationsUnsupported = -32001
errcodePanic = -32603
errcodeMarshalError = -32603
errorcodeInternalServerError = -32603
)

type methodNotFoundError struct{ method string }
Expand Down
9 changes: 8 additions & 1 deletion rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,14 @@ func (h *handler) handleSubscribe(cp *callProc, msg *jsonrpcMessage) *jsonrpcMes
func (h *handler) runMethod(ctx context.Context, msg *jsonrpcMessage, callb *callback, args []reflect.Value) *jsonrpcMessage {
result, err := callb.call(ctx, msg.Method, args)
if err != nil {
return msg.errorResponse(err)
if ec, ok := err.(Error); ok {
return msg.errorResponse(ec)
} else {
return msg.errorResponse(&internalServerError{
code: errorcodeInternalServerError,
message: err.Error(),
})
}
niczy marked this conversation as resolved.
Show resolved Hide resolved
}
return msg.response(result)
}
Expand Down
9 changes: 3 additions & 6 deletions rpc/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (msg *jsonrpcMessage) String() string {
return string(b)
}

func (msg *jsonrpcMessage) errorResponse(err error) *jsonrpcMessage {
func (msg *jsonrpcMessage) errorResponse(err Error) *jsonrpcMessage {
resp := errorMessage(err)
resp.ID = msg.ID
return resp
Expand All @@ -109,15 +109,12 @@ func (msg *jsonrpcMessage) response(result interface{}) *jsonrpcMessage {
return &jsonrpcMessage{Version: vsn, ID: msg.ID, Result: enc}
}

func errorMessage(err error) *jsonrpcMessage {
func errorMessage(err Error) *jsonrpcMessage {
msg := &jsonrpcMessage{Version: vsn, ID: null, Error: &jsonError{
Code: errcodeDefault,
Message: err.Error(),
}}
ec, ok := err.(Error)
if ok {
msg.Error.Code = ec.ErrorCode()
}
msg.Error.Code = err.ErrorCode()
de, ok := err.(DataError)
if ok {
msg.Error.Data = de.ErrorData()
Expand Down
2 changes: 1 addition & 1 deletion rpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestServerRegisterName(t *testing.T) {
t.Fatalf("Expected service calc to be registered")
}

wantCallbacks := 12
wantCallbacks := 13
if len(svc.callbacks) != wantCallbacks {
t.Errorf("Expected %d callbacks for service 'service', got %d", wantCallbacks, len(svc.callbacks))
}
Expand Down
3 changes: 3 additions & 0 deletions rpc/testdata/internal-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

--> {"jsonrpc":"2.0","id":2,"method":"test_panic","params": []}
<-- {"jsonrpc":"2.0","id":2,"error":{"code":-32603,"message":"method handler crashed"}}

--> {"jsonrpc":"2.0","id":2,"method":"test_returnInternalError","params": []}
<-- {"jsonrpc":"2.0","id":2,"error":{"code":-32603,"message":"service internal error"}}
4 changes: 4 additions & 0 deletions rpc/testservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (s *testService) ReturnError() error {
return testError{}
}

func (s *testService) ReturnInternalError() error {
return errors.New("service internal error")
}

func (s *testService) MarshalError() *MarshalErrObj {
return &MarshalErrObj{}
}
Expand Down