Skip to content

Commit

Permalink
Update error handling on revert (and pull)
Browse files Browse the repository at this point in the history
  • Loading branch information
MDrakos committed Nov 9, 2023
1 parent 447ca43 commit 4bf8db3
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 20 deletions.
17 changes: 17 additions & 0 deletions internal/runners/pull/rationalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ func rationalizeError(err *error) {
),
errs.SetInput(),
)
case model.NotFoundErrorType, model.ForbiddenErrorType:
*err = errs.WrapUserFacing(*err,
locale.Tl("err_pull_not_found",
mergeCommitErr.Error(),
),
errs.SetInput(),
errs.SetTips(
locale.T("tip_private_project_auth"),
),
)
default:
*err = errs.WrapUserFacing(*err,
locale.Tl("err_pull_no_common_base",
"Could not merge, recieved error message: {{.V0}}",
mergeCommitErr.Error(),
),
)
}
}
}
47 changes: 47 additions & 0 deletions internal/runners/revert/rationalize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package revert

import (
"errors"

"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/pkg/platform/api/buildplanner/model"
)

func rationalizeError(err *error) {
if err == nil {
return
}

var revertCommitError *model.RevertCommitError

switch {
case errors.As(*err, &revertCommitError):
switch revertCommitError.Type {
case model.NotFoundErrorType, model.ForbiddenErrorType:
*err = errs.WrapUserFacing(*err,
locale.Tl("err_revert_not_found",
revertCommitError.Error(),
),
errs.SetInput(),
errs.SetTips(
locale.T("tip_private_project_auth"),
),
)
case model.NoChangeSinceLastCommitErrorType:
*err = errs.WrapUserFacing(*err,
locale.Tl("err_revert_no_change",
"Could not revert commit, no changes since last commit",
),
errs.SetInput(),
)
default:
*err = errs.WrapUserFacing(*err,
locale.Tl("err_revert_not_found",
"Could not revert commit, recieved error message: {{.V0}}",
revertCommitError.Error(),
),
)
}
}
}
4 changes: 3 additions & 1 deletion internal/runners/revert/revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func New(prime primeable) *Revert {
}
}

func (r *Revert) Run(params *Params) error {
func (r *Revert) Run(params *Params) (rerr error) {
defer rationalizeError(&rerr)

if r.project == nil {
return locale.NewInputError("err_no_project")
}
Expand Down
55 changes: 38 additions & 17 deletions pkg/platform/api/buildplanner/model/buildplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,23 @@ const (
MergeCommitStrategyFastForward MergeStrategy = "FastForward"

// Error types
ErrorType = "Error"
NotFoundErrorType = "NotFound"
BuildResultPlanningErrorType = "PlanningError"
ParseErrorType = "ParseError"
AlreadyExistsErrorType = "AlreadyExists"
NoChangeSinceLastCommitErrorType = "NoChangeSinceLastCommit"
HeadOnBranchMovedErrorType = "HeadOnBranchMoved"
ForbiddenErrorType = "Forbidden"
RemediableSolveErrorType = "RemediableSolveError"
PlanningErrorType = "PlanningError"
MergeConflictType = "MergeConflict"
FastForwardErrorType = "FastForwardError"
NoCommonBaseFoundType = "NoCommonBaseFound"
ValidationErrorType = "ValidationError"
MergeConflictErrorType = "MergeConflict"
ErrorType = "Error"
NotFoundErrorType = "NotFound"
ParseErrorType = "ParseError"
AlreadyExistsErrorType = "AlreadyExists"
NoChangeSinceLastCommitErrorType = "NoChangeSinceLastCommit"
HeadOnBranchMovedErrorType = "HeadOnBranchMoved"
ForbiddenErrorType = "Forbidden"
RemediableSolveErrorType = "RemediableSolveError"
PlanningErrorType = "PlanningError"
MergeConflictType = "MergeConflict"
FastForwardErrorType = "FastForwardError"
NoCommonBaseFoundType = "NoCommonBaseFound"
ValidationErrorType = "ValidationError"
MergeConflictErrorType = "MergeConflict"
RevertConflictErrorType = "RevertConflict"
CommitNotInTargetHistoryErrorType = "CommitNotInTargetHistory"
ComitHasNoParentErrorType = "CommitHasNoParent"
)

func IsStateToolArtifact(mimeType string) bool {
Expand Down Expand Up @@ -282,17 +284,22 @@ func (b *BuildPlanByCommit) CommitID() (strfmt.UUID, error) {

func IsErrorResponse(errorType string) bool {
return errorType == ErrorType ||
errorType == NotFoundErrorType ||
errorType == ParseErrorType ||
errorType == AlreadyExistsErrorType ||
errorType == NoChangeSinceLastCommitErrorType ||
errorType == HeadOnBranchMovedErrorType ||
errorType == ForbiddenErrorType ||
errorType == RemediableSolveErrorType ||
errorType == PlanningErrorType ||
errorType == NotFoundErrorType ||
errorType == MergeConflictType ||
errorType == FastForwardErrorType ||
errorType == NoCommonBaseFoundType ||
errorType == ValidationErrorType
errorType == ValidationErrorType ||
errorType == MergeConflictErrorType ||
errorType == RevertConflictErrorType ||
errorType == CommitNotInTargetHistoryErrorType ||
errorType == ComitHasNoParentErrorType
}

func ProcessCommitError(commit *Commit, fallbackMessage string) error {
Expand Down Expand Up @@ -357,6 +364,20 @@ func ProcessProjectError(project *Project, fallbackMessage string) error {
return errs.New(fallbackMessage)
}

type RevertCommitError struct {
Type string
Message string
}

func (m *RevertCommitError) Error() string { return m.Message }

func ProcessRevertCommitError(rcErr *revertedCommit, fallbackMessage string) error {
if rcErr.Type != "" {
return &RevertCommitError{rcErr.Type, rcErr.Message}
}
return errs.New(fallbackMessage)
}

type ProjectCreatedError struct {
Type string
Message string
Expand Down
12 changes: 10 additions & 2 deletions pkg/platform/model/buildplanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,19 @@ func (bp *BuildPlanner) RevertCommit(organization, project, parentCommitID, comm
}

if resp.RevertedCommit == nil {
return "", errs.New("Commit is nil")
return "", errs.New("Revert commit response is nil")
}

if bpModel.IsErrorResponse(resp.RevertedCommit.Type) {
return "", bpModel.ProcessCommitError(resp.RevertedCommit.Commit, "Could not revert commit")
return "", bpModel.ProcessRevertCommitError(resp.RevertedCommit, "Could not revert commit")
}

if resp.RevertedCommit.Commit == nil {
return "", errs.New("Revert commit's commit is nil'")
}

if bpModel.IsErrorResponse(resp.RevertedCommit.Commit.Type) {
return "", bpModel.ProcessCommitError(resp.RevertedCommit.Commit, "Could not process error response from revert commit")
}

if resp.RevertedCommit.Commit.CommitID == "" {
Expand Down

0 comments on commit 4bf8db3

Please sign in to comment.