zapcodec: chain ErrInsufficientLength into wrappers equivalent

Wrap zapcodec.ErrInsufficientLength via errors.Join with
wrappers.ErrInsufficientLength on every MarshalInto / UnmarshalFrom
exit path so callers asserting on either sentinel continue to match.

This preserves test-suite backward compatibility for any caller that
historically asserted on luxfi/utils/wrappers.ErrInsufficientLength
(e.g. proto/p/state TestGetFeeStateErrors, vms/proposervm/block
TestParseBytes) without forcing edits to those tests.

O(1) errors.Join — no allocation when err is nil.
This commit is contained in:
Hanzo AI
2026-06-06 05:51:34 -07:00
parent b9e41288cb
commit 2704f74fc2
+27 -4
View File
@@ -46,6 +46,7 @@
package zapcodec
import (
"errors"
"fmt"
"reflect"
"sync"
@@ -201,10 +202,10 @@ func (c *codecImpl) MarshalInto(value interface{}, p *wrappers.Packer) error {
p.Bytes = zp.bytes
p.Offset = zp.offset
if p.Err == nil {
p.Err = zp.err
p.Err = chainWrappersErr(zp.err)
}
if err != nil {
return err
return chainWrappersErr(err)
}
return p.Err
}
@@ -221,14 +222,36 @@ func (c *codecImpl) UnmarshalFrom(p *wrappers.Packer, dest interface{}) error {
p.Bytes = zp.bytes
p.Offset = zp.offset
if p.Err == nil {
p.Err = zp.err
p.Err = chainWrappersErr(zp.err)
}
if err != nil {
return err
return chainWrappersErr(err)
}
return p.Err
}
// chainWrappersErr wraps zapcodec sentinel errors so they chain into
// their luxfi/utils/wrappers equivalents for errors.Is compatibility.
//
// Callers that historically asserted on wrappers.ErrInsufficientLength
// (e.g. proto/p/state's TestGetFeeStateErrors,
// vms/proposervm/block's TestParseBytes, vms/platformvm/state's
// TestParseValidator/DelegatorMetadata) continue to match without
// edit. Callers asserting on zapcodec.ErrInsufficientLength directly
// also match — both names are in the chain.
//
// Wrapping is an O(1) errors.Join — no allocation when err is nil.
func chainWrappersErr(err error) error {
if err == nil {
return nil
}
switch {
case errors.Is(err, ErrInsufficientLength):
return errors.Join(wrappers.ErrInsufficientLength, err)
}
return err
}
// Size returns the on-wire size of value, excluding any outer
// version-prefix layer the manager applies.
func (c *codecImpl) Size(value interface{}) (int, error) {