Skip to content

Commit

Permalink
update(http): remove kerrors, change the default to be influxdb error
Browse files Browse the repository at this point in the history
  • Loading branch information
kelwang committed Jan 25, 2019
1 parent 296e119 commit 79ce306
Show file tree
Hide file tree
Showing 23 changed files with 437 additions and 297 deletions.
19 changes: 10 additions & 9 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
// Some error code constant, ideally we want define common platform codes here
// projects on use platform's error, should have their own central place like this.
const (
EInternal = "internal error"
ENotFound = "not found"
EConflict = "conflict" // action cannot be performed
EInvalid = "invalid" // validation failed
EEmptyValue = "empty value"
EUnavailable = "unavailable"
EForbidden = "forbidden"
EUnauthorized = "unauthorized"
EMethodNotAllowed = "method not allowed"
EInternal = "internal error"
ENotFound = "not found"
EConflict = "conflict" // action cannot be performed
EInvalid = "invalid" // validation failed
EUnprocessableEntity = "unprocessable entity" // data type is correct, but out of range
EEmptyValue = "empty value"
EUnavailable = "unavailable"
EForbidden = "forbidden"
EUnauthorized = "unauthorized"
EMethodNotAllowed = "method not allowed"
)

// Error is the error struct of platform.
Expand Down
12 changes: 8 additions & 4 deletions http/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
platform "github.com/influxdata/influxdb"
pcontext "github.com/influxdata/influxdb/context"
"github.com/influxdata/influxdb/inmem"
"github.com/influxdata/influxdb/kit/errors"
"github.com/influxdata/influxdb/mock"

