Skip to content

Commit

Permalink
Merge pull request #382 from onflow/fxamacker/fix-encoding-error-type
Browse files Browse the repository at this point in the history
Fix error type for external errors during serialization
  • Loading branch information
fxamacker authored Apr 5, 2024
2 parents 92fcde4 + 661c07f commit c6c951d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion array.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,8 @@ func (a *ArrayDataSlab) encodeAsInlined(enc *Encoder) error {

extraDataIndex, err := enc.inlinedExtraData().addArrayExtraData(a.extraData)
if err != nil {
return NewEncodingError(err)
// err is already categorized by InlinedExtraData.addArrayExtraData().
return err
}

if extraDataIndex > maxInlinedExtraDataIndex {
Expand Down
6 changes: 4 additions & 2 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3009,7 +3009,8 @@ func (m *MapDataSlab) encodeAsInlinedMap(enc *Encoder) error {

extraDataIndex, err := enc.inlinedExtraData().addMapExtraData(m.extraData)
if err != nil {
return NewEncodingError(err)
// err is already categorized by InlinedExtraData.addMapExtraData().
return err
}

if extraDataIndex > maxInlinedExtraDataIndex {
Expand Down Expand Up @@ -3070,7 +3071,8 @@ func encodeAsInlinedCompactMap(

extraDataIndex, cachedKeys, err := enc.inlinedExtraData().addCompactMapExtraData(extraData, hkeys, keys)
if err != nil {
return NewEncodingError(err)
// err is already categorized by InlinedExtraData.addCompactMapExtraData().
return err
}

if len(keys) != len(cachedKeys) {
Expand Down
15 changes: 13 additions & 2 deletions typeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (c *compactMapExtraData) Encode(enc *Encoder, encodeTypeInfo encodeTypeInfo
// element 0: map extra data
err = c.mapExtraData.Encode(enc, encodeTypeInfo)
if err != nil {
// err is already categorized by MapExtraData.Encode().
return err
}

Expand Down Expand Up @@ -160,7 +161,8 @@ func (c *compactMapExtraData) Encode(enc *Encoder, encodeTypeInfo encodeTypeInfo
for _, key := range c.keys {
err = key.Encode(enc)
if err != nil {
return NewEncodingError(err)
// Wrap err as external error (if needed) because err is returned by ComparableStorable.Encode().
return wrapErrorfAsExternalErrorIfNeeded(err, "failed to encode key's storable")
}
}

Expand Down Expand Up @@ -195,6 +197,7 @@ func newCompactMapExtraData(
// element 0: map extra data
mapExtraData, err := newMapExtraData(dec, decodeTypeInfo)
if err != nil {
// err is already categorized by newMapExtraData().
return nil, err
}

Expand Down Expand Up @@ -362,6 +365,7 @@ func (ied *InlinedExtraData) Encode(enc *Encoder) error {
return nil
})
if err != nil {
// err is already categorized by ExtraData.Encode().
return err
}
}
Expand Down Expand Up @@ -475,18 +479,21 @@ func newInlinedExtraDataFromData(
case CBORTagInlinedArrayExtraData:
inlinedExtraData[i], err = newArrayExtraData(dec, decodeTypeInfo)
if err != nil {
// err is already categorized by newArrayExtraData().
return nil, nil, err
}

case CBORTagInlinedMapExtraData:
inlinedExtraData[i], err = newMapExtraData(dec, decodeTypeInfo)
if err != nil {
// err is already categorized by newMapExtraData().
return nil, nil, err
}

case CBORTagInlinedCompactMapExtraData:
inlinedExtraData[i], err = newCompactMapExtraData(dec, decodeTypeInfo, decodeStorable)
if err != nil {
// err is already categorized by newCompactMapExtraData().
return nil, nil, err
}

Expand All @@ -504,6 +511,7 @@ func newInlinedExtraDataFromData(
func (ied *InlinedExtraData) addArrayExtraData(data *ArrayExtraData) (int, error) {
encodedTypeInfo, err := getEncodedTypeInfo(data.TypeInfo)
if err != nil {
// err is already categorized by getEncodedTypeInfo().
return 0, err
}

Expand All @@ -528,6 +536,7 @@ func (ied *InlinedExtraData) addArrayExtraData(data *ArrayExtraData) (int, error
func (ied *InlinedExtraData) addMapExtraData(data *MapExtraData) (int, error) {
encodedTypeInfo, err := getEncodedTypeInfo(data.TypeInfo)
if err != nil {
// err is already categorized by getEncodedTypeInfo().
return 0, err
}

Expand All @@ -546,6 +555,7 @@ func (ied *InlinedExtraData) addCompactMapExtraData(

encodedTypeInfo, err := getEncodedTypeInfo(data.TypeInfo)
if err != nil {
// err is already categorized by getEncodedTypeInfo().
return 0, nil, err
}

Expand Down Expand Up @@ -644,7 +654,8 @@ func getEncodedTypeInfo(ti TypeInfo) (string, error) {
enc := cbor.NewStreamEncoder(b)
err := ti.Encode(enc)
if err != nil {
return "", err
// Wrap err as external error (if needed) because err is returned by TypeInfo.Encode().
return "", wrapErrorfAsExternalErrorIfNeeded(err, "failed to encode type info")
}
enc.Flush()

Expand Down

0 comments on commit c6c951d

Please sign in to comment.