Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error type for external errors during serialization #382

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading