-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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. 😄 Well, result of |
FYI - Had the same issue, expected the My original struct
Implmented MarshalJSON on the Result struct
JSON output ...
{
"score": 25,
"error": "did not qualify"
},
... |
per golang/go#5161 serializing an `error` object to json emits an empty object; this caused deployed-labeler to emit an empty object when erroring. This commit converts all errors to strings before returning to the user to provide more context.
per golang/go#5161 serializing an `error` object to json emits an empty object; this caused deployed-labeler to emit an empty object when erroring. This commit converts all errors to strings before returning to the user to provide more context. Signed-off-by: Tarun Pothulapati <tarun@gitpod.io>
per golang/go#5161 serializing an `error` object to json emits an empty object; this caused deployed-labeler to emit an empty object when erroring. This commit converts all errors to strings before returning to the user to provide more context. Signed-off-by: Tarun Pothulapati <tarun@gitpod.io>
just stumbled on this too. I guess changing it now would be a breaking change |
The |
But I would suggest that any error marshaling in Go has at least this basic structure: |
Hi all, we kicked off a discussion for a possible "encoding/json/v2" package that addresses the spirit of this proposal. I don't think we should provide native support for |
The main use for this status is to send update messages through the WS, in the form of JSON. Marshalling a JSON field into an error object doesn't work in Go, so it's better to send the error message as string. See golang/go#5161
* basic observer framework * fleshing out the tracker status * set up provision for onPeerConnUpdate function * add provision for peer conn state update channel * connects to a valid tracker * added observer channel for announce status * set up provision for onPeerConnUpdate function * add provision for peer conn state update channel * set up Peer Connection status Observers * add missing Observer initialisation * add PeerConn test: connection established * add comment on torrent used for PeerConn testing * merged PeerObserver commits, moved readChannelTimeout * added failure case for AnnounceStatus observer * added Observers factory method * Added Event to AnnounceStatus, with embedded TrackerStatus * AnnounceStatus details and error reporting * state updates must be non-blocking * json annotations and use matching infoHash string * add unit tests on PeerConn Observer status reading * refactor test by doing direct transfer locally * add test and debug log on dropped connection * add provision for test on PeerID * remove unused OnPeerConnUpdate function * change Err field in PeerStatus to string The main use for this status is to send update messages through the WS, in the form of JSON. Marshalling a JSON field into an error object doesn't work in Go, so it's better to send the error message as string. See golang/go#5161 * add PeerID check to test * remove unused method --------- Co-authored-by: Parker Whittle <pwhittle@medicom.us>
The text was updated successfully, but these errors were encountered: