From 8eed8382cb7d9c9d859649fa94b4e4d311262973 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 26 Feb 2024 15:30:57 +0200 Subject: [PATCH] Add proper error codes in google login provisioning API --- provisioning.go | 52 ++++++++++++++++++++++++++++++++++++++----------- user.go | 3 ++- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/provisioning.go b/provisioning.go index c608d74..85c1c62 100644 --- a/provisioning.go +++ b/provisioning.go @@ -33,6 +33,7 @@ import ( "maunium.net/go/mautrix/bridge/status" "maunium.net/go/mautrix/id" + "go.mau.fi/mautrix-gmessages/libgm" "go.mau.fi/mautrix-gmessages/libgm/gmproto" ) @@ -340,11 +341,18 @@ func (prov *ProvisioningAPI) GoogleLoginStart(w http.ResponseWriter, r *http.Req emoji, err := user.AsyncLoginGoogleStart(req.Cookies) if err != nil { log.Err(err).Msg("Failed to start login") - // TODO proper error codes - jsonResponse(w, http.StatusInternalServerError, Error{ - Error: "Failed to start login", - ErrCode: "start login fail", - }) + switch { + case errors.Is(err, libgm.ErrNoDevicesFound): + jsonResponse(w, http.StatusBadRequest, Error{ + Error: err.Error(), + ErrCode: "no-devices-found", + }) + default: + jsonResponse(w, http.StatusInternalServerError, Error{ + Error: "Failed to start login", + ErrCode: "unknown", + }) + } return } jsonResponse(w, http.StatusOK, &RespGoogleLoginStart{Status: "emoji", Emoji: emoji}) @@ -358,12 +366,34 @@ func (prov *ProvisioningAPI) GoogleLoginWait(w http.ResponseWriter, r *http.Requ err := user.AsyncLoginGoogleWait() if err != nil { - log.Err(err).Msg("Failed to start login") - // TODO proper error codes - jsonResponse(w, http.StatusInternalServerError, Error{ - Error: "Failed to finish login", - ErrCode: "finish login fail", - }) + log.Err(err).Msg("Failed to wait for google login") + switch { + case errors.Is(err, ErrNoLoginInProgress): + jsonResponse(w, http.StatusBadRequest, Error{ + Error: "No login in progress", + ErrCode: "login-not-in-progress", + }) + case errors.Is(err, libgm.ErrIncorrectEmoji): + jsonResponse(w, http.StatusBadRequest, Error{ + Error: err.Error(), + ErrCode: "incorrect-emoji", + }) + case errors.Is(err, libgm.ErrPairingCancelled): + jsonResponse(w, http.StatusBadRequest, Error{ + Error: err.Error(), + ErrCode: "pairing-cancelled", + }) + case errors.Is(err, libgm.ErrPairingTimeout): + jsonResponse(w, http.StatusBadRequest, Error{ + Error: err.Error(), + ErrCode: "timeout", + }) + default: + jsonResponse(w, http.StatusInternalServerError, Error{ + Error: "Failed to finish login", + ErrCode: "unknown", + }) + } return } jsonResponse(w, http.StatusOK, LoginResponse{Status: "success"}) diff --git a/user.go b/user.go index 1505443..4080f7b 100644 --- a/user.go +++ b/user.go @@ -377,6 +377,7 @@ func (user *User) SetManagementRoom(roomID id.RoomID) { var ErrAlreadyLoggedIn = errors.New("already logged in") var ErrLoginInProgress = errors.New("login already in progress") +var ErrNoLoginInProgress = errors.New("no login in progress") var ErrLoginTimeout = errors.New("login timed out") func (user *User) createClient(sess *libgm.AuthData) { @@ -507,7 +508,7 @@ func (user *User) AsyncLoginGoogleStart(cookies map[string]string) (outEmoji str func (user *User) AsyncLoginGoogleWait() error { ch := user.googleAsyncPairErrChan.Swap(nil) if ch == nil { - return fmt.Errorf("no login in progress") + return ErrNoLoginInProgress } return <-*ch }