Skip to content

Commit

Permalink
fix(login): error handling with i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
dinxsh committed Oct 12, 2024
1 parent 4590633 commit bea81b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
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"
)

0 comments on commit bea81b6

Please sign in to comment.