Skip to content

Commit

Permalink
fix: regularize error processing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidby-influx committed Dec 22, 2023
1 parent 1ca654b commit 69846d0
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 130 deletions.
51 changes: 17 additions & 34 deletions authorization/storage_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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,
}
}

Expand All @@ -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,
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 10 additions & 3 deletions kit/platform/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(": ")
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
Loading

0 comments on commit 69846d0

Please sign in to comment.