Skip to content

Commit

Permalink
Improve handling of resumeSession failures (bluesky-social#2010)
Browse files Browse the repository at this point in the history
* Better handling of resumeSession errors

* Update test

* Format

* Pare back to only necessary

* Update handling for 500s

* Should really be update

* Update logic from feedback

* Update tests

* Feedback

* Revert debug change

* Changeset

* Bump minor
  • Loading branch information
estrattonbailey authored Jan 5, 2024
1 parent 5560b7a commit 1406773
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .changeset/chilled-ghosts-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@atproto/api': minor
---

Improve `resumeSession` event emission. It will no longer double emit when some
requests fail, and the `create-failed` event has been replaced by `expired`
where appropriate, and with a new event `network-error` where appropriate or an
unknown error occurs.
34 changes: 26 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,41 @@ 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?.('update', this.session)
return res
} catch (e) {
this.session = undefined
throw e
} finally {
if (this.session) {
this._persistSession?.('create', this.session)

if (e instanceof XRPCError) {
/*
* `ExpiredToken` and `InvalidToken` are handled in
* `this_refreshSession`, and emit an `expired` event there.
*
* Everything else is handled here.
*/
if (
[1, 408, 425, 429, 500, 502, 503, 504, 522, 524].includes(e.status)
) {
this._persistSession?.('network-error', undefined)
} else {
this._persistSession?.('expired', 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
4 changes: 2 additions & 2 deletions packages/api/tests/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('agent', () => {

expect(events.length).toEqual(2)
expect(events[0]).toEqual('create')
expect(events[1]).toEqual('create')
expect(events[1]).toEqual('update')
expect(sessions.length).toEqual(2)
expect(sessions[0]?.accessJwt).toEqual(agent1.session?.accessJwt)
expect(sessions[1]?.accessJwt).toEqual(agent2.session?.accessJwt)
Expand Down Expand Up @@ -343,7 +343,7 @@ describe('agent', () => {

expect(events.length).toEqual(2)
expect(events[0]).toEqual('create-failed')
expect(events[1]).toEqual('create-failed')
expect(events[1]).toEqual('network-error')
expect(sessions.length).toEqual(2)
expect(typeof sessions[0]).toEqual('undefined')
expect(typeof sessions[1]).toEqual('undefined')
Expand Down

0 comments on commit 1406773

Please sign in to comment.