Skip to content

Commit

Permalink
Fix custom errors in RPC calls and added a few custom codes for commo…
Browse files Browse the repository at this point in the history
…n errors.
  • Loading branch information
cscatolini committed Apr 16, 2018
1 parent d23771c commit 87b8467
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 75 deletions.
14 changes: 11 additions & 3 deletions cluster/nats_rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
package cluster

import (
"errors"
"fmt"
"time"

"github.com/gogo/protobuf/proto"
nats "github.com/nats-io/go-nats"
"github.com/topfreegames/pitaya/config"
"github.com/topfreegames/pitaya/constants"
"github.com/topfreegames/pitaya/errors"
"github.com/topfreegames/pitaya/internal/message"
"github.com/topfreegames/pitaya/protos"
"github.com/topfreegames/pitaya/route"
Expand Down Expand Up @@ -146,8 +146,16 @@ func (ns *NatsRPCClient) Call(
return nil, err
}

if res.Error != "" {
return nil, errors.New(res.Error)
if res.Error != nil {
if res.Error.Code == "" {
res.Error.Code = errors.ErrUnknownCode
}
err := &errors.Error{
Code: res.Error.Code,
Message: res.Error.Msg,
Metadata: res.Error.Metadata,
}
return nil, err
}
return res, nil
}
Expand Down
3 changes: 2 additions & 1 deletion cluster/nats_rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/topfreegames/pitaya/constants"
e "github.com/topfreegames/pitaya/errors"
"github.com/topfreegames/pitaya/helpers"
"github.com/topfreegames/pitaya/internal/message"
"github.com/topfreegames/pitaya/protos"
Expand Down Expand Up @@ -301,7 +302,7 @@ func TestNatsRPCClientCall(t *testing.T) {
expected *protos.Response
err error
}{
{"test_error", &protos.Response{Data: []byte("nok"), Error: "nok"}, nil, errors.New("nok")},
{"test_error", &protos.Response{Data: []byte("nok"), Error: &protos.Error{Msg: "nok"}}, nil, e.NewError(errors.New("nok"), e.ErrUnknownCode)},
{"test_ok", &protos.Response{Data: []byte("ok")}, &protos.Response{Data: []byte("ok")}, nil},
{"test_bad_response", []byte("invalid"), nil, errors.New("unexpected EOF")},
{"test_bad_proto", &protos.Session{ID: 1, Uid: "snap"}, nil, errors.New("proto: bad wiretype for field protos.Response.Data: got wiretype 0, want 2")},
Expand Down
13 changes: 11 additions & 2 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@

package errors

// ErrUnknowCode is a string code representing an unknown error
// ErrUnknownCode is a string code representing an unknown error
// This will be used when no error code is sent by the handler
const ErrUnknowCode = "PIT-000"
const ErrUnknownCode = "PIT-000"

// ErrInternalCode is a string code representing an internal Pitaya error
const ErrInternalCode = "PIT-500"

// ErrNotFoundCode is a string code representing a not found related error
const ErrNotFoundCode = "PIT-404"

// ErrBadRequestCode is a string code representing a bad request related error
const ErrBadRequestCode = "PIT-400"

// Error is an error with a code, message and metadata
type Error struct {
Expand Down
3 changes: 2 additions & 1 deletion examples/demo/cluster/services/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"fmt"

"github.com/topfreegames/pitaya"
"github.com/topfreegames/pitaya/component"
"github.com/topfreegames/pitaya/session"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func (c *Connector) GetSessionData(s *session.Session) (*SessionData, error) {
func (c *Connector) SetSessionData(s *session.Session, data *SessionData) (*Response, error) {
err := s.SetData(data.Data)
if err != nil {
return nil, err
return nil, pitaya.Error(err, "CN-000", map[string]string{"failed": "set data"})
}
return reply(200, "success")
}
Expand Down
17 changes: 7 additions & 10 deletions examples/demo/cluster/services/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package services
import (
"encoding/gob"
"fmt"
"strconv"
"time"

"github.com/google/uuid"
"github.com/topfreegames/pitaya"
"github.com/topfreegames/pitaya/component"
"github.com/topfreegames/pitaya/session"
Expand Down Expand Up @@ -101,10 +101,9 @@ func (r *Room) AfterInit() {

// Entry is the entrypoint
func (r *Room) Entry(s *session.Session, msg []byte) (*JoinResponse, error) {
fakeUID := uuid.New().String() // just use s.ID as uid !!!
err := s.Bind(fakeUID) // binding session uid
err := s.Bind(strconv.Itoa(int(s.ID())))
if err != nil {
return nil, err
return nil, pitaya.Error(err, "RH-000", map[string]string{"failed": "bind"})
}
return &JoinResponse{Result: "ok"}, nil
}
Expand Down Expand Up @@ -155,15 +154,13 @@ func (r *Room) Message(s *session.Session, msg *UserMessage) {
}

// SendRPC sends rpc
func (r *Room) SendRPC(s *session.Session, msg *SendRPCMsg) *RPCResponse {
ret := &RPCResponse{}
func (r *Room) SendRPC(s *session.Session, msg *SendRPCMsg) (*RPCResponse, error) {
ret := &RPCResponse{Msg: "ok"}
err := pitaya.RPCTo(msg.ServerID, msg.Route, ret, msg.Msg)
if err != nil {
ret.Msg = err.Error()
return ret
return nil, pitaya.Error(err, "RPC-000")
}
fmt.Printf("rpc ret: %s\n", ret)
return ret
return ret, nil
}

// MessageRemote just echoes the given message
Expand Down
59 changes: 30 additions & 29 deletions protos/pitaya.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protos/pitaya.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ message Request {

message Response {
bytes Data = 1;
string Error = 2;
Error Error = 2;
}

message Error {
Expand Down
Binary file modified serialize/protobuf/fixtures/TestMarshal/test_ok.golden
Binary file not shown.
6 changes: 3 additions & 3 deletions serialize/protobuf/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestMarshal(t *testing.T) {
raw interface{}
err error
}{
"test_ok": {&protos.Response{Data: []byte("data"), Error: "error"}, nil},
"test_ok": {&protos.Response{Data: []byte("data"), Error: &protos.Error{Msg: "error"}}, nil},
"test_not_a_message": {"invalid", constants.ErrWrongValueType},
}
serializer := NewSerializer()
Expand Down Expand Up @@ -81,8 +81,8 @@ func TestUnmarshal(t *testing.T) {
dest interface{}
err error
}{
"test_ok": {&protos.Response{Data: []byte("data"), Error: "error"}, data, &dest, nil},
"test_invalid_dest": {&protos.Response{Data: []byte(nil), Error: ""}, data, "invalid", constants.ErrWrongValueType},
"test_ok": {&protos.Response{Data: []byte("data"), Error: &protos.Error{Msg: "error"}}, data, &dest, nil},
"test_invalid_dest": {&protos.Response{Data: []byte(nil)}, data, "invalid", constants.ErrWrongValueType},
}
serializer := NewSerializer()

Expand Down
Loading

0 comments on commit 87b8467

Please sign in to comment.