encoding/json: use the Error method to marshal an error value? #5161
Comments
See also issue #3353. |
I believe this is the expected behavior of json.Marshal(interfaceValue). According to the documentation, if the argument is an interface value, it is encodes the value of that interface. The implementation of fmt.Errorf uses errors.errorString type. The type is a struct with one unexported member 's' which holds the error. Since the member is unexported, json.Marshal does not care to encode it. A possible workaround is to create a new error type that both satisfies "error" and "json.Marshaler" like http://play.golang.org/p/NrtoYsGzij |
Hi! I hate to be the one who breaks the almost-a-decade silence, but after several years of using golang I've stumbled uppon a piece of code that uses JSON marshaling of an error interface value. Guess, it is a rare scenario. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: