Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of resumeSession failures #2010

Merged
merged 13 commits into from
Jan 5, 2024
Next Next commit
Better handling of resumeSession errors
  • Loading branch information
estrattonbailey committed Jan 4, 2024
commit 75dbfa4185e404d6d5641034ddb4c93da0c13d87
23 changes: 15 additions & 8 deletions packages/api/src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ErrorResponseBody, errorResponseBody } from '@atproto/xrpc'
import { defaultFetchHandler } from '@atproto/xrpc'
import { defaultFetchHandler, XRPCError, ResponseType } from '@atproto/xrpc'
import { isValidDidDoc, getPdsEndpoint } from '@atproto/common-web'
import {
AtpBaseClient,
Expand Down Expand Up @@ -159,23 +159,30 @@ export class AtpAgent {
try {
this.session = session
const res = await this.api.com.atproto.server.getSession()
if (!res.success || res.data.did !== this.session.did) {
throw new Error('Invalid session')
if (res.data.did !== this.session.did) {
throw new XRPCError(ResponseType.InvalidRequest, 'Invalid session', 'InvalidDID')
}
this.session.email = res.data.email
this.session.handle = res.data.handle
this.session.emailConfirmed = res.data.emailConfirmed
this._updateApiEndpoint(res.data.didDoc)
this._persistSession?.('create', this.session)
return res
} catch (e) {
this.session = undefined
throw e
} finally {
if (this.session) {
this._persistSession?.('create', this.session)

if (e instanceof XRPCError && e.error) {
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
// `ExpiredToken` and `InvalidToken` are handled by the `this._refreshSession`
if (['AuthMissing', 'InvalidDID'].includes(e.error)) {
this._persistSession?.('expired', undefined)
} else {
this._persistSession?.('network-error', undefined)
}
} else {
this._persistSession?.('create-failed', undefined)
this._persistSession?.('network-error', undefined)
}

throw e
}
}

Expand Down
7 changes: 6 additions & 1 deletion packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { LabelPreference } from './moderation/types'
/**
* Used by the PersistSessionHandler to indicate what change occurred
*/
export type AtpSessionEvent = 'create' | 'create-failed' | 'update' | 'expired'
export type AtpSessionEvent =
| 'create'
| 'create-failed'
| 'update'
| 'expired'
| 'network-error'

/**
* Used by AtpAgent to store active sessions
Expand Down