Skip to content

Commit

Permalink
Adjust formatting of legacy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dsnet committed Dec 30, 2024
1 parent 0240acd commit cc4ebba
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions arshal_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ func MarshalFuncV1[T any](fn func(T) ([]byte, error)) *Marshalers {
return collapseSemanticErrors(err)
}
if err := enc.WriteValue(val); err != nil {
if mo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalFuncV1") // unlike unmarshal, always wrapped
}
if isSyntacticError(err) {
err = newMarshalErrorBefore(enc, t, err)
}
Expand Down
3 changes: 3 additions & 0 deletions arshal_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func makeMethodArshaler(fncs *arshaler, t reflect.Type) *arshaler {
return collapseSemanticErrors(err)
}
if err := enc.WriteValue(val); err != nil {
if mo.Flags.Get(jsonflags.ReportLegacyErrorValues) {
return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalJSON") // unlike unmarshal, always wrapped
}
if isSyntacticError(err) {
err = newMarshalErrorBefore(enc, t, err)
}
Expand Down
3 changes: 2 additions & 1 deletion v1/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func transformMarshalError(root any, err error) error {
if err.Err == internal.ErrCycle && err.GoType != nil {
errStr += " via " + err.GoType.String()
}
return &UnsupportedValueError{Str: err.Err.Error()}
errStr = strings.TrimPrefix(errStr, "unsupported value: ")
return &UnsupportedValueError{Str: errStr}
}
} else if ok {
return (*UnsupportedValueError)(nil)
Expand Down

0 comments on commit cc4ebba

Please sign in to comment.