@@ -78,11 +78,12 @@ export interface OAuth2GrantHandleDeps {
7878 /** The cluster lock, when the deployment has a cluster service. Read at refresh time. */
7979 readonly lock ?: ( ) => ILock | undefined ;
8080 /**
81- * Told when the authorization server refused an organization's grant and
82- * the connection was marked `reconnect_required`, so the plugin re-judges
81+ * Told when a refresh marked an organization's connection
82+ * `reconnect_required`, with the `reason` it stored (whose cause is the
83+ * one that happened on that path), so the plugin logs it and re-judges
8384 * the instance. A throw here is the listener's own.
8485 */
85- readonly onGrantRefused ?: ( organizationId : string ) => void ;
86+ readonly onGrantRefused ?: ( organizationId : string , reason : string ) => void ;
8687 readonly timeoutMs ?: number ;
8788 readonly fetch ?: typeof fetch ;
8889 readonly now ?: ( ) => number ;
@@ -155,6 +156,15 @@ export function reconnectRequiredReason(
155156 + 'uses the new connection (ADR-0143 D4)' ;
156157}
157158
159+ /**
160+ * The dispatch refusal of a connection already marked `reconnect_required`:
161+ * the reason the row stores, whose cause is the one that marked it, and the
162+ * authorization server's refusal only for a row that stores none.
163+ */
164+ function markedReason ( connector : string , record : ConnectorConnectionRecord ) : string {
165+ return record . statusReason ?? reconnectRequiredReason ( connector , record . organizationId ) ;
166+ }
167+
158168/** Create the handle of one `oauth2` instance. */
159169export function createOAuth2GrantHandle ( deps : OAuth2GrantHandleDeps ) : OAuth2GrantHandle {
160170 const { connector, config } = deps ;
@@ -235,7 +245,7 @@ export function createOAuth2GrantHandle(deps: OAuth2GrantHandleDeps): OAuth2Gran
235245 throw unavailable ( `the connection of connector instance '${ connector } ' could not be read now; the next dispatch retries` ) ;
236246 }
237247 if ( ! record ) throw unavailable ( notConnectedReason ( connector , organizationId ) ) ;
238- if ( record . status === 'reconnect_required' ) throw unavailable ( reconnectRequiredReason ( connector , record . organizationId ) ) ;
248+ if ( record . status === 'reconnect_required' ) throw unavailable ( markedReason ( connector , record ) ) ;
239249 return record ;
240250 } ;
241251
@@ -248,7 +258,7 @@ export function createOAuth2GrantHandle(deps: OAuth2GrantHandleDeps): OAuth2Gran
248258 /* the refusal stands either way; the row is re-judged on the next dispatch */
249259 }
250260 try {
251- deps . onGrantRefused ?.( record . organizationId ) ;
261+ deps . onGrantRefused ?.( record . organizationId , reason ) ;
252262 } catch {
253263 /* the listener's throw is its own */
254264 }
@@ -286,7 +296,7 @@ export function createOAuth2GrantHandle(deps: OAuth2GrantHandleDeps): OAuth2Gran
286296 const refreshOnce = async ( organizationId : string ) : Promise < CachedToken > => {
287297 const current = await deps . connections . read ( organizationId , connector ) ;
288298 if ( ! current ) throw unavailable ( notConnectedReason ( connector , organizationId ) ) ;
289- if ( current . status === 'reconnect_required' ) throw unavailable ( reconnectRequiredReason ( connector , organizationId ) ) ;
299+ if ( current . status === 'reconnect_required' ) throw unavailable ( markedReason ( connector , current ) ) ;
290300 let refreshToken : string | undefined ;
291301 try {
292302 refreshToken = await deps . connections . resolveRefreshToken ( current ) ;
0 commit comments