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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: wrap runMethod error with internalServerError
Nicholas Zhao committed Sep 5, 2022
commit 051c61b5e867d4f1e090771a33b7976cba4098ac
9 changes: 6 additions & 3 deletions rpc/errors.go
Original file line number Diff line number Diff line change
@@ -35,8 +35,11 @@ func (err HTTPError) Error() string {

// Error wraps RPC errors, which contain an error code in addition to the message.
type Error interface {
Error() string // returns the message
ErrorCode() int // returns the code
Error() string // returns the message

// returns the code following JSON-RPC 2.0 spec
// https://www.jsonrpc.org/specification
ErrorCode() int
}

// A DataError contains some data in addition to the error message.
@@ -105,7 +108,7 @@ func (e *invalidParamsError) Error() string { return e.message }

type internalServerError struct{ cause error }

func (e *internalServerError) ErrorCode() int { return -32605 }
func (e *internalServerError) ErrorCode() int { return -32603 }

func (e *internalServerError) Error() string {
return fmt.Sprintf("internal server error caused by %s", e.cause.Error())
4 changes: 2 additions & 2 deletions rpc/handler.go
Original file line number Diff line number Diff line change
@@ -353,7 +353,7 @@ func (h *handler) handleCall(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage
// handleSubscribe processes *_subscribe method calls.
func (h *handler) handleSubscribe(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage {
if !h.allowSubscribe {
return msg.errorResponse(ErrNotificationsUnsupported)
return msg.errorResponse(&internalServerError{cause: ErrNotificationsUnsupported})
}

// Subscription method name is first argument.
@@ -387,7 +387,7 @@ 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)
return msg.errorResponse(&internalServerError{cause: err})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not correct. Here, err is the error value returned by the RPC method implementation. When the method returns an error that implements rpc.Error or rpc.DataError, we want to return the code and data contained in err.

However, with this change, the error code is overwritten.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the RPC method returns non rpc.Error, should we wrap it with internalServerError?

}
return msg.response(result)
}
11 changes: 7 additions & 4 deletions rpc/testservice_test.go
Original file line number Diff line number Diff line change
@@ -212,14 +212,17 @@ func (x largeRespService) LargeResp() string {
type MarshalErrObj struct {
}

// invalidMarshalService simulutes services returning invalid object
// for json marshal.
type invalidMarshalService struct{}
// internalErrorService simulutes JSON-RPC internal server errors
type internalErrorService struct{}

func (x invalidMarshalService) InvalidObj() *MarshalErrObj {
func (x internalErrorService) MarshalError() *MarshalErrObj {
return &MarshalErrObj{}
}

func (x internalErrorService) Panic() error {
panic("service panic")
}

func (o *MarshalErrObj) MarshalText() ([]byte, error) {
return nil, errors.New("marshal error")
}
42 changes: 36 additions & 6 deletions rpc/websocket_test.go
Original file line number Diff line number Diff line change
@@ -237,24 +237,54 @@ func TestClientWebsocketInternalMarshalError(t *testing.T) {
defer srv.Stop()
defer httpsrv.Close()

srv.RegisterName("test", invalidMarshalService{})
srv.RegisterName("test", internalErrorService{})

c, err := DialWebsocket(context.Background(), wsURL, "")
if err != nil {
t.Fatal(err)
}

var r string
err = c.Call(&r, "test_invalidObj")
err = c.Call(&r, "test_marshalError")
if err == nil {
t.Fatal("test_invalidObj call should return error")
t.Fatal("test_marshalError call should return error")
}
jsonerror, ok := err.(*jsonError)
if !ok {
t.Fatalf("test_invalidObj should reutrn jsonError, but %v found", reflect.TypeOf(err))
t.Fatalf("test_marshalError should reutrn jsonError, but %v found", reflect.TypeOf(err))
}
if jsonerror.Code != -32605 {
t.Errorf("wrong error code %d, -32605 expected", jsonerror.Code)
if jsonerror.Code != -32603 {
t.Errorf("wrong error code %d, -32603 expected", jsonerror.Code)
}
}

func TestClientWebsocketInternalRPCPanic(t *testing.T) {
var (
srv = NewServer()
httpsrv = httptest.NewServer(srv.WebsocketHandler(nil))
wsURL = "ws:" + strings.TrimPrefix(httpsrv.URL, "http:")
)
defer srv.Stop()
defer httpsrv.Close()

srv.RegisterName("test", internalErrorService{})

c, err := DialWebsocket(context.Background(), wsURL, "")
if err != nil {
t.Fatal(err)
}

var r string
err = c.Call(&r, "test_panic")
if err == nil {
t.Fatal("test_panic call should return error")
}
jsonerror, ok := err.(*jsonError)
if !ok {
t.Fatalf("test_panic should reutrn jsonError, but %v found", reflect.TypeOf(err))
}
if jsonerror.Code != -32603 {
t.Errorf("wrong error code %d, -32603 expected", jsonerror.Code)
}
}