Skip to content

Commit 2f7ff76

Browse files
authored
Print measurement for putting buffer exceptions (#136)
1 parent 02e058c commit 2f7ff76

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

client/session.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ func (s *Session) genTSInsertRecordReq(deviceId string, time int64,
667667
request.Timestamp = time
668668
request.Measurements = measurements
669669
request.IsAligned = &isAligned
670-
if bys, err := valuesToBytes(types, values); err == nil {
670+
if bys, err := valuesToBytes(types, values, measurements); err == nil {
671671
request.Values = bys
672672
} else {
673673
return nil, err
@@ -753,7 +753,7 @@ func (s *Session) InsertRecordsOfOneDevice(deviceId string, timestamps []int64,
753753

754754
valuesList := make([][]byte, length)
755755
for i := 0; i < length; i++ {
756-
if valuesList[i], err = valuesToBytes(dataTypesSlice[i], valuesSlice[i]); err != nil {
756+
if valuesList[i], err = valuesToBytes(dataTypesSlice[i], valuesSlice[i], measurementsSlice[i]); err != nil {
757757
return nil, err
758758
}
759759
}
@@ -795,7 +795,7 @@ func (s *Session) InsertAlignedRecordsOfOneDevice(deviceId string, timestamps []
795795

796796
valuesList := make([][]byte, length)
797797
for i := 0; i < length; i++ {
798-
if valuesList[i], err = valuesToBytes(dataTypesSlice[i], valuesSlice[i]); err != nil {
798+
if valuesList[i], err = valuesToBytes(dataTypesSlice[i], valuesSlice[i], measurementsSlice[i]); err != nil {
799799
return nil, err
800800
}
801801
}
@@ -1043,7 +1043,7 @@ func (s *Session) genInsertRecordsReq(deviceIds []string, measurements [][]strin
10431043
}
10441044
v := make([][]byte, length)
10451045
for i := 0; i < len(measurements); i++ {
1046-
if bys, err := valuesToBytes(dataTypes[i], values[i]); err == nil {
1046+
if bys, err := valuesToBytes(dataTypes[i], values[i], measurements[i]); err == nil {
10471047
v[i] = bys
10481048
} else {
10491049
return nil, err
@@ -1053,7 +1053,7 @@ func (s *Session) genInsertRecordsReq(deviceIds []string, measurements [][]strin
10531053
return &request, nil
10541054
}
10551055

1056-
func valuesToBytes(dataTypes []TSDataType, values []interface{}) ([]byte, error) {
1056+
func valuesToBytes(dataTypes []TSDataType, values []interface{}, measurementNames []string) ([]byte, error) {
10571057
buff := &bytes.Buffer{}
10581058
for i, t := range dataTypes {
10591059
binary.Write(buff, binary.BigEndian, byte(t))
@@ -1068,35 +1068,35 @@ func valuesToBytes(dataTypes []TSDataType, values []interface{}) ([]byte, error)
10681068
case bool:
10691069
binary.Write(buff, binary.BigEndian, v)
10701070
default:
1071-
return nil, fmt.Errorf("values[%d] %v(%v) must be bool", i, v, reflect.TypeOf(v))
1071+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be bool", measurementNames[i], i, v, reflect.TypeOf(v))
10721072
}
10731073
case INT32:
10741074
switch v.(type) {
10751075
case int32:
10761076
binary.Write(buff, binary.BigEndian, v)
10771077
default:
1078-
return nil, fmt.Errorf("values[%d] %v(%v) must be int32", i, v, reflect.TypeOf(v))
1078+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be int32", measurementNames[i], i, v, reflect.TypeOf(v))
10791079
}
10801080
case INT64, TIMESTAMP:
10811081
switch v.(type) {
10821082
case int64:
10831083
binary.Write(buff, binary.BigEndian, v)
10841084
default:
1085-
return nil, fmt.Errorf("values[%d] %v(%v) must be int64", i, v, reflect.TypeOf(v))
1085+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be int64", measurementNames[i], i, v, reflect.TypeOf(v))
10861086
}
10871087
case FLOAT:
10881088
switch v.(type) {
10891089
case float32:
10901090
binary.Write(buff, binary.BigEndian, v)
10911091
default:
1092-
return nil, fmt.Errorf("values[%d] %v(%v) must be float32", i, v, reflect.TypeOf(v))
1092+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be float32", measurementNames[i], i, v, reflect.TypeOf(v))
10931093
}
10941094
case DOUBLE:
10951095
switch v.(type) {
10961096
case float64:
10971097
binary.Write(buff, binary.BigEndian, v)
10981098
default:
1099-
return nil, fmt.Errorf("values[%d] %v(%v) must be float64", i, v, reflect.TypeOf(v))
1099+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be float64", measurementNames[i], i, v, reflect.TypeOf(v))
11001100
}
11011101
case TEXT, STRING:
11021102
switch s := v.(type) {
@@ -1109,7 +1109,7 @@ func valuesToBytes(dataTypes []TSDataType, values []interface{}) ([]byte, error)
11091109
binary.Write(buff, binary.BigEndian, int32(size))
11101110
binary.Write(buff, binary.BigEndian, s)
11111111
default:
1112-
return nil, fmt.Errorf("values[%d] %v(%v) must be string or []byte", i, v, reflect.TypeOf(v))
1112+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be string or []byte", measurementNames[i], i, v, reflect.TypeOf(v))
11131113
}
11141114
case BLOB:
11151115
switch s := v.(type) {
@@ -1118,7 +1118,7 @@ func valuesToBytes(dataTypes []TSDataType, values []interface{}) ([]byte, error)
11181118
binary.Write(buff, binary.BigEndian, int32(size))
11191119
binary.Write(buff, binary.BigEndian, s)
11201120
default:
1121-
return nil, fmt.Errorf("values[%d] %v(%v) must be []byte", i, v, reflect.TypeOf(v))
1121+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be []byte", measurementNames[i], i, v, reflect.TypeOf(v))
11221122
}
11231123
case DATE:
11241124
switch s := v.(type) {
@@ -1129,10 +1129,10 @@ func valuesToBytes(dataTypes []TSDataType, values []interface{}) ([]byte, error)
11291129
}
11301130
binary.Write(buff, binary.BigEndian, date)
11311131
default:
1132-
return nil, fmt.Errorf("values[%d] %v(%v) must be time.Time", i, v, reflect.TypeOf(v))
1132+
return nil, fmt.Errorf("measurement %s values[%d] %v(%v) must be time.Time", measurementNames[i], i, v, reflect.TypeOf(v))
11331133
}
11341134
default:
1135-
return nil, fmt.Errorf("types[%d] is incorrect, it must in (BOOLEAN, INT32, INT64, FLOAT, DOUBLE, TEXT, TIMESTAMP, BLOB, DATE, STRING)", i)
1135+
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)
11361136
}
11371137
}
11381138
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)