refactor: update usage of errors

This commit is contained in:
Derrick Hammer 2024-02-15 21:06:30 -05:00
parent 16e8c84daa
commit 1b3934c793
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 9 additions and 17 deletions

View File

@ -1,18 +1,13 @@
package account package account
import ( import (
"errors" "net/http"
"git.lumeweb.com/LumeWeb/portal/account" "git.lumeweb.com/LumeWeb/portal/account"
"git.lumeweb.com/LumeWeb/portal/api/middleware" "git.lumeweb.com/LumeWeb/portal/api/middleware"
"go.sia.tech/jape" "go.sia.tech/jape"
"go.uber.org/fx" "go.uber.org/fx"
"go.uber.org/zap" "go.uber.org/zap"
"net/http"
)
var (
errInvalidLogin = errors.New("invalid login")
errFailedToCreateAccount = errors.New("failed to create account")
) )
type HttpHandler struct { type HttpHandler struct {
@ -43,7 +38,7 @@ func (h *HttpHandler) login(jc jape.Context) {
exists, _, err := h.accounts.EmailExists(request.Email) exists, _, err := h.accounts.EmailExists(request.Email)
if !exists { if !exists {
_ = jc.Error(errInvalidLogin, http.StatusUnauthorized) _ = jc.Error(account.NewAccountError(account.ErrKeyInvalidLogin, nil), http.StatusUnauthorized)
if err != nil { if err != nil {
h.logger.Error("failed to check if email exists", zap.Error(err)) 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) user, err := h.accounts.CreateAccount(request.Email, request.Password)
if err != nil { 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 return
} }
err = h.accounts.UpdateAccountName(user.ID, request.FirstName, request.LastName) err = h.accounts.UpdateAccountName(user.ID, request.FirstName, request.LastName)
if err != nil { 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 return
} }
} }
@ -138,13 +135,8 @@ func (h *HttpHandler) otpDisable(jc jape.Context) {
valid, _, err := h.accounts.ValidLoginByUserID(user, request.Password) valid, _, err := h.accounts.ValidLoginByUserID(user, request.Password)
if !valid { if !valid {
if err != nil { _ = jc.Error(account.NewAccountError(account.ErrKeyInvalidLogin, nil), http.StatusUnauthorized)
err = errors.Join(errInvalidLogin, err) return
}
if jc.Check("failed to validate password", err) != nil {
return
}
} }
err = h.accounts.OTPDisable(user) err = h.accounts.OTPDisable(user)