Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
AutoopsPackageName = "autoops"
CoderefPackageName = "coderef"
TeamPackageName = "team"
ExperimentPackageName = "experiment"

invalidTypeUnknown = "unknown"
invalidTypeEmpty = "empty"
Expand Down
10 changes: 2 additions & 8 deletions pkg/experiment/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"google.golang.org/grpc/status"

accountclient "github.com/bucketeer-io/bucketeer/v2/pkg/account/client"
"github.com/bucketeer-io/bucketeer/v2/pkg/api/api"
autoopsclient "github.com/bucketeer-io/bucketeer/v2/pkg/autoops/client"
storage "github.com/bucketeer-io/bucketeer/v2/pkg/experiment/storage/v2"
featureclient "github.com/bucketeer-io/bucketeer/v2/pkg/feature/client"
Expand Down Expand Up @@ -155,14 +156,7 @@ func (s *experimentService) checkEnvironmentRole(
zap.String("environmentId", environmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
}
return editor, nil
Expand Down
61 changes: 40 additions & 21 deletions pkg/experiment/api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,47 @@
package api

import (
"google.golang.org/grpc/codes"
gstatus "google.golang.org/grpc/status"
"github.com/bucketeer-io/bucketeer/v2/pkg/api/api"
pkgErr "github.com/bucketeer-io/bucketeer/v2/pkg/error"
)

var (
statusInternal = gstatus.New(codes.Internal, "experiment: internal")
statusInvalidCursor = gstatus.New(codes.InvalidArgument, "experiment: cursor is invalid")
statusNoCommand = gstatus.New(codes.InvalidArgument, "experiment: must contain at least one command")
statusFeatureIDRequired = gstatus.New(codes.InvalidArgument, "experiment: feature id must be specified")
statusExperimentIDRequired = gstatus.New(codes.InvalidArgument, "experiment: experiment id must be specified")
statusExperimentNameRequired = gstatus.New(codes.InvalidArgument, "experiment: experiment name must be specified")
statusGoalIDRequired = gstatus.New(codes.InvalidArgument, "experiment: goal id must be specified")
statusGoalTypeMismatch = gstatus.New(codes.InvalidArgument, "experiment: goal type mismatch")
statusInvalidGoalID = gstatus.New(codes.InvalidArgument, "experiment: invalid goal id")
statusGoalNameRequired = gstatus.New(codes.InvalidArgument, "experiment: goal name must be specified")
statusPeriodTooLong = gstatus.New(codes.InvalidArgument, "experiment: period too long")
statusPeriodInvalid = gstatus.New(codes.InvalidArgument, "experiment: period is invalid")
statusInvalidOrderBy = gstatus.New(codes.InvalidArgument, "expriment: order_by is invalid")
statusNotFound = gstatus.New(codes.NotFound, "experiment: not found")
statusGoalNotFound = gstatus.New(codes.NotFound, "experiment: goal not found")
statusFeatureNotFound = gstatus.New(codes.NotFound, "experiment: feature not found")
statusAlreadyExists = gstatus.New(codes.AlreadyExists, "experiment: already exists")
statusUnauthenticated = gstatus.New(codes.Unauthenticated, "experiment: unauthenticated")
statusPermissionDenied = gstatus.New(codes.PermissionDenied, "experiment: permission denied")
statusInternal = api.NewGRPCStatus(
pkgErr.NewErrorInternal(pkgErr.ExperimentPackageName, "internal error"))
statusInvalidCursor = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.ExperimentPackageName, "cursor is invalid", "cursor"))
statusNoCommand = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgNil(pkgErr.ExperimentPackageName, "must contain at least one command", "command"))
statusFeatureIDRequired = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgEmpty(pkgErr.ExperimentPackageName, "feature id must be specified", "feature_id"))
statusExperimentIDRequired = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgEmpty(pkgErr.ExperimentPackageName, "experiment id must be specified", "experiment_id"))
statusExperimentNameRequired = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgEmpty(pkgErr.ExperimentPackageName, "experiment name must be specified", "experiment_name"))
statusGoalIDRequired = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgEmpty(pkgErr.ExperimentPackageName, "goal id must be specified", "goal_id"))
statusGoalTypeMismatch = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.ExperimentPackageName, "goal type mismatch", "goal_type"))
statusInvalidGoalID = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.ExperimentPackageName, "invalid goal id", "goal_id"))
statusGoalNameRequired = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgEmpty(pkgErr.ExperimentPackageName, "goal name must be specified", "goal_name"))
statusPeriodTooLong = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.ExperimentPackageName, "period too long", "period"))
statusPeriodInvalid = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.ExperimentPackageName, "period is invalid", "period"))
statusInvalidOrderBy = api.NewGRPCStatus(
pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.ExperimentPackageName, "order_by is invalid", "order_by"))
statusNotFound = api.NewGRPCStatus(
pkgErr.NewErrorNotFound(pkgErr.ExperimentPackageName, "not found", "experiment"))
statusGoalNotFound = api.NewGRPCStatus(
pkgErr.NewErrorNotFound(pkgErr.ExperimentPackageName, "goal not found", "goal_id"))
statusFeatureNotFound = api.NewGRPCStatus(
pkgErr.NewErrorNotFound(pkgErr.ExperimentPackageName, "feature not found", "feature"))
statusAlreadyExists = api.NewGRPCStatus(
pkgErr.NewErrorAlreadyExists(pkgErr.ExperimentPackageName, "already exists"))
statusUnauthenticated = api.NewGRPCStatus(
pkgErr.NewErrorUnauthenticated(pkgErr.ExperimentPackageName, "unauthenticated"))
statusPermissionDenied = api.NewGRPCStatus(
pkgErr.NewErrorPermissionDenied(pkgErr.ExperimentPackageName, "permission denied"))
)
118 changes: 14 additions & 104 deletions pkg/experiment/api/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/bucketeer-io/bucketeer/v2/pkg/api/api"
domainevent "github.com/bucketeer-io/bucketeer/v2/pkg/domainevent/domain"
"github.com/bucketeer-io/bucketeer/v2/pkg/experiment/command"
"github.com/bucketeer-io/bucketeer/v2/pkg/experiment/domain"
Expand Down Expand Up @@ -69,14 +70,7 @@ func (s *experimentService) GetExperiment(
}
return nil, dt.Err()
}
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
return &proto.GetExperimentResponse{
Experiment: experiment.Experiment,
Expand Down Expand Up @@ -242,14 +236,7 @@ func (s *experimentService) ListExperiments(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}

summary, err := s.experimentStorage.GetExperimentSummary(ctx, req.EnvironmentId)
Expand All @@ -262,14 +249,7 @@ func (s *experimentService) ListExperiments(
req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
return &proto.ListExperimentsResponse{
Experiments: experiments,
Expand Down Expand Up @@ -361,14 +341,7 @@ func (s *experimentService) CreateExperiment(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
for _, gid := range req.Command.GoalIds {
_, err := s.getGoalMySQL(ctx, gid, req.EnvironmentId)
Expand All @@ -383,14 +356,7 @@ func (s *experimentService) CreateExperiment(
}
return nil, dt.Err()
}
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
}
experiment, err := domain.NewExperiment(
Expand All @@ -416,14 +382,7 @@ func (s *experimentService) CreateExperiment(
zap.Any("featureVariations", resp.Feature.Variations),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
err = s.mysqlClient.RunInTransactionV2(ctx, func(contextWithTx context.Context, _ mysql.Transaction) error {
handler, err := command.NewExperimentCommandHandler(
Expand Down Expand Up @@ -458,14 +417,7 @@ func (s *experimentService) CreateExperiment(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
return &proto.CreateExperimentResponse{
Experiment: experiment.Experiment,
Expand Down Expand Up @@ -504,14 +456,7 @@ func (s *experimentService) createExperimentNoCommand(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
experiment, err := domain.NewExperiment(
req.FeatureId,
Expand All @@ -536,14 +481,7 @@ func (s *experimentService) createExperimentNoCommand(
zap.Any("featureVariations", getFeatureResp.Feature.Variations),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
err = s.mysqlClient.RunInTransactionV2(ctx, func(ctxWithTx context.Context, tx mysql.Transaction) error {
for _, gid := range req.GoalIds {
Expand Down Expand Up @@ -626,14 +564,7 @@ func (s *experimentService) createExperimentNoCommand(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
return &proto.CreateExperimentResponse{
Experiment: experiment.Experiment,
Expand Down Expand Up @@ -839,14 +770,7 @@ func (s *experimentService) UpdateExperiment(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
return &proto.UpdateExperimentResponse{
Experiment: experimentPb,
Expand Down Expand Up @@ -945,14 +869,7 @@ func (s *experimentService) updateExperimentNoCommand(
zap.String("environmentId", req.EnvironmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
return nil, api.NewGRPCStatus(err).Err()
}
return &proto.UpdateExperimentResponse{
Experiment: experimentPb,
Expand Down Expand Up @@ -1310,14 +1227,7 @@ func (s *experimentService) updateExperiment(
zap.String("environmentId", environmentId),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return statusInternal.Err()
}
return dt.Err()
return api.NewGRPCStatus(err).Err()
}
return nil
}
Loading