Handle thrown errors much better

This commit is contained in:
Eric Bailey
2024-04-08 11:34:34 -05:00
parent 896e67120c
commit 1f9679b920
3 changed files with 34 additions and 27 deletions
+12 -6
View File
@@ -1,11 +1,12 @@
import {useCallback} from 'react'
import {isWeb} from '#/platform/detection'
import {useAnalytics} from '#/lib/analytics/analytics'
import {useSessionApi, SessionAccount} from '#/state/session'
import * as Toast from '#/view/com/util/Toast'
import {useCloseAllActiveElements} from '#/state/util'
import {logger} from '#/logger'
import {isWeb} from '#/platform/detection'
import {SessionAccount, useSessionApi} from '#/state/session'
import {useLoggedOutViewControls} from '#/state/shell/logged-out'
import {useCloseAllActiveElements} from '#/state/util'
import * as Toast from '#/view/com/util/Toast'
import {LogEvents} from '../statsig/statsig'
export function useAccountSwitcher() {
@@ -44,9 +45,14 @@ export function useAccountSwitcher() {
'circle-exclamation',
)
}
} catch (e) {
Toast.show('Sorry! We need you to enter your password.')
} catch (e: any) {
logger.error(`switch account: selectAccount failed`, {
message: e.message,
})
clearCurrentAccount() // back user out to login
setTimeout(() => {
Toast.show('Sorry! We need you to enter your password.')
}, 100)
}
},
[
+17 -9
View File
@@ -5,6 +5,7 @@ import {useLingui} from '@lingui/react'
import {useAnalytics} from '#/lib/analytics/analytics'
import {logEvent} from '#/lib/statsig/statsig'
import {logger} from '#/logger'
import {SessionAccount, useSession, useSessionApi} from '#/state/session'
import {useLoggedOutViewControls} from '#/state/shell/logged-out'
import * as Toast from '#/view/com/util/Toast'
@@ -38,15 +39,22 @@ export const ChooseAccountForm = ({
setShowLoggedOut(false)
Toast.show(_(msg`Already signed in as @${account.handle}`))
} else {
await initSession(account)
logEvent('account:loggedIn', {
logContext: 'ChooseAccountForm',
withPassword: false,
})
track('Sign In', {resumedSession: true})
setTimeout(() => {
Toast.show(_(msg`Signed in as @${account.handle}`))
}, 100)
try {
await initSession(account)
logEvent('account:loggedIn', {
logContext: 'ChooseAccountForm',
withPassword: false,
})
track('Sign In', {resumedSession: true})
setTimeout(() => {
Toast.show(_(msg`Signed in as @${account.handle}`))
}, 100)
} catch (e: any) {
logger.error('choose account: initSession failed', {
message: e.message,
})
onSelectAccount(account)
}
}
} else {
onSelectAccount(account)
+5 -12
View File
@@ -213,6 +213,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
console.log('PERSIST', window.__id, {
event,
refreshJwt: session?.refreshJwt?.slice(-10),
agent: agent.session?.refreshJwt.slice(-10),
})
const expired = event === 'expired' || event === 'create-failed'
@@ -226,7 +227,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
return
}
agent.session = session
const refreshedAccount = agentToSessionAccount(agent)
if (!refreshedAccount) {
@@ -414,19 +414,12 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
{},
logger.DebugContext.session,
)
try {
// will call `persistSession` on `BskyAgent` instance above if success
await networkRetry(1, () => agent.resumeSession(prevSession))
setCurrentAgent(agent)
} catch (e) {
// this can fail on bad connections as well, so `clearCurrentAccount`
// bumps them out to login, but doesn't rug tokens
logger.error(`session: resumeSession failed`, {message: e})
clearCurrentAccount()
}
// will call `persistSession` on `BskyAgent` instance above if success
await networkRetry(1, () => agent.resumeSession(prevSession))
setCurrentAgent(agent)
}
},
[upsertAndPersistAccount, clearCurrentAccount, persistSession],
[upsertAndPersistAccount, persistSession],
)
const resumeSession = React.useCallback<ApiContext['resumeSession']>(