Add better messages for expected google login errors

This commit is contained in:
Tulir Asokan 2024-03-12 17:51:45 +02:00
parent dce41f68bc
commit 847b9a3a90
2 changed files with 24 additions and 5 deletions

View file

@ -19,6 +19,7 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"strings" "strings"
@ -29,6 +30,7 @@ import (
"maunium.net/go/mautrix/event" "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-gmessages/libgm"
"go.mau.fi/mautrix-gmessages/libgm/gmproto" "go.mau.fi/mautrix-gmessages/libgm/gmproto"
) )
@ -156,6 +158,13 @@ func fnLoginGoogle(ce *WrappedCommandEvent) {
ce.Reply("Send your Google cookies here, formatted as a key-value JSON object (see <https://docs.mau.fi/bridges/go/gmessages/authentication.html> for details)") ce.Reply("Send your Google cookies here, formatted as a key-value JSON object (see <https://docs.mau.fi/bridges/go/gmessages/authentication.html> for details)")
} }
const (
pairingErrMsgNoDevices = "No devices found. Make sure you've enabled account pairing in the Google Messages app on your phone."
pairingErrMsgIncorrectEmoji = "Incorrect emoji chosen on phone, please try again"
pairingErrMsgCancelled = "Pairing cancelled on phone"
pairingErrMsgTimeout = "Pairing timed out, please try again"
)
func fnLoginGoogleCookies(ce *WrappedCommandEvent) { func fnLoginGoogleCookies(ce *WrappedCommandEvent) {
ce.User.CommandState = nil ce.User.CommandState = nil
if ce.User.Session != nil { if ce.User.Session != nil {
@ -183,7 +192,17 @@ func fnLoginGoogleCookies(ce *WrappedCommandEvent) {
ce.Reply(emoji) ce.Reply(emoji)
}) })
if err != nil { if err != nil {
if errors.Is(err, libgm.ErrNoDevicesFound) {
ce.Reply(pairingErrMsgNoDevices)
} else if errors.Is(err, libgm.ErrIncorrectEmoji) {
ce.Reply(pairingErrMsgIncorrectEmoji)
} else if errors.Is(err, libgm.ErrPairingCancelled) {
ce.Reply(pairingErrMsgCancelled)
} else if errors.Is(err, libgm.ErrPairingTimeout) {
ce.Reply(pairingErrMsgTimeout)
} else {
ce.Reply("Login failed: %v", err) ce.Reply("Login failed: %v", err)
}
} else { } else {
ce.Reply("Login successful") ce.Reply("Login successful")
} }

View file

@ -353,7 +353,7 @@ func (prov *ProvisioningAPI) GoogleLoginStart(w http.ResponseWriter, r *http.Req
switch { switch {
case errors.Is(err, libgm.ErrNoDevicesFound): case errors.Is(err, libgm.ErrNoDevicesFound):
jsonResponse(w, http.StatusBadRequest, Error{ jsonResponse(w, http.StatusBadRequest, Error{
Error: err.Error(), Error: pairingErrMsgNoDevices,
ErrCode: "no-devices-found", ErrCode: "no-devices-found",
}) })
default: default:
@ -384,17 +384,17 @@ func (prov *ProvisioningAPI) GoogleLoginWait(w http.ResponseWriter, r *http.Requ
}) })
case errors.Is(err, libgm.ErrIncorrectEmoji): case errors.Is(err, libgm.ErrIncorrectEmoji):
jsonResponse(w, http.StatusBadRequest, Error{ jsonResponse(w, http.StatusBadRequest, Error{
Error: err.Error(), Error: pairingErrMsgIncorrectEmoji,
ErrCode: "incorrect-emoji", ErrCode: "incorrect-emoji",
}) })
case errors.Is(err, libgm.ErrPairingCancelled): case errors.Is(err, libgm.ErrPairingCancelled):
jsonResponse(w, http.StatusBadRequest, Error{ jsonResponse(w, http.StatusBadRequest, Error{
Error: err.Error(), Error: pairingErrMsgCancelled,
ErrCode: "pairing-cancelled", ErrCode: "pairing-cancelled",
}) })
case errors.Is(err, libgm.ErrPairingTimeout): case errors.Is(err, libgm.ErrPairingTimeout):
jsonResponse(w, http.StatusBadRequest, Error{ jsonResponse(w, http.StatusBadRequest, Error{
Error: err.Error(), Error: pairingErrMsgTimeout,
ErrCode: "timeout", ErrCode: "timeout",
}) })
case errors.Is(err, context.Canceled): case errors.Is(err, context.Canceled):