Skip to content

Commit

Permalink
Add more user-facing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MDrakos committed Nov 27, 2023
1 parent b5e616c commit 2e86d6b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
4 changes: 4 additions & 0 deletions internal/locale/locales/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,8 @@ err_invalid_output_format:
other: "Invalid output format specified: '[NOTICE]{{.V0}}[/RESET]'; Supported formats: [ACTIONABLE]{{.V1}}[/RESET]"
err_invalid_language:
other: "Invalid language specified: '[NOTICE]{{.V0}}[/RESET]'; Supported languages: [ACTIONABLE]{{.V1}}[/RESET]"
err_init_invalid_org:
other: The organization '[ACTIONABLE]{{.V0}}[/RESET]' either does not exist, or you do not have permissions to create a project in it.
push_creating_project:
other: "Creating project [NOTICE]{{.V1}}[/RESET] under [NOTICE]{{.V0}}[/RESET] on the ActiveState Platform"
push_project_updated:
Expand Down Expand Up @@ -2032,6 +2034,8 @@ pjfile_deprecation_entry:
other: " - '[ACTIONABLE]{{.V0}}[/RESET]' located at byte [ACTIONABLE]{{.V1}}[/RESET]"
err_init_authenticated:
other: You need to be authenticated to initialize a project. Authenticate by running '[ACTIONABLE]state auth[/RESET]'.
err_init_project_exists:
other: A project file already exists for {{.V0}} at {{.V1}}
err_country_blocked:
other: This service is not available in your region.
err_commit_id_invalid:
Expand Down
33 changes: 26 additions & 7 deletions internal/runners/initialize/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ActiveState/cli/internal/primer"
"github.com/ActiveState/cli/internal/runbits"
"github.com/ActiveState/cli/internal/runbits/commitmediator"
"github.com/ActiveState/cli/internal/runbits/rationalize"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
"github.com/ActiveState/cli/pkg/platform/runtime/setup"
Expand Down Expand Up @@ -53,6 +54,19 @@ type primeable interface {
primer.SvcModeler
}

type errProjectExists struct {
error
name string
path string
}

type errNoOwner struct {
error
owner string
}

var errNoLanguage = errs.New("No language specified")

// New returns a prepared ptr to Initialize instance.
func New(prime primeable) *Initialize {
return &Initialize{prime.Auth(), prime.Config(), prime.Output(), prime.Analytics(), prime.SvcModel()}
Expand Down Expand Up @@ -91,7 +105,7 @@ func (r *Initialize) Run(params *RunParams) (rerr error) {
logging.Debug("Init: %s/%s %v", params.Namespace.Owner, params.Namespace.Project, params.Private)

if !r.auth.Authenticated() {
return locale.NewInputError("err_init_authenticated")
return rationalize.ErrNotAuthenticated
}

path := params.Path
Expand All @@ -104,7 +118,11 @@ func (r *Initialize) Run(params *RunParams) (rerr error) {
}

if fileutils.TargetExists(filepath.Join(path, constants.ConfigFileName)) {
return locale.NewInputError("err_projectfile_exists")
return errProjectExists{
error: errs.New("Project file already exists"),
name: params.Namespace.Project,
path: path,
}
}

err := fileutils.MkdirUnlessExists(path)
Expand All @@ -130,7 +148,7 @@ func (r *Initialize) Run(params *RunParams) (rerr error) {
}

if languageName == "" {
return locale.NewInputError("err_init_no_language")
return errNoLanguage
}

// Require 'python', 'python@3', or 'python@2' instead of 'python3' or 'python2'.
Expand Down Expand Up @@ -176,9 +194,10 @@ func (r *Initialize) Run(params *RunParams) (rerr error) {
}
}
if owner == "" {
return locale.NewInputError("err_invalid_org",
"The organization '[ACTIONABLE]{{.V0}}[/RESET]' either does not exist, or you do not have permissions to create a project in it.",
params.Namespace.Owner)
return errNoOwner{
error: errs.New("Could not find organization"),
owner: params.Namespace.Owner,
}
}
namespace := project.Namespaced{Owner: owner, Project: params.Namespace.Project}

Expand Down Expand Up @@ -244,7 +263,7 @@ func (r *Initialize) Run(params *RunParams) (rerr error) {
Description: locale.T("commit_message_add_initial"),
})
if err != nil {
return locale.WrapError(err, "err_init_commit", "Could not create initial commit")
return locale.WrapError(err, "err_init_commit", "Could not create project")
}

if err := commitmediator.Set(proj, commitID.String()); err != nil {
Expand Down
28 changes: 28 additions & 0 deletions internal/runners/initialize/rationalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/runbits/rationalize"
bpModel "github.com/ActiveState/cli/pkg/platform/api/buildplanner/model"
"github.com/ActiveState/cli/pkg/platform/runtime/setup"
"github.com/ActiveState/cli/pkg/project"
Expand All @@ -14,11 +15,38 @@ import (
func rationalizeError(namespace *project.Namespaced, rerr *error) {
var pcErr *bpModel.ProjectCreatedError
var errArtifactSetup *setup.ArtifactSetupErrors
var projectExistsErr *errProjectExists
var noOwnerErr *errNoOwner

switch {
case rerr == nil:
return

// Not authenticated
case errors.Is(*rerr, rationalize.ErrNotAuthenticated):
*rerr = errs.WrapUserFacing(*rerr,
locale.T("err_init_authenticated"),
errs.SetInput(),
)

case errors.As(*rerr, &projectExistsErr):
*rerr = errs.WrapUserFacing(*rerr,
locale.Tr("err_init_project_exists", projectExistsErr.name, projectExistsErr.path),
errs.SetInput(),
)

case errors.Is(*rerr, errNoLanguage):
*rerr = errs.WrapUserFacing(*rerr,
locale.T("err_init_no_language"),
errs.SetInput(),
)

case errors.As(*rerr, &noOwnerErr):
*rerr = errs.WrapUserFacing(*rerr,
locale.Tr("err_invalid_org", noOwnerErr.owner),
errs.SetInput(),
)

// Error creating project.
case errors.As(*rerr, &pcErr):
switch pcErr.Type {
Expand Down

0 comments on commit 2e86d6b

Please sign in to comment.