Skip to content

Commit

Permalink
Merge pull request #21 from jxsl13/fix-noop-error-messages
Browse files Browse the repository at this point in the history
improve error messages that were previously overshadowed by read on closed connection error message due to the connection being closed
  • Loading branch information
ChillerDragon authored Sep 16, 2024
2 parents 16e3230 + c6b60b3 commit 492ad84
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
6 changes: 5 additions & 1 deletion snapshot7/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const (
UninitializedTick = -1
)

var (
ErrNoAltSnapInSnapStorage = errors.New("there is no alt snap in the storage")
)

// TODO: do we even need this?
//
// can we just put the snap as is in the map?
Expand Down Expand Up @@ -85,7 +89,7 @@ func (s *Storage) OldestTick() int {

func (s *Storage) altSnapshot() (*Snapshot, error) {
if s.altSnap.snap == nil {
return nil, errors.New("there is no alt snap in the storage")
return nil, ErrNoAltSnapInSnapStorage
}
return s.altSnap.snap, nil
}
Expand Down
1 change: 0 additions & 1 deletion teeworlds7/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func userMsgCallback[T any](userCallbacks []func(T, DefaultAction) error, msg T,
if len(userCallbacks) == 0 {
if defaultAction != nil {
return defaultAction()

}
return nil
}
Expand Down
28 changes: 15 additions & 13 deletions teeworlds7/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ func (client *Client) Connect(serverIp string, serverPort int) error {

func (client *Client) ConnectContext(ctx context.Context, serverIp string, serverPort int) (err error) {
ctx, cancelCause := context.WithCancelCause(ctx)
defer cancelCause(nil) // always cancel

// wait for the reader goroutine to finish execution, before leaving this function scope
var wg sync.WaitGroup
defer wg.Wait()

ch := make(chan []byte, maxPacksize)
var d net.Dialer
conn, err := d.DialContext(ctx, "udp", fmt.Sprintf("%s:%d", serverIp, serverPort))
if err != nil {
return fmt.Errorf("failed to connect to server: %s:%d: %w", serverIp, serverPort, err)
}
client.Conn = conn
defer func() {
// only the first cancelation cause is relevant
// subsequent cancelations will be ignored
Expand All @@ -86,20 +99,9 @@ func (client *Client) ConnectContext(ctx context.Context, serverIp string, serve
err = ctxErr
return
}
}()

// wait for the reader goroutine to finish execution, before leaving this function scope
var wg sync.WaitGroup
defer wg.Wait()

ch := make(chan []byte, maxPacksize)
var d net.Dialer
conn, err := d.DialContext(ctx, "udp", fmt.Sprintf("%s:%d", serverIp, serverPort))
if err != nil {
return fmt.Errorf("failed to connect to server: %s:%d: %w", serverIp, serverPort, err)
}
client.Conn = conn
defer func() {
// close connection after error handling in order not to
// hide the actual cause of the error
closeErr := client.Conn.Close()
if closeErr != nil {
slog.Error("failed to close connection", "error", closeErr)
Expand Down
16 changes: 13 additions & 3 deletions teeworlds7/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ func (client *Client) processSystem(netMsg messages7.NetMessage, response *proto
client.SnapshotStorage.PurgeUntil(deltaTick)

for _, callback := range client.Callbacks.Snapshot {
callback(newFullSnap, nil)
err = callback(newFullSnap, noOpFunc)
if err != nil {
return fmt.Errorf("failed to execute snapshot callback: %w", err)
}
}

client.Game.Input.AckGameTick = msg.GameTick
Expand Down Expand Up @@ -151,7 +154,10 @@ func (client *Client) processSystem(netMsg messages7.NetMessage, response *proto
client.SnapshotStorage.PurgeUntil(deltaTick)

for _, callback := range client.Callbacks.Snapshot {
callback(newFullSnap, nil)
err = callback(newFullSnap, noOpFunc)
if err != nil {
return fmt.Errorf("failed to execute snapshot callback: %w", err)
}
}

client.Game.Input.AckGameTick = msg.GameTick
Expand Down Expand Up @@ -193,7 +199,7 @@ func (client *Client) processSystem(netMsg messages7.NetMessage, response *proto
client.SnapshotStorage.PurgeUntil(deltaTick)

for _, callback := range client.Callbacks.Snapshot {
err = callback(prevSnap, nil)
err = callback(prevSnap, noOpFunc)
if err != nil {
return fmt.Errorf("failed to execute snapshot callback: %w", err)
}
Expand Down Expand Up @@ -255,3 +261,7 @@ func (client *Client) processSystem(netMsg messages7.NetMessage, response *proto
}
return true, nil
}

func noOpFunc() error {
return nil
}

0 comments on commit 492ad84

Please sign in to comment.