Skip to content

Commit

Permalink
Specifically emit expired event for resumption failures
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Jan 2, 2024
1 parent 2860951 commit 046d4fa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-apes-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@atproto/api': patch
---

Emit `expired` events for failed responses from `agent.resumeSession` instead of `create-failed`. `create-failed` events now reserved for unexpected exceptions, like a network timeout.
16 changes: 10 additions & 6 deletions packages/api/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,27 @@ export class AtpAgent {
try {
this.session = session
const res = await this.api.com.atproto.server.getSession()
console.log('resumeSession', res)
if (!res.success || res.data.did !== this.session.did) {
throw new Error('Invalid session')
}
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) {
} catch (e: any) {
console.error('resumeSession', e)
this.session = undefined
throw e
} finally {
if (this.session) {
this._persistSession?.('create', this.session)
} else {

if (e.message.includes('Invalid session')) {
this._persistSession?.('create-failed', undefined)
} else {
this._persistSession?.('unknown', undefined)
}

throw e
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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' | 'unknown'

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

0 comments on commit 046d4fa

Please sign in to comment.