diff --git a/common/errors.go b/common/errors.go new file mode 100644 index 0000000..1963bc9 --- /dev/null +++ b/common/errors.go @@ -0,0 +1,27 @@ +package common + +import "errors" + +var ( + ErrFrontendBadIndex = errors.New("incorrect connection index") + ErrBackendBusy = errors.New("backend is busy") +) + +// ShouldNotError panics if the error is not nil. +func ShouldNotError(err error) { + if err != nil { + panic(err) + } +} + +// ShouldNotErrorUnless panics if the error is not nil and not in the allowed list. +func ShouldNotErrorUnless(err error, allowed ...error) { + if err != nil { + for _, a := range allowed { + if err == a { + return + } + } + panic(err) + } +} diff --git a/common/misc.go b/common/misc.go index 1403477..623ca8e 100644 --- a/common/misc.go +++ b/common/misc.go @@ -1,19 +1,14 @@ package common -import "reflect" +import ( + "reflect" +) // MaybeUnused is a helper function to mark variables as used to avoid compiler warnings. func MaybeUnused(v ...interface{}) struct{} { return struct{}{} } -// ShouldNotError panics if the error is not nil. -func ShouldNotError(err error) { - if err != nil { - panic(err) - } -} - // ReverseMap takes a map and returns a new map with the keys and values reversed. func ReverseMap(m interface{}) interface{} { inputType := reflect.TypeOf(m) diff --git a/gpcm/error.go b/gpcm/error.go index 18d8f0c..d2a43f9 100644 --- a/gpcm/error.go +++ b/gpcm/error.go @@ -483,7 +483,7 @@ func (g *GameSpySession) replyError(gpErr GPError) { if err != nil { logging.Error("GPCM", "Failed to send packet:", err) } - common.ShouldNotError(common.CloseConnection(ServerName, g.ConnIndex)) + _ = common.CloseConnection(ServerName, g.ConnIndex) } logging.Event("gpcm_returned_error", map[string]any{ "profile_id": g.User.ProfileId, @@ -506,7 +506,7 @@ func (g *GameSpySession) replyError(gpErr GPError) { if err != nil { logging.Error("GPCM", "Failed to send packet:", err) } - common.ShouldNotError(common.CloseConnection(ServerName, g.ConnIndex)) + _ = common.CloseConnection(ServerName, g.ConnIndex) } logging.Event("gpcm_returned_error", map[string]any{ diff --git a/gpcm/kick.go b/gpcm/kick.go index 0792c53..a9ec775 100644 --- a/gpcm/kick.go +++ b/gpcm/kick.go @@ -27,7 +27,7 @@ func kickPlayer(profileID uint32, reason string) { case "network_error": // No error message - common.ShouldNotError(common.CloseConnection(ServerName, session.ConnIndex)) + _ = common.CloseConnection(ServerName, session.ConnIndex) return } diff --git a/main.go b/main.go index a25a587..fa8a1cc 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "errors" "io" "net" "net/rpc" @@ -555,11 +554,6 @@ func handleConnection(server serverInfo, conn net.Conn, index uint64) { } } -var ( - ErrBadIndex = errors.New("incorrect connection index") - ErrorBusy = errors.New("backend is busy") -) - // RPCFrontendPacket.SendPacket is called by the backend to send a packet to a connection func (r *RPCFrontendPacket) SendPacket(args RPCFrontendPacket, _ *struct{}) error { rpcMutex.Lock() @@ -567,7 +561,7 @@ func (r *RPCFrontendPacket) SendPacket(args RPCFrontendPacket, _ *struct{}) erro conn := connections[args.Server][args.Index] if conn == nil { - return ErrBadIndex + return common.ErrFrontendBadIndex } _, err := (*conn).Write(args.Data) @@ -582,7 +576,7 @@ func (r *RPCFrontendPacket) CloseConnection(args RPCFrontendPacket, _ *struct{}) conn := connections[args.Server][args.Index] if conn == nil { - return ErrBadIndex + return common.ErrFrontendBadIndex } return (*conn).Close() @@ -636,7 +630,7 @@ func (r *RPCFrontendPacket) VerifyState(uuid string, reload *bool) error { rpcMutex.Unlock() logging.Error("FRONTEND", "Failed to verify UUID, backend is active") *reload = false - return ErrorBusy + return common.ErrBackendBusy } if uuid != frontendUuid {