From 37bb367adfc0086f7d4ae905eaf0148a19d0f96d Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Tue, 2 Jan 2024 16:55:58 -0600 Subject: [PATCH] Specifically emit expired event for resumption failures --- .changeset/loud-apes-listen.md | 7 +++++++ packages/api/src/agent.ts | 14 ++++++++------ packages/api/src/types.ts | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 .changeset/loud-apes-listen.md diff --git a/.changeset/loud-apes-listen.md b/.changeset/loud-apes-listen.md new file mode 100644 index 00000000000..91829131fe6 --- /dev/null +++ b/.changeset/loud-apes-listen.md @@ -0,0 +1,7 @@ +--- +'@atproto/api': patch +--- + +Add additional `AtpSessionEvent` event, `unknown`, and implements it within +`resumeSession`. This is used for unknown exceptions, such as network +connectivity issues. diff --git a/packages/api/src/agent.ts b/packages/api/src/agent.ts index aea3cce9d4b..dcf3845f223 100644 --- a/packages/api/src/agent.ts +++ b/packages/api/src/agent.ts @@ -166,16 +166,18 @@ export class AtpAgent { 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) { 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 } } diff --git a/packages/api/src/types.ts b/packages/api/src/types.ts index c0f78bfaafc..978c5759aff 100644 --- a/packages/api/src/types.ts +++ b/packages/api/src/types.ts @@ -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