diff --git a/internal/runners/pull/rationalize.go b/internal/runners/pull/rationalize.go index 4690016f48..e6a4845d57 100644 --- a/internal/runners/pull/rationalize.go +++ b/internal/runners/pull/rationalize.go @@ -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(), + ), + ) } } } diff --git a/internal/runners/revert/rationalize.go b/internal/runners/revert/rationalize.go new file mode 100644 index 0000000000..d763797d8f --- /dev/null +++ b/internal/runners/revert/rationalize.go @@ -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(), + ), + ) + } + } +} diff --git a/internal/runners/revert/revert.go b/internal/runners/revert/revert.go index de5bb3450c..b6c72237c3 100644 --- a/internal/runners/revert/revert.go +++ b/internal/runners/revert/revert.go @@ -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") } diff --git a/pkg/platform/api/buildplanner/model/buildplan.go b/pkg/platform/api/buildplanner/model/buildplan.go index 726974d81f..1012847e68 100644 --- a/pkg/platform/api/buildplanner/model/buildplan.go +++ b/pkg/platform/api/buildplanner/model/buildplan.go @@ -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 { @@ -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 { @@ -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 diff --git a/pkg/platform/model/buildplanner.go b/pkg/platform/model/buildplanner.go index 392eb90a76..7948f7ad37 100644 --- a/pkg/platform/model/buildplanner.go +++ b/pkg/platform/model/buildplanner.go @@ -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 == "" {