From 0c360c0cc4cfa8872336b2add44b626f5eb74487 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 3 Sep 2024 13:19:32 +0300 Subject: [PATCH] remove panics after failed execution --- internal/handlers/ussd/menuhandler.go | 49 ++++++++++++++++----------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index ebcc7f3..cb2a913 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -41,6 +41,7 @@ const ( Offerings = "OFFERINGS" Recipient = "RECIPIENT" Amount = "AMOUNT" + AccountCreated = "ACCOUNTCREATED" ) func toBytes(s string) []byte { @@ -146,27 +147,37 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) if err != nil { return res, err } - accountResp, err := h.accountService.CreateAccount() + _, err = h.db.Fetch([]byte(AccountCreated)) if err != nil { - res.FlagSet = append(res.FlagSet, flags["flag_account_creation_failed"]) - return res, err - } - data := map[string]string{ - TrackingIdKey: accountResp.Result.TrackingId, - PublicKeyKey: accountResp.Result.PublicKey, - CustodialIdKey: accountResp.Result.CustodialId.String(), - } + if errors.Is(err, gdbm.ErrItemNotFound) { + accountResp, err := h.accountService.CreateAccount() + if err != nil { + res.FlagSet = append(res.FlagSet, flags["flag_account_creation_failed"]) + return res, err + } + data := map[string]string{ + TrackingIdKey: accountResp.Result.TrackingId, + PublicKeyKey: accountResp.Result.PublicKey, + CustodialIdKey: accountResp.Result.CustodialId.String(), + } - for key, value := range data { - err := h.db.Store(toBytes(key), toBytes(value), true) - if err != nil { + for key, value := range data { + err := h.db.Store(toBytes(key), toBytes(value), true) + if err != nil { + return res, err + } + } + key := []byte(AccountCreated) + value := []byte("1") + h.db.Store(key, value, true) + res.FlagSet = append(res.FlagSet, flags["flag_account_created"]) + return res, err + } else { return res, err - } + } else { + return res, nil } - - res.FlagSet = append(res.FlagSet, flags["flag_account_created"]) - return res, err } // SavePin persists the user's PIN choice into the filesystem @@ -609,11 +620,11 @@ func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byt err = h.db.Delete([]byte(Amount)) if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) { - panic(err) + return res,err } err = h.db.Delete([]byte(Recipient)) if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) { - panic(err) + return res,err } res.FlagReset = append(res.FlagReset, flags["flag_invalid_recipient"], flags["flag_invalid_recipient_with_invite"]) @@ -634,7 +645,7 @@ func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input err = h.db.Delete([]byte(Amount)) if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) { - panic(err) + return res,err } res.FlagReset = append(res.FlagReset, flags["flag_invalid_amount"])