Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(login): comprehensive error handling #181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions src/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ func loginCmd() *cmdBuilder.Cmd {

regions, err := regionRetriever.RetrieveAllFromURL(ctx, cmdData.Params.GetString("regionUrl"))
if err != nil {
return err
return errors.Wrap(err, i18n.T(i18n.ErrorRetrievingRegions))
}

reg, err := getLoginRegion(ctx, uxBlocks, regions, cmdData.Params.GetString("region"))
if err != nil {
return err
return errors.Wrap(err, i18n.T(i18n.ErrorSelectingRegion))
}

restApiClient := zeropsRestApiClient.NewAuthorizedClient(cmdData.Args["token"][0], "https://"+reg.Address)

response, err := restApiClient.GetUserInfo(ctx)
if err != nil {
return err
return errors.Wrap(err, i18n.T(i18n.ErrorGettingUserInfo))
}

output, err := response.Output()
if err != nil {
return err
return errors.Wrap(err, i18n.T(i18n.ErrorParsingUserInfo))
}

_, err = cmdData.CliStorage.Update(func(data cliStorage.Data) cliStorage.Data {
Expand All @@ -57,7 +57,7 @@ func loginCmd() *cmdBuilder.Cmd {
return data
})
if err != nil {
return err
return errors.Wrap(err, i18n.T(i18n.ErrorUpdatingCliStorage))
}

uxBlocks.PrintInfo(styles.SuccessLine(i18n.T(i18n.LoginSuccess, output.FullName, output.Email)))
Expand Down Expand Up @@ -103,7 +103,11 @@ func getLoginRegion(
uxBlock.SelectTableHeader(header),
)
if err != nil {
return region.RegionItem{}, err
return region.RegionItem{}, errors.Wrap(err, i18n.T(i18n.ErrorSelectingRegion))
}

if regionIndex[0] < 0 || regionIndex[0] >= len(regions) {
return region.RegionItem{}, errors.New(i18n.T(i18n.ErrorInvalidRegionIndex))
}

return regions[regionIndex[0]], nil
Expand Down
9 changes: 9 additions & 0 deletions src/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,13 @@ more info: https://docs.zerops.io/references/cli/`,
ErrorInvalidScopedProjectId: "Invalid ID of the scoped project [%s], select a different project using `zcli scope project` command.",
ErrorInvalidServiceId: "Invalid service ID [%s], %s", // values: serviceId, message
ErrorServiceNotFound: "Service [%s] not found",

// login errors
ErrorRetrievingRegions: "Error retrieving regions",
ErrorSelectingRegion: "Error selecting region",
ErrorGettingUserInfo: "Error getting user information",
ErrorParsingUserInfo: "Error parsing user information",
ErrorUpdatingCliStorage: "Error updating CLI storage",

ErrorInvalidRegionIndex: "Invalid region index selected",
}
9 changes: 9 additions & 0 deletions src/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,13 @@ const (
ErrorInvalidScopedProjectId = "ErrorInvalidScopedProjectId"
ErrorInvalidServiceId = "ErrorInvalidServiceId"
ErrorServiceNotFound = "ErrorServiceNotFound"

// login errors
ErrorRetrievingRegions = "ErrorRetrievingRegions"
ErrorSelectingRegion = "ErrorSelectingRegion"
ErrorGettingUserInfo = "ErrorGettingUserInfo"
ErrorParsingUserInfo = "ErrorParsingUserInfo"
ErrorUpdatingCliStorage = "ErrorUpdatingCliStorage"

ErrorInvalidRegionIndex = "ErrorInvalidRegionIndex"
)