]> Cypherpunks repositories - gostls13.git/commitdiff
gob: generate a better error message in one confusing place
authorRob Pike <r@golang.org>
Wed, 5 Jan 2011 17:28:47 +0000 (09:28 -0800)
committerRob Pike <r@golang.org>
Wed, 5 Jan 2011 17:28:47 +0000 (09:28 -0800)
(with maybe more to come) by printing a human-readable
representation of a remote type.

R=rsc
CC=golang-dev
https://golang.org/cl/3802045

src/pkg/gob/decode.go
src/pkg/gob/type.go

index 5a19b781971627c8abe733b332b914531b7b9064..5791f62f3045b619d7101297d9ae97489c2e9896 100644 (file)
@@ -861,12 +861,22 @@ func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId) bool {
        return true
 }
 
+// typeString returns a human-readable description of the type identified by remoteId.
+func (dec *Decoder) typeString(remoteId typeId) string {
+       if t := idToType[remoteId]; t != nil {
+               // globally known type.
+               return t.string()
+       }
+       return dec.wireType[remoteId].string()
+}
+
+
 func (dec *Decoder) compileSingle(remoteId typeId, rt reflect.Type) (engine *decEngine, err os.Error) {
        engine = new(decEngine)
        engine.instr = make([]decInstr, 1) // one item
        name := rt.String()                // best we can do
        if !dec.compatibleType(rt, remoteId) {
-               return nil, os.ErrorString("gob: wrong type received for local value " + name)
+               return nil, os.ErrorString("gob: wrong type received for local value " + name + ": " + dec.typeString(remoteId))
        }
        op, indir := dec.decOpFor(remoteId, rt, name)
        ovfl := os.ErrorString(`value for "` + name + `" out of range`)
index 1c2b2027ef1367617e676290e4365869c171b001..2ca96ce90d98f52211ce5807f77a4d32223ac1b3 100644 (file)
@@ -392,11 +392,22 @@ type wireType struct {
        mapT    *mapType
 }
 
-func (w *wireType) name() string {
-       if w.structT != nil {
+func (w *wireType) string() string {
+       const unknown = "unknown type"
+       if w == nil {
+               return unknown
+       }
+       switch {
+       case w.arrayT != nil:
+               return w.arrayT.name
+       case w.sliceT != nil:
+               return w.sliceT.name
+       case w.structT != nil:
                return w.structT.name
+       case w.mapT != nil:
+               return w.mapT.name
        }
-       return "unknown"
+       return unknown
 }
 
 type typeInfo struct {