从自定义json Umarshaller返回的错误缺少上下文

I am writing a function that parses a JSON object. I would like to emit structured error messages that indicate which specific fields have errors in them.

Originally I checked if the error type was *json.UnmarshalTypeError and then retrieved the json tag name from its Field property. Unfortunately this fails if the struct to which I'm unmarshalling the JSON has custom types that implement their own UnmarshalJSON functions. The errors they return are my custom errors and there is no way to determine which field from the struct they came from.

Playground contrasting the built in vs custom error: https://play.golang.org/p/auH3PE7j5H

At this point I'm considering changing to using reflection, unmarshalling the object into a map of json.RawMessage initially and then unmarshalling one field at a time so that I can identify the problematic field(s). Is there any simpler way? This will require me to basically duplicate the internal json package logic to analyze the json tags to figure out which field to unmarshal each raw message into.

json.UnmarshalTypeError is exported, as are all of its fields. There's no reason you can't return this error type from your custom marshalers. In fact, I'd venture this is an intended use of this type!

func (third *Second) UnmarshalJSON(data []byte) error {
    return &json.UnmarshalTypeError{
        // ...
    }
}