From 1b3934c7937dfa3ce3ace9a996085607ff204f78 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 15 Feb 2024 21:06:30 -0500 Subject: [PATCH] refactor: update usage of errors --- api/account/http.go | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/api/account/http.go b/api/account/http.go index 4753b15..a1ea70c 100644 --- a/api/account/http.go +++ b/api/account/http.go @@ -1,18 +1,13 @@ package account import ( - "errors" + "net/http" + "git.lumeweb.com/LumeWeb/portal/account" "git.lumeweb.com/LumeWeb/portal/api/middleware" "go.sia.tech/jape" "go.uber.org/fx" "go.uber.org/zap" - "net/http" -) - -var ( - errInvalidLogin = errors.New("invalid login") - errFailedToCreateAccount = errors.New("failed to create account") ) type HttpHandler struct { @@ -43,7 +38,7 @@ func (h *HttpHandler) login(jc jape.Context) { exists, _, err := h.accounts.EmailExists(request.Email) if !exists { - _ = jc.Error(errInvalidLogin, http.StatusUnauthorized) + _ = jc.Error(account.NewAccountError(account.ErrKeyInvalidLogin, nil), http.StatusUnauthorized) if err != nil { h.logger.Error("failed to check if email exists", zap.Error(err)) } @@ -68,14 +63,16 @@ func (h *HttpHandler) register(jc jape.Context) { user, err := h.accounts.CreateAccount(request.Email, request.Password) if err != nil { - _ = jc.Error(errFailedToCreateAccount, http.StatusBadRequest) + _ = jc.Error(err, http.StatusUnauthorized) + h.logger.Error("failed to update account name", zap.Error(err)) return } err = h.accounts.UpdateAccountName(user.ID, request.FirstName, request.LastName) if err != nil { - _ = jc.Error(errors.Join(errFailedToCreateAccount, err), http.StatusBadRequest) + _ = jc.Error(account.NewAccountError(account.ErrKeyAccountCreationFailed, err), http.StatusBadRequest) + h.logger.Error("failed to update account name", zap.Error(err)) return } } @@ -138,13 +135,8 @@ func (h *HttpHandler) otpDisable(jc jape.Context) { valid, _, err := h.accounts.ValidLoginByUserID(user, request.Password) if !valid { - if err != nil { - err = errors.Join(errInvalidLogin, err) - } - - if jc.Check("failed to validate password", err) != nil { - return - } + _ = jc.Error(account.NewAccountError(account.ErrKeyInvalidLogin, nil), http.StatusUnauthorized) + return } err = h.accounts.OTPDisable(user)