From 69846d052bfdef9b61693e5e4e017cb85c95f3ce Mon Sep 17 00:00:00 2001 From: davidby-influx Date: Thu, 21 Dec 2023 16:01:41 -0800 Subject: [PATCH] fix: regularize error processing --- authorization/storage_authorization.go | 51 +++++--------- kit/platform/errors/errors.go | 13 +++- tenant/storage_bucket.go | 94 +++++++++++++------------- tenant/storage_org.go | 51 +++++++------- tenant/storage_user.go | 49 ++++++++------ 5 files changed, 128 insertions(+), 130 deletions(-) diff --git a/authorization/storage_authorization.go b/authorization/storage_authorization.go index 58791f227ab..9a0b0e9731d 100644 --- a/authorization/storage_authorization.go +++ b/authorization/storage_authorization.go @@ -91,21 +91,16 @@ func (s *Store) CreateAuthorization(ctx context.Context, tx kv.Tx, a *influxdb.A } if err := idx.Put(authIndexKey(a.Token), encodedID); err != nil { - return &errors.Error{ - Code: errors.EInternal, - Err: err, - } + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateAuthorization)) } b, err := tx.Bucket(authBucket) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateAuthorization)) } if err := b.Put(encodedID, v); err != nil { - return &errors.Error{ - Err: err, - } + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateAuthorization)) } return nil @@ -120,7 +115,7 @@ func (s *Store) GetAuthorizationByID(ctx context.Context, tx kv.Tx, id platform. b, err := tx.Bucket(authBucket) if err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindAuthorizationByID)) } v, err := b.Get(encodedID) @@ -129,15 +124,12 @@ func (s *Store) GetAuthorizationByID(ctx context.Context, tx kv.Tx, id platform. } if err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindAuthorizationByID)) } a := &influxdb.Authorization{} if err := decodeAuthorization(v, a); err != nil { - return nil, &errors.Error{ - Code: errors.EInvalid, - Err: err, - } + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindAuthorizationByID)) } return a, nil @@ -155,6 +147,7 @@ func (s *Store) GetAuthorizationByToken(ctx context.Context, tx kv.Tx, token str return nil, &errors.Error{ Code: errors.ENotFound, Msg: "authorization not found", + Op: influxdb.OpFindAuthorizationByToken, } } @@ -163,6 +156,7 @@ func (s *Store) GetAuthorizationByToken(ctx context.Context, tx kv.Tx, token str return nil, &errors.Error{ Code: errors.EInvalid, Err: err, + Op: influxdb.OpFindAuthorizationByToken, } } @@ -182,7 +176,7 @@ func (s *Store) ListAuthorizations(ctx context.Context, tx kv.Tx, f influxdb.Aut return true }) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindAuthorizations)) } return as, nil @@ -226,18 +220,12 @@ func (s *Store) forEachAuthorization(ctx context.Context, tx kv.Tx, pred kv.Curs func (s *Store) UpdateAuthorization(ctx context.Context, tx kv.Tx, id platform.ID, a *influxdb.Authorization) (*influxdb.Authorization, error) { v, err := encodeAuthorization(a) if err != nil { - return nil, &errors.Error{ - Code: errors.EInvalid, - Err: err, - } + return nil, errors.ErrInternalServiceError(err, errors.WithErrorCode(errors.EInvalid), errors.WithErrorOp(influxdb.OpUpdateAuthorization)) } encodedID, err := a.ID.Encode() if err != nil { - return nil, &errors.Error{ - Code: errors.ENotFound, - Err: err, - } + return nil, errors.ErrInternalServiceError(err, errors.WithErrorCode(errors.ENotFound), errors.WithErrorOp(influxdb.OpUpdateAuthorization)) } idx, err := authIndexBucket(tx) @@ -246,21 +234,16 @@ func (s *Store) UpdateAuthorization(ctx context.Context, tx kv.Tx, id platform.I } if err := idx.Put(authIndexKey(a.Token), encodedID); err != nil { - return nil, &errors.Error{ - Code: errors.EInternal, - Err: err, - } + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateAuthorization)) } b, err := tx.Bucket(authBucket) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateAuthorization)) } if err := b.Put(encodedID, v); err != nil { - return nil, &errors.Error{ - Err: err, - } + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateAuthorization)) } return a, nil @@ -286,15 +269,15 @@ func (s *Store) DeleteAuthorization(ctx context.Context, tx kv.Tx, id platform.I b, err := tx.Bucket(authBucket) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteAuthorization)) } if err := idx.Delete([]byte(a.Token)); err != nil { - return errors.ErrInternalServiceError(err) + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteAuthorization)) } if err := b.Delete(encodedID); err != nil { - return errors.ErrInternalServiceError(err) + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteAuthorization)) } return nil diff --git a/kit/platform/errors/errors.go b/kit/platform/errors/errors.go index 692598c6940..c521ad95bef 100644 --- a/kit/platform/errors/errors.go +++ b/kit/platform/errors/errors.go @@ -121,7 +121,9 @@ func WithErrorOp(op string) func(*Error) { // Error implements the error interface by writing out the recursive messages. func (e *Error) Error() string { - if e.Msg != "" && e.Err != nil { + if e == nil { + return "" + } else if e.Msg != "" && e.Err != nil { var b strings.Builder b.WriteString(e.Msg) b.WriteString(": ") @@ -281,6 +283,8 @@ func BoltToInfluxError(err error) error { var e *Error ok := errors.As(err, &e) switch { + case err == nil: + return nil case ok: // Already an Influx error, we are good to go. return e @@ -299,9 +303,12 @@ func BoltToInfluxError(err error) error { } } -func ErrInternalServiceError(err error, options ...func(*Error)) *Error { +func ErrInternalServiceError(err error, options ...func(*Error)) error { var e *Error - if !errors.As(err, &e) { + + if err == nil { + return nil + } else if !errors.As(err, &e) { setters := make([]func(*Error), 0, len(options)+2) // Defaults first, so they can be overridden by arguments. setters = append(setters, WithErrorErr(err), WithErrorCode(EInternal)) diff --git a/tenant/storage_bucket.go b/tenant/storage_bucket.go index 8386c2ecfb6..e1110a3691d 100644 --- a/tenant/storage_bucket.go +++ b/tenant/storage_bucket.go @@ -3,8 +3,9 @@ package tenant import ( "context" "encoding/json" - "github.com/influxdata/influxdb/v2" + errors2 "github.com/influxdata/influxdb/v2/pkg/errors" + "github.com/influxdata/influxdb/v2/kit/platform" "github.com/influxdata/influxdb/v2/kit/platform/errors" "github.com/influxdata/influxdb/v2/kv" @@ -86,7 +87,7 @@ func (s *Store) GetBucket(ctx context.Context, tx kv.Tx, id platform.ID) (*influ b, err := tx.Bucket(bucketBucket) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBucket)) } v, err := b.Get(encodedID) @@ -95,7 +96,7 @@ func (s *Store) GetBucket(ctx context.Context, tx kv.Tx, id platform.ID) (*influ } if err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBucket)) } return unmarshalBucket(v) @@ -104,15 +105,12 @@ func (s *Store) GetBucket(ctx context.Context, tx kv.Tx, id platform.ID) (*influ func (s *Store) GetBucketByName(ctx context.Context, tx kv.Tx, orgID platform.ID, n string) (*influxdb.Bucket, error) { key, err := bucketIndexKey(orgID, n) if err != nil { - return nil, &errors.Error{ - Code: errors.EInvalid, - Err: err, - } + return nil, errors.ErrInternalServiceError(err, errors.WithErrorCode(errors.EInvalid), errors.WithErrorOp(influxdb.OpFindBucket)) } idx, err := tx.Bucket(bucketIndex) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBucket)) } buf, err := idx.Get(key) @@ -123,14 +121,12 @@ func (s *Store) GetBucketByName(ctx context.Context, tx kv.Tx, orgID platform.ID } if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBucket)) } var id platform.ID if err := id.Decode(buf); err != nil { - return nil, &errors.Error{ - Err: err, - } + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBucket)) } return s.GetBucket(ctx, tx, id) } @@ -140,7 +136,7 @@ type BucketFilter struct { OrganizationID *platform.ID } -func (s *Store) ListBuckets(ctx context.Context, tx kv.Tx, filter BucketFilter, opt ...influxdb.FindOptions) ([]*influxdb.Bucket, error) { +func (s *Store) ListBuckets(ctx context.Context, tx kv.Tx, filter BucketFilter, opt ...influxdb.FindOptions) (buckets []*influxdb.Bucket, retErr error) { // this isn't a list action its a `GetBucketByName` if (filter.OrganizationID != nil && filter.OrganizationID.Valid()) && filter.Name != nil { return nil, invalidBucketListRequest @@ -158,7 +154,7 @@ func (s *Store) ListBuckets(ctx context.Context, tx kv.Tx, filter BucketFilter, b, err := tx.Bucket(bucketBucket) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBuckets)) } var opts []kv.CursorOption @@ -177,9 +173,13 @@ func (s *Store) ListBuckets(ctx context.Context, tx kv.Tx, filter BucketFilter, cursor, err := b.ForwardCursor(seek, opts...) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBuckets)) } - defer cursor.Close() + defer errors2.Capture(&retErr, + func() error { + e := cursor.Close() + return errors.ErrInternalServiceError(e, errors.WithErrorOp(influxdb.OpFindBuckets)) + })() count := 0 bs := []*influxdb.Bucket{} @@ -203,22 +203,19 @@ func (s *Store) ListBuckets(ctx context.Context, tx kv.Tx, filter BucketFilter, } } - return bs, cursor.Err() + return bs, errors.ErrInternalServiceError(cursor.Err(), errors.WithErrorOp(influxdb.OpFindBuckets)) } -func (s *Store) listBucketsByOrg(ctx context.Context, tx kv.Tx, orgID platform.ID, o influxdb.FindOptions) ([]*influxdb.Bucket, error) { +func (s *Store) listBucketsByOrg(ctx context.Context, tx kv.Tx, orgID platform.ID, o influxdb.FindOptions) (buckets []*influxdb.Bucket, retErr error) { // get the prefix key (org id with an empty name) key, err := bucketIndexKey(orgID, "") if err != nil { - return nil, &errors.Error{ - Code: errors.EInvalid, - Err: err, - } + return nil, errors.ErrInternalServiceError(err, errors.WithErrorCode(errors.EInvalid), errors.WithErrorOp(influxdb.OpFindBuckets)) } idx, err := tx.Bucket(bucketIndex) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBuckets)) } start := key @@ -242,17 +239,20 @@ func (s *Store) listBucketsByOrg(ctx context.Context, tx kv.Tx, orgID platform.I return lastKey, nil }() if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBuckets)) } // Once we've found the end, walk backwards from it on the next iteration. opts = append(opts, kv.WithCursorDirection(kv.CursorDescending)) } cursor, err := idx.ForwardCursor(start, opts...) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBuckets)) } - defer cursor.Close() - + defer errors2.Capture(&retErr, + func() error { + e := cursor.Close() + return errors.ErrInternalServiceError(e, errors.WithErrorOp(influxdb.OpFindBuckets)) + })() count := 0 bs := []*influxdb.Bucket{} searchingForAfter := o.After != nil @@ -263,14 +263,12 @@ func (s *Store) listBucketsByOrg(ctx context.Context, tx kv.Tx, orgID platform.I } if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBuckets)) } var id platform.ID if err := id.Decode(v); err != nil { - return nil, &errors.Error{ - Err: err, - } + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBuckets)) } if searchingForAfter { searchingForAfter = id != *o.After @@ -278,7 +276,7 @@ func (s *Store) listBucketsByOrg(ctx context.Context, tx kv.Tx, orgID platform.I } b, err := s.GetBucket(ctx, tx, id) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindBuckets)) } bs = append(bs, b) @@ -288,7 +286,7 @@ func (s *Store) listBucketsByOrg(ctx context.Context, tx kv.Tx, orgID platform.I } } - return bs, cursor.Err() + return bs, errors.ErrInternalServiceError(cursor.Err(), errors.WithErrorOp(influxdb.OpFindBuckets)) } func (s *Store) CreateBucket(ctx context.Context, tx kv.Tx, bucket *influxdb.Bucket) (err error) { @@ -304,29 +302,29 @@ func (s *Store) CreateBucket(ctx context.Context, tx kv.Tx, bucket *influxdb.Buc } if err := s.uniqueBucketName(ctx, tx, bucket.OrgID, bucket.Name); err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateBucket)) } bucket.SetCreatedAt(s.now()) bucket.SetUpdatedAt(s.now()) idx, err := tx.Bucket(bucketIndex) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateBucket)) } b, err := tx.Bucket(bucketBucket) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateBucket)) } v, err := marshalBucket(bucket) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateBucket)) } ikey, err := bucketIndexKey(bucket.OrgID, bucket.Name) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateBucket)) } if err := idx.Put(ikey, encodedID); err != nil { @@ -368,26 +366,26 @@ func (s *Store) UpdateBucket(ctx context.Context, tx kv.Tx, id platform.ID, upd idx, err := tx.Bucket(bucketIndex) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateBucket)) } oldIkey, err := bucketIndexKey(bucket.OrgID, bucket.Name) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateBucket)) } if err := idx.Delete(oldIkey); err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateBucket)) } bucket.Name = *upd.Name newIkey, err := bucketIndexKey(bucket.OrgID, bucket.Name) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateBucket)) } if err := idx.Put(newIkey, encodedID); err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateBucket)) } } @@ -431,24 +429,24 @@ func (s *Store) DeleteBucket(ctx context.Context, tx kv.Tx, id platform.ID) erro idx, err := tx.Bucket(bucketIndex) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteBucket)) } ikey, err := bucketIndexKey(bucket.OrgID, bucket.Name) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteBucket)) } if err := idx.Delete(ikey); err != nil { - return errors.ErrInternalServiceError(err) + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteBucket)) } b, err := tx.Bucket(bucketBucket) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteBucket)) } if err := b.Delete(encodedID); err != nil { - return errors.ErrInternalServiceError(err) + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteBucket)) } return nil diff --git a/tenant/storage_org.go b/tenant/storage_org.go index 17f6f547cd7..d2221204aa0 100644 --- a/tenant/storage_org.go +++ b/tenant/storage_org.go @@ -9,6 +9,7 @@ import ( "github.com/influxdata/influxdb/v2/kit/platform" "github.com/influxdata/influxdb/v2/kit/platform/errors" "github.com/influxdata/influxdb/v2/kv" + errors2 "github.com/influxdata/influxdb/v2/pkg/errors" ) var ( @@ -25,7 +26,7 @@ func (s *Store) uniqueOrgName(ctx context.Context, tx kv.Tx, uname string) error idx, err := tx.Bucket(organizationIndex) if err != nil { - return err + return errors.ErrInternalServiceError(err) } _, err = idx.Get(key) @@ -73,7 +74,7 @@ func (s *Store) GetOrg(ctx context.Context, tx kv.Tx, id platform.ID) (*influxdb b, err := tx.Bucket(organizationBucket) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindOrganizationByID)) } v, err := b.Get(encodedID) @@ -82,7 +83,7 @@ func (s *Store) GetOrg(ctx context.Context, tx kv.Tx, id platform.ID) (*influxdb } if err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindOrganizationByID)) } return unmarshalOrg(v) @@ -91,7 +92,7 @@ func (s *Store) GetOrg(ctx context.Context, tx kv.Tx, id platform.ID) (*influxdb func (s *Store) GetOrgByName(ctx context.Context, tx kv.Tx, n string) (*influxdb.Organization, error) { b, err := tx.Bucket(organizationIndex) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindOrganization)) } uid, err := b.Get(organizationIndexKey(n)) @@ -100,7 +101,7 @@ func (s *Store) GetOrgByName(ctx context.Context, tx kv.Tx, n string) (*influxdb } if err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindOrganization)) } var id platform.ID @@ -110,7 +111,7 @@ func (s *Store) GetOrgByName(ctx context.Context, tx kv.Tx, n string) (*influxdb return s.GetOrg(ctx, tx, id) } -func (s *Store) ListOrgs(ctx context.Context, tx kv.Tx, opt ...influxdb.FindOptions) ([]*influxdb.Organization, error) { +func (s *Store) ListOrgs(ctx context.Context, tx kv.Tx, opt ...influxdb.FindOptions) (orgs []*influxdb.Organization, retErr error) { // if we dont have any options it would be irresponsible to just give back all orgs in the system if len(opt) == 0 { opt = append(opt, influxdb.FindOptions{}) @@ -119,14 +120,18 @@ func (s *Store) ListOrgs(ctx context.Context, tx kv.Tx, opt ...influxdb.FindOpti b, err := tx.Bucket(organizationBucket) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindOrganizations)) } cursor, err := b.ForwardCursor(nil) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindOrganizations)) } - defer cursor.Close() + defer errors2.Capture(&retErr, + func() error { + e := cursor.Close() + return errors.ErrInternalServiceError(e, errors.WithErrorOp(influxdb.OpFindOrganizations)) + })() count := 0 us := []*influxdb.Organization{} @@ -147,7 +152,7 @@ func (s *Store) ListOrgs(ctx context.Context, tx kv.Tx, opt ...influxdb.FindOpti } } - return us, cursor.Err() + return us, errors.ErrInternalServiceError(cursor.Err(), errors.WithErrorOp(influxdb.OpFindOrganizations)) } func (s *Store) CreateOrg(ctx context.Context, tx kv.Tx, o *influxdb.Organization) (err error) { @@ -155,7 +160,7 @@ func (s *Store) CreateOrg(ctx context.Context, tx kv.Tx, o *influxdb.Organizatio // generate new bucket ID o.ID, err = s.generateSafeID(ctx, tx, organizationBucket, s.OrgIDGen) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateOrganization)) } encodedID, err := o.ID.Encode() @@ -171,17 +176,17 @@ func (s *Store) CreateOrg(ctx context.Context, tx kv.Tx, o *influxdb.Organizatio o.SetUpdatedAt(s.now()) idx, err := tx.Bucket(organizationIndex) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateOrganization)) } b, err := tx.Bucket(organizationBucket) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateOrganization)) } v, err := marshalOrg(o) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateOrganization)) } if err := idx.Put(organizationIndexKey(o.Name), encodedID); err != nil { @@ -214,17 +219,17 @@ func (s *Store) UpdateOrg(ctx context.Context, tx kv.Tx, id platform.ID, upd inf idx, err := tx.Bucket(organizationIndex) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateOrganization)) } if err := idx.Delete(organizationIndexKey(u.Name)); err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateOrganization)) } u.Name = *upd.Name if err := idx.Put(organizationIndexKey(*upd.Name), encodedID); err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateOrganization)) } } @@ -239,10 +244,10 @@ func (s *Store) UpdateOrg(ctx context.Context, tx kv.Tx, id platform.ID, upd inf b, err := tx.Bucket(organizationBucket) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateOrganization)) } if err := b.Put(encodedID, v); err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateOrganization)) } return u, nil @@ -261,20 +266,20 @@ func (s *Store) DeleteOrg(ctx context.Context, tx kv.Tx, id platform.ID) error { idx, err := tx.Bucket(organizationIndex) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteOrganization)) } if err := idx.Delete([]byte(u.Name)); err != nil { - return errors.ErrInternalServiceError(err) + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteOrganization)) } b, err := tx.Bucket(organizationBucket) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteOrganization)) } if err := b.Delete(encodedID); err != nil { - return errors.ErrInternalServiceError(err) + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteOrganization)) } return nil diff --git a/tenant/storage_user.go b/tenant/storage_user.go index dc3d1a9eec9..19de8628503 100644 --- a/tenant/storage_user.go +++ b/tenant/storage_user.go @@ -8,6 +8,7 @@ import ( "github.com/influxdata/influxdb/v2/kit/platform" "github.com/influxdata/influxdb/v2/kit/platform/errors" "github.com/influxdata/influxdb/v2/kv" + errors2 "github.com/influxdata/influxdb/v2/pkg/errors" ) var ( @@ -39,7 +40,7 @@ func (s *Store) uniqueUserName(tx kv.Tx, uname string) error { idx, err := tx.Bucket(userIndex) if err != nil { - return err + return errors.ErrInternalServiceError(err) } _, err = idx.Get([]byte(uname)) @@ -62,7 +63,7 @@ func (s *Store) uniqueUserID(tx kv.Tx, id platform.ID) error { b, err := tx.Bucket(userBucket) if err != nil { - return err + return errors.ErrInternalServiceError(err) } _, err = b.Get(encodedID) @@ -85,7 +86,7 @@ func (s *Store) GetUser(ctx context.Context, tx kv.Tx, id platform.ID) (*influxd b, err := tx.Bucket(userBucket) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindUserByID)) } v, err := b.Get(encodedID) @@ -94,7 +95,7 @@ func (s *Store) GetUser(ctx context.Context, tx kv.Tx, id platform.ID) (*influxd } if err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindUserByID)) } return unmarshalUser(v) @@ -103,7 +104,7 @@ func (s *Store) GetUser(ctx context.Context, tx kv.Tx, id platform.ID) (*influxd func (s *Store) GetUserByName(ctx context.Context, tx kv.Tx, n string) (*influxdb.User, error) { b, err := tx.Bucket(userIndex) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindUser)) } uid, err := b.Get([]byte(n)) @@ -112,7 +113,7 @@ func (s *Store) GetUserByName(ctx context.Context, tx kv.Tx, n string) (*influxd } if err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindUser)) } var id platform.ID @@ -122,7 +123,7 @@ func (s *Store) GetUserByName(ctx context.Context, tx kv.Tx, n string) (*influxd return s.GetUser(ctx, tx, id) } -func (s *Store) ListUsers(ctx context.Context, tx kv.Tx, opt ...influxdb.FindOptions) ([]*influxdb.User, error) { +func (s *Store) ListUsers(ctx context.Context, tx kv.Tx, opt ...influxdb.FindOptions) (users []*influxdb.User, retErr error) { if len(opt) == 0 { opt = append(opt, influxdb.FindOptions{}) } @@ -130,7 +131,7 @@ func (s *Store) ListUsers(ctx context.Context, tx kv.Tx, opt ...influxdb.FindOpt b, err := tx.Bucket(userBucket) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindUsers)) } var opts []kv.CursorOption @@ -149,9 +150,13 @@ func (s *Store) ListUsers(ctx context.Context, tx kv.Tx, opt ...influxdb.FindOpt cursor, err := b.ForwardCursor(seek, opts...) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpFindUsers)) } - defer cursor.Close() + defer errors2.Capture(&retErr, + func() error { + e := cursor.Close() + return errors.ErrInternalServiceError(e, errors.WithErrorOp(influxdb.OpFindOrganizations)) + })() count := 0 us := []*influxdb.User{} @@ -172,7 +177,7 @@ func (s *Store) ListUsers(ctx context.Context, tx kv.Tx, opt ...influxdb.FindOpt } } - return us, cursor.Err() + return us, errors.ErrInternalServiceError(cursor.Err(), errors.WithErrorOp(influxdb.OpFindOrganizations)) } func (s *Store) CreateUser(ctx context.Context, tx kv.Tx, u *influxdb.User) error { @@ -195,12 +200,12 @@ func (s *Store) CreateUser(ctx context.Context, tx kv.Tx, u *influxdb.User) erro idx, err := tx.Bucket(userIndex) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateUser)) } b, err := tx.Bucket(userBucket) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpCreateUser)) } v, err := marshalUser(u) @@ -237,17 +242,17 @@ func (s *Store) UpdateUser(ctx context.Context, tx kv.Tx, id platform.ID, upd in idx, err := tx.Bucket(userIndex) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateUser)) } if err := idx.Delete([]byte(u.Name)); err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateUser)) } u.Name = *upd.Name if err := idx.Put([]byte(u.Name), encodedID); err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateUser)) } } @@ -262,10 +267,10 @@ func (s *Store) UpdateUser(ctx context.Context, tx kv.Tx, id platform.ID, upd in b, err := tx.Bucket(userBucket) if err != nil { - return nil, err + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateUser)) } if err := b.Put(encodedID, v); err != nil { - return nil, errors.ErrInternalServiceError(err) + return nil, errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpUpdateUser)) } return u, nil @@ -288,16 +293,16 @@ func (s *Store) DeleteUser(ctx context.Context, tx kv.Tx, id platform.ID) error } if err := idx.Delete([]byte(u.Name)); err != nil { - return errors.ErrInternalServiceError(err) + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteUser)) } b, err := tx.Bucket(userBucket) if err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteUser)) } if err := b.Delete(encodedID); err != nil { - return errors.ErrInternalServiceError(err) + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteUser)) } // Clean up users password. @@ -306,7 +311,7 @@ func (s *Store) DeleteUser(ctx context.Context, tx kv.Tx, id platform.ID) error return UnavailablePasswordServiceError(err) } if err := ub.Delete(encodedID); err != nil { - return err + return errors.ErrInternalServiceError(err, errors.WithErrorOp(influxdb.OpDeleteUser)) } // Clean up user URMs.