Преглед на файлове

goprotobuf: Remove ErrWrongType and improve error messages.

LGTM=nigeltao
R=nigeltao
CC=golang-codereviews
https://codereview.appspot.com/110080043
David Symonds преди 11 години
родител
ревизия
baeae8bcd1
променени са 3 файла, в които са добавени 8 реда и са изтрити 16 реда
  1. 4 7
      proto/all_test.go
  2. 3 8
      proto/decode.go
  3. 1 1
      proto/message_set.go

+ 4 - 7
proto/all_test.go

@@ -1174,13 +1174,10 @@ func TestTypeMismatch(t *testing.T) {
 	// Now Unmarshal it to the wrong type.
 	// Now Unmarshal it to the wrong type.
 	pb2 := initGoTestField()
 	pb2 := initGoTestField()
 	err := o.Unmarshal(pb2)
 	err := o.Unmarshal(pb2)
-	switch err {
-	case ErrWrongType:
-		// fine
-	case nil:
-		t.Error("expected wrong type error, got no error")
-	default:
-		t.Error("expected wrong type error, got", err)
+	if err == nil {
+		t.Error("expected error, got no error")
+	} else if !strings.Contains(err.Error(), "bad wiretype") {
+		t.Error("expected bad wiretype error, got", err)
 	}
 	}
 }
 }
 
 

+ 3 - 8
proto/decode.go

@@ -43,11 +43,6 @@ import (
 	"reflect"
 	"reflect"
 )
 )
 
 
-// ErrWrongType occurs when the wire encoding for the field disagrees with
-// that specified in the type being decoded.  This is usually caused by attempting
-// to convert an encoded protocol buffer into a struct of the wrong type.
-var ErrWrongType = errors.New("proto: field/encoding mismatch: wrong type for field")
-
 // errOverflow is returned when an integer is too large to be represented.
 // errOverflow is returned when an integer is too large to be represented.
 var errOverflow = errors.New("proto: integer overflow")
 var errOverflow = errors.New("proto: integer overflow")
 
 
@@ -363,11 +358,11 @@ func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group
 			if is_group {
 			if is_group {
 				return nil // input is satisfied
 				return nil // input is satisfied
 			}
 			}
-			return ErrWrongType
+			return fmt.Errorf("proto: %s: wiretype end group for non-group", st)
 		}
 		}
 		tag := int(u >> 3)
 		tag := int(u >> 3)
 		if tag <= 0 {
 		if tag <= 0 {
-			return fmt.Errorf("proto: illegal tag %d", tag)
+			return fmt.Errorf("proto: %s: illegal tag %d", st, tag)
 		}
 		}
 		fieldnum, ok := prop.decoderTags.get(tag)
 		fieldnum, ok := prop.decoderTags.get(tag)
 		if !ok {
 		if !ok {
@@ -397,7 +392,7 @@ func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group
 				// a packable field
 				// a packable field
 				dec = p.packedDec
 				dec = p.packedDec
 			} else {
 			} else {
-				err = ErrWrongType
+				err = fmt.Errorf("proto: bad wiretype for field %s.%s: got wiretype %d, want %d", st, st.Field(fieldnum).Name, wire, p.WireType)
 				continue
 				continue
 			}
 			}
 		}
 		}

+ 1 - 1
proto/message_set.go

@@ -127,7 +127,7 @@ func (ms *MessageSet) Marshal(pb Message) error {
 
 
 	mti, ok := pb.(messageTypeIder)
 	mti, ok := pb.(messageTypeIder)
 	if !ok {
 	if !ok {
-		return ErrWrongType // TODO: custom error?
+		return ErrNoMessageTypeId
 	}
 	}
 
 
 	mtid := mti.MessageTypeId()
 	mtid := mti.MessageTypeId()