From cc4ebbae2402542e6fc17502fde0fe78a0167f35 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Sun, 29 Dec 2024 21:42:02 -0800 Subject: [PATCH] Adjust formatting of legacy errors --- arshal_funcs.go | 3 +++ arshal_methods.go | 3 +++ v1/inject.go | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/arshal_funcs.go b/arshal_funcs.go index 82d89c5..d1ba39c 100644 --- a/arshal_funcs.go +++ b/arshal_funcs.go @@ -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) } diff --git a/arshal_methods.go b/arshal_methods.go index 81a659a..7b9f33d 100644 --- a/arshal_methods.go +++ b/arshal_methods.go @@ -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) } diff --git a/v1/inject.go b/v1/inject.go index bf20c5c..13bbd1f 100644 --- a/v1/inject.go +++ b/v1/inject.go @@ -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)