// A MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
type MarshalerError struct {
- Type reflect.Type
- Err error
+ Type reflect.Type
+ Err error
+ sourceFunc string
}
func (e *MarshalerError) Error() string {
- return "json: error calling MarshalJSON for type " + e.Type.String() + ": " + e.Err.Error()
+ srcFunc := e.sourceFunc
+ if srcFunc == "" {
+ srcFunc = "MarshalJSON"
+ }
+ return "json: error calling " + srcFunc +
+ " for type " + e.Type.String() +
+ ": " + e.Err.Error()
}
+// Unwrap returns the underlying error.
func (e *MarshalerError) Unwrap() error { return e.Err }
var hex = "0123456789abcdef"
err = compact(&e.Buffer, b, opts.escapeHTML)
}
if err != nil {
- e.error(&MarshalerError{v.Type(), err})
+ e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
}
}
err = compact(&e.Buffer, b, opts.escapeHTML)
}
if err != nil {
- e.error(&MarshalerError{v.Type(), err})
+ e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
}
}
}
b, err := m.MarshalText()
if err != nil {
- e.error(&MarshalerError{v.Type(), err})
+ e.error(&MarshalerError{v.Type(), err, "MarshalText"})
}
e.stringBytes(b, opts.escapeHTML)
}
m := va.Interface().(encoding.TextMarshaler)
b, err := m.MarshalText()
if err != nil {
- e.error(&MarshalerError{v.Type(), err})
+ e.error(&MarshalerError{v.Type(), err, "MarshalText"})
}
e.stringBytes(b, opts.escapeHTML)
}
for i, v := range keys {
sv[i].v = v
if err := sv[i].resolve(); err != nil {
- e.error(&MarshalerError{v.Type(), err})
+ e.error(fmt.Errorf("json: encoding error for type %q: %q", v.Type().String(), err.Error()))
}
}
sort.Slice(sv, func(i, j int) bool { return sv[i].s < sv[j].s })
t.Fatalf("Marshal: got %s want %s", got, want)
}
}
+
+func TestMarshalerError(t *testing.T) {
+ s := "test variable"
+ st := reflect.TypeOf(s)
+ errText := "json: test error"
+
+ tests := []struct {
+ err *MarshalerError
+ want string
+ }{
+ {
+ &MarshalerError{st, fmt.Errorf(errText), ""},
+ "json: error calling MarshalJSON for type " + st.String() + ": " + errText,
+ },
+ {
+ &MarshalerError{st, fmt.Errorf(errText), "TestMarshalerError"},
+ "json: error calling TestMarshalerError for type " + st.String() + ": " + errText,
+ },
+ }
+
+ for i, tt := range tests {
+ got := tt.err.Error()
+ if got != tt.want {
+ t.Errorf("MarshalerError test %d, got: %s, want: %s", i, got, tt.want)
+ }
+ }
+}