fix(influxdb): use influxdb.Error in ID.Decode

pull/13889/head
zhulongcheng 2019-05-11 15:12:15 +08:00
parent e725e1a2bc
commit 3c1e9ee5f3
6 changed files with 19 additions and 20 deletions

View File

@ -711,11 +711,7 @@ func decodePostDashboardCellRequest(ctx context.Context, r *http.Request) (*post
}
if err := req.dashboardID.DecodeFromString(id); err != nil {
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "invalid dashboard id",
Err: err,
}
return nil, err
}
return req, nil

View File

@ -1033,8 +1033,7 @@ func TestService_handlePostDashboardCell(t *testing.T) {
body: `
{
"code": "invalid",
"error": "id must have a length of 16 bytes",
"message": "invalid dashboard id"
"message": "id must have a length of 16 bytes"
}`,
},
},

View File

@ -174,10 +174,7 @@ func requestVariableID(ctx context.Context) (platform.ID, error) {
id, err := platform.IDFromString(urlID)
if err != nil {
return platform.InvalidID(), &platform.Error{
Code: platform.EInvalid,
Msg: err.Error(),
}
return platform.InvalidID(), err
}
return *id, nil

19
id.go
View File

@ -3,7 +3,6 @@ package influxdb
import (
"encoding/binary"
"encoding/hex"
"errors"
"reflect"
"strconv"
"unsafe"
@ -12,11 +11,19 @@ import (
// IDLength is the exact length a string (or a byte slice representing it) must have in order to be decoded into a valid ID.
const IDLength = 16
// ErrInvalidID signifies invalid IDs.
var ErrInvalidID = errors.New("invalid ID")
var (
// ErrInvalidID signifies invalid IDs.
ErrInvalidID = &Error{
Code: EInvalid,
Msg: "invalid ID",
}
// ErrInvalidIDLength is returned when an ID has the incorrect number of bytes.
var ErrInvalidIDLength = errors.New("id must have a length of 16 bytes")
// ErrInvalidIDLength is returned when an ID has the incorrect number of bytes.
ErrInvalidIDLength = &Error{
Code: EInvalid,
Msg: "id must have a length of 16 bytes",
}
)
// ID is a unique identifier.
//
@ -57,7 +64,7 @@ func (i *ID) Decode(b []byte) error {
res, err := strconv.ParseUint(unsafeBytesToString(b), 16, 64)
if err != nil {
return err
return ErrInvalidID
}
if *i = ID(res); !i.Valid() {

View File

@ -40,7 +40,7 @@ func TestIDFromString(t *testing.T) {
name: "Should not be able to decode a non hex ID",
id: "gggggggggggggggg",
wantErr: true,
err: `strconv.ParseUint: parsing "gggggggggggggggg": invalid syntax`,
err: platform.ErrInvalidID.Error(),
},
{
name: "Should not be able to decode inputs with length less than 16 bytes",

View File

@ -192,7 +192,7 @@ func TestService_SetPassword(t *testing.T) {
password: "howdydoody",
},
wants: wants{
err: fmt.Errorf("kv/setPassword: <internal error> User ID for user1 has been corrupted; Err: invalid ID"),
err: fmt.Errorf("kv/setPassword: <internal error> User ID for user1 has been corrupted; Err: <invalid> invalid ID"),
},
},
{
@ -403,7 +403,7 @@ func TestService_ComparePassword(t *testing.T) {
password: "howdydoody",
},
wants: wants{
err: fmt.Errorf("kv/setPassword: <internal error> User ID for user1 has been corrupted; Err: invalid ID"),
err: fmt.Errorf("kv/setPassword: <internal error> User ID for user1 has been corrupted; Err: <invalid> invalid ID"),
},
},
{