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
rpc: use existing test framework for new error code tests
fjl committed Sep 6, 2022
commit b79f4cbc0eaa2611b1733dd9b1df369699ff04af
7 changes: 7 additions & 0 deletions rpc/testdata/internal-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// These tests trigger various 'internal error' conditions.

--> {"jsonrpc":"2.0","id":1,"method":"test_marshalError","params": []}
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"json: error calling MarshalText for type *rpc.MarshalErrObj: marshal error"}}

--> {"jsonrpc":"2.0","id":2,"method":"test_panic","params": []}
<-- {"jsonrpc":"2.0","id":2,"error":{"code":-32603,"message":"method handler crashed"}}
57 changes: 0 additions & 57 deletions rpc/websocket_test.go
Original file line number Diff line number Diff line change
@@ -25,7 +25,6 @@ import (
"net/http/httptest"
"net/http/httputil"
"net/url"
"reflect"
"strings"
"sync/atomic"
"testing"
@@ -228,62 +227,6 @@ func TestClientWebsocketLargeMessage(t *testing.T) {
}
}

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

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

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

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

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 != internalServerErrorCode {
t.Errorf("wrong error code %d, %d expected", jsonerror.Code, internalServerErrorCode)
}
}

func TestClientWebsocketSevered(t *testing.T) {
t.Parallel()