Skip to content

Commit 0ddc50a

Browse files
authored
[to dev/1.3] Print measurement for putting buffer exceptions (#137)
1 parent fad2e86 commit 0ddc50a

2 files changed

Lines changed: 28 additions & 14 deletions

File tree

client/session.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ func (s *Session) genTSInsertRecordReq(deviceId string, time int64,
579579
request.Timestamp = time
580580
request.Measurements = measurements
581581
request.IsAligned = &isAligned
582-
if bys, err := valuesToBytes(types, values); err == nil {
582+
if bys, err := valuesToBytes(types, values, measurements); err == nil {
583583
request.Values = bys
584584
} else {
585585
return nil, err
@@ -665,7 +665,7 @@ func (s *Session) InsertRecordsOfOneDevice(deviceId string, timestamps []int64,
665665

666666
valuesList := make([][]byte, length)
667667
for i := 0; i < length; i++ {
668-
if valuesList[i], err = valuesToBytes(dataTypesSlice[i], valuesSlice[i]); err != nil {
668+
if valuesList[i], err = valuesToBytes(dataTypesSlice[i], valuesSlice[i], measurementsSlice[i]); err != nil {
669669
return nil, err
670670
}
671671
}
@@ -707,7 +707,7 @@ func (s *Session) InsertAlignedRecordsOfOneDevice(deviceId string, timestamps []
707707

708708
valuesList := make([][]byte, length)
709709
for i := 0; i < length; i++ {
710-
if valuesList[i], err = valuesToBytes(dataTypesSlice[i], valuesSlice[i]); err != nil {
710+
if valuesList[i], err = valuesToBytes(dataTypesSlice[i], valuesSlice[i], measurementsSlice[i]); err != nil {
711711
return nil, err
712712
}
713713
}
@@ -952,7 +952,7 @@ func (s *Session) genInsertRecordsReq(deviceIds []string, measurements [][]strin
952952
}
953953
v := make([][]byte, length)
954954
for i := 0; i < len(measurements); i++ {
955-
if bys, err := valuesToBytes(dataTypes[i], values[i]); err == nil {
955+
if bys, err := valuesToBytes(dataTypes[i], values[i], measurements[i]); err == nil {
956956
v[i] = bys
957957
} else {
958958
return nil, err
@@ -962,7 +962,7 @@ func (s *Session) genInsertRecordsReq(deviceIds []string, measurements [][]strin
962962
return &request, nil
963963
}
964964

965-
func valuesToBytes(dataTypes []TSDataType, values []interface{}) ([]byte, error) {
965+
func valuesToBytes(dataTypes []TSDataType, values []interface{}, measurementNames []string) ([]byte, error) {
966966
buff := &bytes.Buffer{}
967967
for i, t := range dataTypes {
968968
binary.Write(buff, binary.BigEndian, byte(t))
@@ -977,35 +977,35 @@ func valuesToBytes(dataTypes []TSDataType, values []interface{}) ([]byte, error)
977977
case bool:
978978
binary.Write(buff, binary.BigEndian, v)
979979
default:
980-
return nil, fmt.Errorf("values[%d] %v(%v) must be bool", i, v, reflect.TypeOf(v))
980+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be bool", measurementNames[i], i, v, reflect.TypeOf(v))
981981
}
982982
case INT32:
983983
switch v.(type) {
984984
case int32:
985985
binary.Write(buff, binary.BigEndian, v)
986986
default:
987-
return nil, fmt.Errorf("values[%d] %v(%v) must be int32", i, v, reflect.TypeOf(v))
987+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be int32", measurementNames[i], i, v, reflect.TypeOf(v))
988988
}
989989
case INT64, TIMESTAMP:
990990
switch v.(type) {
991991
case int64:
992992
binary.Write(buff, binary.BigEndian, v)
993993
default:
994-
return nil, fmt.Errorf("values[%d] %v(%v) must be int64", i, v, reflect.TypeOf(v))
994+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be int64", measurementNames[i], i, v, reflect.TypeOf(v))
995995
}
996996
case FLOAT:
997997
switch v.(type) {
998998
case float32:
999999
binary.Write(buff, binary.BigEndian, v)
10001000
default:
1001-
return nil, fmt.Errorf("values[%d] %v(%v) must be float32", i, v, reflect.TypeOf(v))
1001+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be float32", measurementNames[i], i, v, reflect.TypeOf(v))
10021002
}
10031003
case DOUBLE:
10041004
switch v.(type) {
10051005
case float64:
10061006
binary.Write(buff, binary.BigEndian, v)
10071007
default:
1008-
return nil, fmt.Errorf("values[%d] %v(%v) must be float64", i, v, reflect.TypeOf(v))
1008+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be float64", measurementNames[i], i, v, reflect.TypeOf(v))
10091009
}
10101010
case TEXT, STRING:
10111011
switch s := v.(type) {
@@ -1018,7 +1018,7 @@ func valuesToBytes(dataTypes []TSDataType, values []interface{}) ([]byte, error)
10181018
binary.Write(buff, binary.BigEndian, int32(size))
10191019
binary.Write(buff, binary.BigEndian, s)
10201020
default:
1021-
return nil, fmt.Errorf("values[%d] %v(%v) must be string or []byte", i, v, reflect.TypeOf(v))
1021+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be string or []byte", measurementNames[i], i, v, reflect.TypeOf(v))
10221022
}
10231023
case BLOB:
10241024
switch s := v.(type) {
@@ -1027,7 +1027,7 @@ func valuesToBytes(dataTypes []TSDataType, values []interface{}) ([]byte, error)
10271027
binary.Write(buff, binary.BigEndian, int32(size))
10281028
binary.Write(buff, binary.BigEndian, s)
10291029
default:
1030-
return nil, fmt.Errorf("values[%d] %v(%v) must be []byte", i, v, reflect.TypeOf(v))
1030+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be []byte", measurementNames[i], i, v, reflect.TypeOf(v))
10311031
}
10321032
case DATE:
10331033
switch s := v.(type) {
@@ -1038,10 +1038,10 @@ func valuesToBytes(dataTypes []TSDataType, values []interface{}) ([]byte, error)
10381038
}
10391039
binary.Write(buff, binary.BigEndian, date)
10401040
default:
1041-
return nil, fmt.Errorf("values[%d] %v(%v) must be time.Time", i, v, reflect.TypeOf(v))
1041+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be time.Time", measurementNames[i], i, v, reflect.TypeOf(v))
10421042
}
10431043
default:
1044-
return nil, fmt.Errorf("types[%d] is incorrect, it must in (BOOLEAN, INT32, INT64, FLOAT, DOUBLE, TEXT, TIMESTAMP, BLOB, DATE, STRING)", i)
1044+
return nil, fmt.Errorf("measurement %s types[%d] is incorrect, it must in (BOOLEAN, INT32, INT64, FLOAT, DOUBLE, TEXT, TIMESTAMP, BLOB, DATE, STRING)", measurementNames[i], i)
10451045
}
10461046
}
10471047
return buff.Bytes(), nil

test/e2e/e2e_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ func (s *e2eTestSuite) Test_InsertRecords() {
166166
assert.Equal(status, "Working")
167167
}
168168

169+
func (s *e2eTestSuite) Test_InsertRecordsWithWrongType() {
170+
var (
171+
deviceId = []string{"root.test.d1", "root.test.d2"}
172+
measurements = [][]string{{"s1", "s2"}, {"s1"}}
173+
dataTypes = [][]client.TSDataType{{client.BOOLEAN, client.FLOAT}, {client.BOOLEAN}}
174+
values = [][]interface{}{{100.0, true}, {"aaa"}}
175+
timestamp = []int64{1, 2}
176+
)
177+
_, err := s.session.InsertRecords(deviceId, measurements, dataTypes, values, timestamp)
178+
assert := s.Require()
179+
assert.NotNil(err)
180+
assert.Equal("measurement s1 values[0] 100(float64) must be bool", err.Error())
181+
}
182+
169183
func (s *e2eTestSuite) Test_InsertAlignedRecord() {
170184
var (
171185
deviceId = "root.tsg2.dev1"

0 commit comments

Comments
 (0)