refactor errors

This commit is contained in:
2024-10-14 13:29:05 -05:00
parent 1e33a81a2c
commit 39d4a1e7f0
20 changed files with 398 additions and 444 deletions

View File

@@ -1,11 +1,8 @@
package core
import (
"errors"
"fmt"
"github.com/google/uuid"
"go-nkode/hashset"
py "go-nkode/py-builtin"
"go-nkode/util"
)
@@ -31,16 +28,12 @@ func NewCustomer(nkodePolicy NKodePolicy) (*Customer, error) {
func (c *Customer) IsValidNKode(kp KeypadDimension, passcodeAttrIdx []int) error {
nkodeLen := len(passcodeAttrIdx)
if nkodeLen < c.NKodePolicy.MinNkodeLen {
return errors.New(fmt.Sprintf("NKode length %d is too short. Minimum nKode length is %d", nkodeLen, c.NKodePolicy.MinNkodeLen))
if nkodeLen < c.NKodePolicy.MinNkodeLen || nkodeLen > c.NKodePolicy.MaxNkodeLen {
return ErrInvalidNKodeLength
}
validIdx := py.All[int](passcodeAttrIdx, func(i int) bool {
return i >= 0 && i < kp.TotalAttrs()
})
if !validIdx {
return errors.New(fmt.Sprintf("One or more idx out of range 0-%d in IsValidNKode", kp.TotalAttrs()-1))
if validIdx := kp.ValidateAttributeIndices(passcodeAttrIdx); !validIdx {
return ErrInvalidNKodeIdx
}
passcodeSetVals := make(hashset.Set[uint64])
passcodeAttrVals := make(hashset.Set[uint64])
@@ -59,33 +52,32 @@ func (c *Customer) IsValidNKode(kp KeypadDimension, passcodeAttrIdx []int) error
}
if passcodeSetVals.Size() < c.NKodePolicy.DistinctSets {
return errors.New(fmt.Sprintf("passcode has two few distinct sets min %d, has %d", c.NKodePolicy.DistinctSets, passcodeSetVals.Size()))
return ErrTooFewDistinctSet
}
if passcodeAttrVals.Size() < c.NKodePolicy.DistinctAttributes {
return errors.New(fmt.Sprintf("passcode has two few distinct attributes min %d, has %d", c.NKodePolicy.DistinctAttributes, passcodeAttrVals.Size()))
return ErrTooFewDistinctAttributes
}
return nil
}
func (c *Customer) RenewKeys() ([]uint64, []uint64) {
func (c *Customer) RenewKeys() ([]uint64, []uint64, error) {
oldAttrs := make([]uint64, len(c.Attributes.AttrVals))
oldSets := make([]uint64, len(c.Attributes.SetVals))
copy(oldAttrs, c.Attributes.AttrVals)
copy(oldSets, c.Attributes.SetVals)
err := c.Attributes.Renew()
if err != nil {
panic(err)
if err := c.Attributes.Renew(); err != nil {
return nil, nil, err
}
attrsXor, err := util.XorLists(oldAttrs, c.Attributes.AttrVals)
if err != nil {
panic(err)
return nil, nil, err
}
setXor, err := util.XorLists(oldSets, c.Attributes.SetVals)
if err != nil {
panic(err)
return nil, nil, err
}
return setXor, attrsXor
return setXor, attrsXor, nil
}