Skip to content

Commit

Permalink
Reduce extensive error checking (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer authored Dec 28, 2023
1 parent aff012f commit fd44931
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 292 deletions.
10 changes: 7 additions & 3 deletions pkg/edition/java/proto/codec/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,13 @@ func (d *Decoder) decodePayload(p []byte) (ctx *proto.PacketContext, err error)
}

// Packet is known, decode data into it.
if err = ctx.Packet.Decode(ctx, payload); err != nil {
if err == io.EOF { // payload was too short or decoder has a bug
err = io.ErrUnexpectedEOF
err = util.RecoverFunc(func() error {
return ctx.Packet.Decode(ctx, payload)
})
if err != nil {
if errors.Is(err, io.EOF) {
// payload was too short or packet decoder has a bug
err = errors.Join(err, io.ErrUnexpectedEOF)
}
return ctx, errs.NewSilentErr("error decoding packet (type: %T, id: %s, protocol: %s, direction: %s): %w",
ctx.Packet, ctx.PacketID, ctx.Protocol, ctx.Direction, err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/edition/java/proto/codec/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func (e *Encoder) WritePacket(packet proto.Packet) (n int, err error) {
Payload: nil,
}

if err = packet.Encode(ctx, buf); err != nil {
if err = util.RecoverFunc(func() error {
return packet.Encode(ctx, buf)
}); err != nil {
return
}

Expand Down
Loading

0 comments on commit fd44931

Please sign in to comment.