go: encoding/gob: better support for error

Errors created by the errors package are not registered with encoding/gob which is necessary in order to send them as results of remote procedure calls.

By now, returning an error from the standard library through an RPC method will cause that the receiver get an EOF error and the transmitter a gob: type not registered for interface: errors.errorString

My proposal is add an init() function in the errors package to register the struct errorString so then, developers will avoid create its own errors implementation when using RPCs.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Rather than teach gob about one particular way of making errors, a better plan might be to have gob implement a fallback for errors in general. If something implements the error interface, gob could arrange to transmit the string and recover the error value on the other side, automatically. For those who want richer, type-aware semantics for errors, for example as is done in https://github.com/upspin/upspin/tree/master/errors, one can always implement one of the standard marshaling interfaces for that type, which would override the default.

I’ve been thinking about doing this for a while; perhaps the time has come to do something.

@robpike will look into doing something for Go 1.11.

Can you show some code demonstrating where this is needed? Thanks.