platformtesting "github.com/influxdata/influxdb/testing"
"github.com/julienschmidt/httprouter"
)
Expand Down Expand Up @@ -522,7 +520,10 @@ func TestService_handlePostAuthorization(t *testing.T) {
UserService: &mock.UserService{
FindUserByIDFn: func(ctx context.Context, id platform.ID) (*platform.User, error) {
if !id.Valid() {
return nil, errors.New("invalid user ID")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "invalid user id",
}
}
return &platform.User{
ID: id,
Expand All @@ -533,7 +534,10 @@ func TestService_handlePostAuthorization(t *testing.T) {
OrganizationService: &mock.OrganizationService{
FindOrganizationByIDF: func(ctx context.Context, id platform.ID) (*platform.Organization, error) {
if !id.Valid() {
return nil, errors.New("invalid org ID")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "invalid org ID",
}
}
return &platform.Organization{
ID: id,
Expand Down
42 changes: 32 additions & 10 deletions http/bucket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/kit/errors"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -102,7 +101,10 @@ func (b *bucket) toPlatform() (*platform.Bucket, error) {
if len(b.RetentionRules) > 0 {
d = time.Duration(b.RetentionRules[0].EverySeconds) * time.Second
if d < time.Second {
return nil, errors.InvalidDataf("expiration seconds must be greater than or equal to one second")
return nil, &platform.Error{
Code: platform.EUnprocessableEntity,
Msg: "expiration seconds must be greater than or equal to one second",
}
}
}

Expand Down Expand Up @@ -156,7 +158,10 @@ func (b *bucketUpdate) toPlatform() (*platform.BucketUpdate, error) {
if len(b.RetentionRules) > 0 {
d = time.Duration(b.RetentionRules[0].EverySeconds) * time.Second
if d < time.Second {
return nil, errors.InvalidDataf("expiration seconds must be greater than or equal to one second")
return nil, &platform.Error{
Code: platform.EUnprocessableEntity,
Msg: "expiration seconds must be greater than or equal to one second",
}
}
}

Expand Down Expand Up @@ -324,7 +329,10 @@ func decodeGetBucketRequest(ctx context.Context, r *http.Request) (*getBucketReq
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}

var i platform.ID
Expand Down Expand Up @@ -364,7 +372,10 @@ func decodeDeleteBucketRequest(ctx context.Context, r *http.Request) (*deleteBuc
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}

var i platform.ID
Expand Down Expand Up @@ -480,17 +491,26 @@ func decodePatchBucketRequest(ctx context.Context, r *http.Request) (*patchBucke
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}

var i platform.ID
if err := i.DecodeFromString(id); err != nil {
return nil, err
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: err.Error(),
}
}

bu := &bucketUpdate{}
if err := json.NewDecoder(r.Body).Decode(bu); err != nil {
return nil, err
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: err.Error(),
}
}

upd, err := bu.toPlatform()
Expand Down Expand Up @@ -561,7 +581,6 @@ func (s *BucketService) FindBucket(ctx context.Context, filter platform.BucketFi
Code: platform.ENotFound,
Op: s.OpPrefix + platform.OpFindBucket,
Msg: "bucket not found",
Err: ErrNotFound,
}
}

Expand Down Expand Up @@ -780,7 +799,10 @@ func decodeGetBucketLogRequest(ctx context.Context, r *http.Request) (*getBucket
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}

var i platform.ID
Expand Down
3 changes: 1 addition & 2 deletions http/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,7 @@ func TestService_handlePostBucket(t *testing.T) {
body, _ := ioutil.ReadAll(res.Body)

if res.StatusCode != tt.wants.statusCode {
msg := res.Header.Get(ErrorHeader)
t.Errorf("%q. handlePostBucket() = %v, want %v: %s", tt.name, res.StatusCode, tt.wants.statusCode, msg)
t.Errorf("%q. handlePostBucket() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
}
if tt.wants.contentType != "" && content != tt.wants.contentType {
t.Errorf("%q. handlePostBucket() = %v, want %v", tt.name, content, tt.wants.contentType)
Expand Down
68 changes: 54 additions & 14 deletions http/dashboard_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strconv"

platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/kit/errors"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -246,7 +245,11 @@ func (h *DashboardHandler) handleGetDashboards(w http.ResponseWriter, r *http.Re

mappings, _, err := h.UserResourceMappingService.FindUserResourceMappings(ctx, filter)
if err != nil {
EncodeError(ctx, errors.InternalErrorf("Error loading dashboard owners: %v", err), w)
EncodeError(ctx, &platform.Error{
Code: platform.EInternal,
Msg: "Error loading dashboard owners",
Err: err,
}, w)
return
}

Expand Down Expand Up @@ -349,7 +352,11 @@ func (h *DashboardHandler) handlePostDashboard(w http.ResponseWriter, r *http.Re
return
}
if err := h.DashboardService.CreateDashboard(ctx, req.Dashboard); err != nil {
EncodeError(ctx, errors.InternalErrorf("Error loading dashboards: %v", err), w)
EncodeError(ctx, &platform.Error{
Code: platform.EInternal,
Msg: "Error loading dashboards",
Err: err,
}, w)
return
}

Expand Down Expand Up @@ -409,7 +416,10 @@ func decodeGetDashboardRequest(ctx context.Context, r *http.Request) (*getDashbo
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}

var i platform.ID
Expand Down Expand Up @@ -453,7 +463,10 @@ func decodeGetDashboardLogRequest(ctx context.Context, r *http.Request) (*getDas
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}

var i platform.ID
Expand Down Expand Up @@ -513,7 +526,10 @@ func decodeDeleteDashboardRequest(ctx context.Context, r *http.Request) (*delete
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}

var i platform.ID
Expand Down Expand Up @@ -562,14 +578,20 @@ func decodePatchDashboardRequest(ctx context.Context, r *http.Request) (*patchDa
req := &patchDashboardRequest{}
upd := platform.DashboardUpdate{}
if err := json.NewDecoder(r.Body).Decode(&upd); err != nil {
return nil, errors.MalformedDataf(err.Error())
return nil, &platform.Error{
Code: platform.EInvalid,
Err: err,
}
}
req.Upd = upd

params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}
var i platform.ID
if err := i.DecodeFromString(id); err != nil {
Expand All @@ -579,7 +601,10 @@ func decodePatchDashboardRequest(ctx context.Context, r *http.Request) (*patchDa
req.DashboardID = i

if err := req.Valid(); err != nil {
return nil, errors.MalformedDataf(err.Error())
return nil, &platform.Error{
Code: platform.EInvalid,
Err: err,
}
}

return req, nil
Expand All @@ -588,7 +613,10 @@ func decodePatchDashboardRequest(ctx context.Context, r *http.Request) (*patchDa
// Valid validates that the dashboard ID is non zero valued and update has expected values set.
func (r *patchDashboardRequest) Valid() error {
if !r.DashboardID.Valid() {
return fmt.Errorf("missing dashboard ID")
return &platform.Error{
Code: platform.EInvalid,
Msg: "missing dashboard ID",
}
}

if pe := r.Upd.Valid(); pe != nil {
Expand All @@ -609,7 +637,10 @@ func decodePostDashboardCellRequest(ctx context.Context, r *http.Request) (*post
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}
if err := req.dashboardID.DecodeFromString(id); err != nil {
return nil, err
Expand Down Expand Up @@ -662,7 +693,10 @@ func decodePutDashboardCellRequest(ctx context.Context, r *http.Request) (*putDa
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}
if err := req.dashboardID.DecodeFromString(id); err != nil {
return nil, err
Expand Down Expand Up @@ -708,15 +742,21 @@ func decodeDeleteDashboardCellRequest(ctx context.Context, r *http.Request) (*de
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
return nil, errors.InvalidDataf("url missing id")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing id",
}
}
if err := req.dashboardID.DecodeFromString(id); err != nil {
return nil, err
}

cellID := params.ByName("cellID")
if cellID == "" {
return nil, errors.InvalidDataf("url missing cellID")
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "url missing cellID",
}
}
if err := req.cellID.DecodeFromString(cellID); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 79ce306

Please sign in to comment.