Remove some panics

This commit is contained in:
Tulir Asokan 2023-07-16 16:18:45 +03:00
parent 6225b83d2f
commit fa5928e405
2 changed files with 16 additions and 19 deletions

View file

@ -106,15 +106,14 @@ func (c *Client) Connect() error {
return fmt.Errorf("not logged in") return fmt.Errorf("not logged in")
} }
refreshErr := c.refreshAuthToken() err := c.refreshAuthToken()
if refreshErr != nil { if err != nil {
panic(refreshErr) return fmt.Errorf("failed to refresh auth token: %w", err)
} }
webEncryptionKeyResponse, webEncryptionKeyErr := c.GetWebEncryptionKey() webEncryptionKeyResponse, err := c.GetWebEncryptionKey()
if webEncryptionKeyErr != nil { if err != nil {
c.Logger.Err(webEncryptionKeyErr).Any("response", webEncryptionKeyResponse).Msg("GetWebEncryptionKey request failed") return fmt.Errorf("failed to get web encryption key: %w", err)
return webEncryptionKeyErr
} }
c.updateWebEncryptionKey(webEncryptionKeyResponse.GetKey()) c.updateWebEncryptionKey(webEncryptionKeyResponse.GetKey())
go c.rpc.ListenReceiveMessages() go c.rpc.ListenReceiveMessages()
@ -122,12 +121,12 @@ func (c *Client) Connect() error {
bugleRes, bugleErr := c.IsBugleDefault() bugleRes, bugleErr := c.IsBugleDefault()
if bugleErr != nil { if bugleErr != nil {
panic(bugleErr) return fmt.Errorf("failed to check bugle default: %w", err)
} }
c.Logger.Info().Any("isBugle", bugleRes.Success).Msg("IsBugleDefault") c.Logger.Debug().Any("isBugle", bugleRes.Success).Msg("IsBugleDefault")
sessionErr := c.SetActiveSession() sessionErr := c.SetActiveSession()
if sessionErr != nil { if sessionErr != nil {
panic(sessionErr) return fmt.Errorf("failed to set active session: %w", err)
} }
return nil return nil
} }

View file

@ -1,6 +1,8 @@
package libgm package libgm
import ( import (
"fmt"
"go.mau.fi/mautrix-gmessages/libgm/events" "go.mau.fi/mautrix-gmessages/libgm/events"
"go.mau.fi/mautrix-gmessages/libgm/pblite" "go.mau.fi/mautrix-gmessages/libgm/pblite"
@ -17,10 +19,7 @@ func (c *Client) handlePairingEvent(response *pblite.Response) {
switch evt := pairEventData.Event.(type) { switch evt := pairEventData.Event.(type) {
case *binary.PairEvents_Paired: case *binary.PairEvents_Paired:
callbackErr := c.completePairing(evt.Paired) c.completePairing(evt.Paired)
if callbackErr != nil {
panic(callbackErr)
}
case *binary.PairEvents_Revoked: case *binary.PairEvents_Revoked:
c.Logger.Debug().Any("data", evt).Msg("Revoked Device") c.Logger.Debug().Any("data", evt).Msg("Revoked Device")
c.triggerEvent(evt.Revoked) c.triggerEvent(evt.Revoked)
@ -29,16 +28,15 @@ func (c *Client) handlePairingEvent(response *pblite.Response) {
} }
} }
func (c *Client) completePairing(data *binary.PairedData) error { func (c *Client) completePairing(data *binary.PairedData) {
c.updateTachyonAuthToken(data.GetTokenData().GetTachyonAuthToken(), data.GetTokenData().GetTTL()) c.updateTachyonAuthToken(data.GetTokenData().GetTachyonAuthToken(), data.GetTokenData().GetTTL())
c.AuthData.Mobile = data.Mobile c.AuthData.Mobile = data.Mobile
c.AuthData.Browser = data.Browser c.AuthData.Browser = data.Browser
c.triggerEvent(&events.PairSuccessful{PairedData: data}) c.triggerEvent(&events.PairSuccessful{PairedData: data})
reconnectErr := c.Reconnect() err := c.Reconnect()
if reconnectErr != nil { if err != nil {
return reconnectErr c.triggerEvent(&events.ListenFatalError{Error: fmt.Errorf("failed to reconnect after pair success: %w", err)})
} }
return nil
} }