From bea81b622b0451390eec35ae4da8e180dcc8e70c Mon Sep 17 00:00:00 2001 From: dinesh Date: Sun, 13 Oct 2024 00:42:19 +0530 Subject: [PATCH] fix(login): error handling with i18n --- src/cmd/login.go | 16 ++++++++++------ src/i18n/en.go | 9 +++++++++ src/i18n/i18n.go | 9 +++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/cmd/login.go b/src/cmd/login.go index adc9a432..bdf7a0e6 100644 --- a/src/cmd/login.go +++ b/src/cmd/login.go @@ -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 { @@ -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))) @@ -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 diff --git a/src/i18n/en.go b/src/i18n/en.go index 5df246f6..45767416 100644 --- a/src/i18n/en.go +++ b/src/i18n/en.go @@ -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", } diff --git a/src/i18n/i18n.go b/src/i18n/i18n.go index 55091c49..2f0bd0d3 100644 --- a/src/i18n/i18n.go +++ b/src/i18n/i18n.go @@ -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" )