Skip to content

Commit

Permalink
Better handling of resumeSession errors
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Jan 4, 2024
1 parent 5e7b013 commit 75dbfa4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
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) {
// `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

0 comments on commit 75dbfa4

Please sign in to comment.