Refactor existing sentinel errors (#774)

This commit is contained in:
Jim authored and GitHub committed 2020-11-05 16:54:03 -05:00
1 parent 7b4959007e
commit af6ef1b687
146 files changed
+1439 -1680

No files matched your search

@@ -2,13 +2,12 @@ package accounts
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/auth/password"
"github.com/hashicorp/boundary/internal/auth/password/store"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/accounts"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/servers/controller/common"
@@ -172,7 +171,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Account, error
}
u, err := repo.LookupAccount(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return nil, handlers.NotFoundErrorf("Account %q doesn't exist.", id)
}
return nil, err
@@ -272,7 +271,7 @@ func (s Service) deleteFromRepo(ctx context.Context, scopeId, id string) (bool,
}
rows, err := repo.DeleteAccount(ctx, scopeId, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return false, nil
}
return false, fmt.Errorf("unable to delete account: %w", err)
@@ -308,7 +307,7 @@ func (s Service) changePasswordInRepo(ctx context.Context, scopeId, id string, v
out, err := repo.ChangePassword(ctx, scopeId, id, currentPassword, newPassword, version)
if err != nil {
switch {
case errors.Is(err, db.ErrRecordNotFound):
case errors.Is(err, errors.ErrRecordNotFound):
return nil, handlers.NotFoundErrorf("Account not found.")
case errors.Is(err, password.ErrTooShort):
return nil, handlers.InvalidArgumentErrorf("Error in provided request.",
@@ -333,7 +332,7 @@ func (s Service) setPasswordInRepo(ctx context.Context, scopeId, id string, vers
out, err := repo.SetPassword(ctx, scopeId, id, pw, version)
if err != nil {
switch {
case errors.Is(err, db.ErrRecordNotFound):
case errors.Is(err, errors.ErrRecordNotFound):
return nil, handlers.NotFoundErrorf("Account not found.")
case errors.Is(err, password.ErrTooShort):
return nil, handlers.InvalidArgumentErrorf("Error in provided request.",
@@ -2,7 +2,7 @@ package authmethods
import (
"context"
"errors"
stderrors "errors"
"fmt"
"strings"
@@ -10,7 +10,7 @@ import (
"github.com/hashicorp/boundary/internal/auth/password"
"github.com/hashicorp/boundary/internal/auth/password/store"
"github.com/hashicorp/boundary/internal/authtoken"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/authmethods"
pba "github.com/hashicorp/boundary/internal/gen/controller/api/resources/authtokens"
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/scopes"
@@ -53,7 +53,7 @@ type Service struct {
// NewService returns a auth method service which handles auth method related requests to boundary.
func NewService(kms *kms.Kms, pwRepoFn common.PasswordAuthRepoFactory, iamRepoFn common.IamRepoFactory, atRepoFn common.AuthTokenRepoFactory) (Service, error) {
if kms == nil {
return Service{}, errors.New("nil kms provided")
return Service{}, stderrors.New("nil kms provided")
}
if pwRepoFn == nil {
return Service{}, fmt.Errorf("nil password repository provided")
@@ -176,7 +176,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.AuthMethod, er
}
u, err := repo.LookupAuthMethod(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return nil, handlers.NotFoundErrorf("AuthMethod %q doesn't exist.", id)
}
return nil, err
@@ -285,7 +285,7 @@ func (s Service) deleteFromRepo(ctx context.Context, scopeId, id string) (bool,
}
rows, err := repo.DeleteAuthMethod(ctx, scopeId, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return false, nil
}
return false, fmt.Errorf("unable to delete auth method: %w", err)
@@ -2,12 +2,11 @@ package authtokens
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/authtoken"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/authtokens"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/servers/controller/common"
@@ -95,7 +94,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.AuthToken, err
}
u, err := repo.LookupAuthToken(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return nil, handlers.NotFoundErrorf("AuthToken %q doesn't exist.", id)
}
return nil, fmt.Errorf("unable to lookup auth token: %w", err)
@@ -113,7 +112,7 @@ func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
}
rows, err := repo.DeleteAuthToken(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return false, nil
}
return false, fmt.Errorf("unable to delete user: %w", err)
@@ -2,7 +2,6 @@ package handlers
import (
"context"
"errors"
"fmt"
"io"
"net/http"
@@ -10,7 +9,7 @@ import (
"strings"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/base62"
@@ -138,11 +137,11 @@ func backendErrorToApiError(inErr error) error {
Code: codes.Unimplemented.String(),
Message: stErr.Message(),
}}
case errors.Is(inErr, db.ErrRecordNotFound):
case errors.Is(inErr, errors.ErrRecordNotFound):
return NotFoundErrorf(genericNotFoundMsg)
case errors.Is(inErr, db.ErrInvalidFieldMask), errors.Is(inErr, db.ErrEmptyFieldMask):
case errors.Is(inErr, errors.ErrInvalidFieldMask), errors.Is(inErr, errors.ErrEmptyFieldMask):
return InvalidArgumentErrorf("Error in provided request", map[string]string{"update_mask": "Invalid update mask provided."})
case db.IsUniqueError(inErr), errors.Is(inErr, db.ErrNotUnique):
case errors.IsUniqueError(inErr), errors.Is(inErr, errors.ErrNotUnique):
return InvalidArgumentErrorf(genericUniquenessMsg, nil)
}
return nil
@@ -2,7 +2,7 @@ package handlers
import (
"context"
"errors"
stderrors "errors"
"fmt"
"io/ioutil"
"net/http"
@@ -11,7 +11,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
@@ -88,7 +88,7 @@ func TestApiErrorHandler(t *testing.T) {
},
{
name: "Unknown error",
err: errors.New("Some random error"),
err: stderrors.New("Some random error"),
expected: &pb.Error{
Status: http.StatusInternalServerError,
Code: "Internal",
@@ -97,7 +97,7 @@ func TestApiErrorHandler(t *testing.T) {
},
{
name: "Db invalid public id error",
err: fmt.Errorf("test error: %w", db.ErrInvalidPublicId),
err: fmt.Errorf("test error: %w", errors.ErrInvalidPublicId),
expected: &pb.Error{
Status: http.StatusInternalServerError,
Code: "Internal",
@@ -106,7 +106,7 @@ func TestApiErrorHandler(t *testing.T) {
},
{
name: "Db invalid parameter",
err: fmt.Errorf("test error: %w", db.ErrInvalidParameter),
err: fmt.Errorf("test error: %w", errors.ErrInvalidParameter),
expected: &pb.Error{
Status: http.StatusInternalServerError,
Code: "Internal",
@@ -115,7 +115,7 @@ func TestApiErrorHandler(t *testing.T) {
},
{
name: "Db invalid field mask",
err: fmt.Errorf("test error: %w", db.ErrInvalidFieldMask),
err: fmt.Errorf("test error: %w", errors.ErrInvalidFieldMask),
expected: &pb.Error{
Status: http.StatusBadRequest,
Code: "InvalidArgument",
@@ -125,7 +125,7 @@ func TestApiErrorHandler(t *testing.T) {
},
{
name: "Db empty field mask",
err: fmt.Errorf("test error: %w", db.ErrEmptyFieldMask),
err: fmt.Errorf("test error: %w", errors.ErrEmptyFieldMask),
expected: &pb.Error{
Status: http.StatusBadRequest,
Code: "InvalidArgument",
@@ -135,7 +135,7 @@ func TestApiErrorHandler(t *testing.T) {
},
{
name: "Db not unique",
err: fmt.Errorf("test error: %w", db.ErrNotUnique),
err: fmt.Errorf("test error: %w", errors.ErrNotUnique),
expected: &pb.Error{
Status: http.StatusBadRequest,
Code: "InvalidArgument",
@@ -144,7 +144,7 @@ func TestApiErrorHandler(t *testing.T) {
},
{
name: "Db record not found",
err: fmt.Errorf("test error: %w", db.ErrRecordNotFound),
err: fmt.Errorf("test error: %w", errors.ErrRecordNotFound),
expected: &pb.Error{
Status: http.StatusNotFound,
Code: "NotFound",
@@ -153,7 +153,7 @@ func TestApiErrorHandler(t *testing.T) {
},
{
name: "Db multiple records",
err: fmt.Errorf("test error: %w", db.ErrMultipleRecords),
err: fmt.Errorf("test error: %w", errors.ErrMultipleRecords),
expected: &pb.Error{
Status: http.StatusInternalServerError,
Code: "Internal",
@@ -2,11 +2,10 @@ package groups
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/groups"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/iam"
@@ -191,7 +190,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Group, error)
}
g, m, err := repo.LookupGroup(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return nil, handlers.NotFoundErrorf("Group %q doesn't exist.", id)
}
return nil, fmt.Errorf("unable to get group: %w", err)
@@ -267,7 +266,7 @@ func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
}
rows, err := repo.DeleteGroup(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return false, nil
}
return false, fmt.Errorf("unable to delete group: %w", err)
@@ -2,11 +2,10 @@ package host_sets
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/hostsets"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/host"
@@ -219,7 +218,7 @@ func (s Service) createInRepo(ctx context.Context, scopeId, catalogId string, it
}
out, err := repo.CreateSet(ctx, scopeId, h)
if err != nil {
if db.IsUniqueError(err) || errors.Is(err, db.ErrNotUnique) {
if errors.IsUniqueError(err) || errors.Is(err, errors.ErrNotUnique) {
// Push this error through so the error interceptor can interpret it correctly.
return nil, err
}
@@ -2,13 +2,12 @@ package hosts
import (
"context"
"errors"
"fmt"
"net"
"strings"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/hosts"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/host"
@@ -177,7 +176,7 @@ func (s Service) createInRepo(ctx context.Context, scopeId, catalogId string, it
}
out, err := repo.CreateHost(ctx, scopeId, h)
if err != nil {
if db.IsUniqueError(err) || errors.Is(err, db.ErrNotUnique) {
if errors.IsUniqueError(err) || errors.Is(err, errors.ErrNotUnique) {
// Push this error through so the error interceptor can interpret it correctly.
return nil, err
}
@@ -2,11 +2,10 @@ package roles
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/roles"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/iam"
@@ -243,7 +242,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Role, error) {
}
out, pr, roleGrants, err := repo.LookupRole(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return nil, handlers.NotFoundErrorf("Role %q doesn't exist.", id)
}
return nil, err
@@ -326,7 +325,7 @@ func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
}
rows, err := repo.DeleteRole(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return false, nil
}
return false, fmt.Errorf("unable to delete role: %w", err)
@@ -2,11 +2,11 @@ package sessions
import (
"context"
"errors"
stderrors "errors"
"fmt"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/sessions"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/servers/controller/common"
@@ -97,7 +97,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Session, error
}
sess, _, err := repo.LookupSession(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return nil, handlers.NotFoundErrorf("Session %q doesn't exist.", id)
}
return nil, err
@@ -176,7 +176,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
parentId = t.ScopeId
opts = append(opts, auth.WithId(id))
default:
res.Error = errors.New("unsupported action")
res.Error = stderrors.New("unsupported action")
return res
}
opts = append(opts, auth.WithScopeId(parentId))
@@ -197,10 +197,10 @@ func toProto(in *session.Session) *pb.Session {
Type: target.SubtypeFromId(in.TargetId).String(),
// TODO: Provide the ServerType and the ServerId when that information becomes relevant in the API.
CreatedTime: in.CreateTime.GetTimestamp(),
UpdatedTime: in.UpdateTime.GetTimestamp(),
ExpirationTime: in.ExpirationTime.GetTimestamp(),
Certificate: in.Certificate,
CreatedTime: in.CreateTime.GetTimestamp(),
UpdatedTime: in.UpdateTime.GetTimestamp(),
ExpirationTime: in.ExpirationTime.GetTimestamp(),
Certificate: in.Certificate,
TerminationReason: in.TerminationReason,
}
if len(in.States) > 0 {
@@ -2,7 +2,7 @@ package targets
import (
"context"
"errors"
stderrors "errors"
"fmt"
"math/rand"
"net/url"
@@ -10,8 +10,8 @@ import (
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/targets"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/host"
@@ -241,14 +241,14 @@ func (s Service) AuthorizeSession(ctx context.Context, req *pbs.AuthorizeSession
}
if authResults.RoundTripValue == nil {
return nil, errors.New("authorize session: expected to get a target back from auth results")
return nil, stderrors.New("authorize session: expected to get a target back from auth results")
}
t, ok := authResults.RoundTripValue.(target.Target)
if !ok {
return nil, errors.New("authorize session: round tripped auth results value is not a target")
return nil, stderrors.New("authorize session: round tripped auth results value is not a target")
}
if t == nil {
return nil, errors.New("authorize session: round tripped target is nil")
return nil, stderrors.New("authorize session: round tripped target is nil")
}
// This could happen if, say, u_recovery was used or u_anon was granted. But
@@ -276,7 +276,7 @@ func (s Service) AuthorizeSession(ctx context.Context, req *pbs.AuthorizeSession
}
t, hostSets, err := repo.LookupTarget(ctx, t.GetPublicId())
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return nil, handlers.NotFoundErrorf("Target %q not found.", t.GetPublicId())
}
return nil, err
@@ -360,7 +360,7 @@ HostSetIterationLoop:
}
endpointHost = h.Address
if endpointHost == "" {
return nil, errors.New("host had empty address")
return nil, stderrors.New("host had empty address")
}
}
if defaultPort != 0 {
@@ -444,7 +444,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Target, error)
}
u, m, err := repo.LookupTarget(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return nil, handlers.NotFoundErrorf("Target %q doesn't exist.", id)
}
return nil, err
@@ -543,7 +543,7 @@ func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
}
rows, err := repo.DeleteTarget(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return false, nil
}
return false, fmt.Errorf("unable to delete target: %w", err)
@@ -2,12 +2,11 @@ package users
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/auth/password"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/users"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/iam"
@@ -192,7 +191,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.User, error) {
}
u, accts, err := repo.LookupUser(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return nil, handlers.NotFoundErrorf("User %q doesn't exist.", id)
}
return nil, err
@@ -268,7 +267,7 @@ func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
}
rows, err := repo.DeleteUser(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
if errors.Is(err, errors.ErrRecordNotFound) {
return false, nil
}
return false, fmt.Errorf("unable to delete user: %w", err)