@@ -111,11 +111,17 @@ async def connect_connector(
111111 )
112112 connector .state = ConnectorState .auth_required
113113 else :
114+ logger .error ("Connector failed" , exc_info = True )
115+ try :
116+ error = (await err .response .aread ()).decode (err .response .encoding or "utf-8" )
117+ except Exception :
118+ error = "Connector has returned an error"
114119 raise PlatformError (
115- ( await err . response . aread ()). decode ( err . response . encoding or "utf-8" ) ,
120+ error ,
116121 status_code = status .HTTP_502_BAD_GATEWAY ,
117122 ) from err
118123 elif isinstance (err , httpx .RequestError ):
124+ logger .error ("Connector failed" , exc_info = True )
119125 raise PlatformError (
120126 "Unable to establish connection with the connector" ,
121127 status_code = status .HTTP_504_GATEWAY_TIMEOUT ,
@@ -133,9 +139,10 @@ async def disconnect_connector(self, *, connector_id: UUID, user: User | None =
133139 async with self ._uow () as uow :
134140 connector = await uow .connectors .get (connector_id = connector_id , user_id = user .id if user else None )
135141
136- if connector .state not in (ConnectorState .connected , ConnectorState .auth_required ):
142+ if connector .state not in (ConnectorState .connected , ConnectorState .disconnected , ConnectorState . auth_required ):
137143 raise PlatformError (
138- "Connector must be in connected or auth_required state" , status_code = status .HTTP_400_BAD_REQUEST
144+ "Connector must be in connected, disconnected or auth_required state" ,
145+ status_code = status .HTTP_400_BAD_REQUEST ,
139146 )
140147
141148 await self ._revoke_auth_token (connector = connector )
@@ -239,15 +246,18 @@ async def refresh_connector(self, *, connector_id: UUID, user: User | None = Non
239246 async with self ._uow () as uow :
240247 connector = await uow .connectors .get (connector_id = connector_id , user_id = user .id if user else None )
241248
242- if connector .state != ConnectorState .connected :
249+ if connector .state not in ( ConnectorState .connected , ConnectorState . disconnected ) :
243250 return
244251
245252 try :
246253 await self .probe_connector (connector = connector )
254+ connector .state = ConnectorState .connected
255+ connector .disconnect_reason = None
247256 except Exception as err :
248- await self ._revoke_auth_token (connector = connector )
249- if connector .auth :
250- connector .auth .flow = None
257+ if isinstance (err , httpx .HTTPStatusError ) and err .response .status_code == status .HTTP_401_UNAUTHORIZED :
258+ await self ._revoke_auth_token (connector = connector )
259+ if connector .auth :
260+ connector .auth .flow = None
251261 connector .state = ConnectorState .disconnected
252262 connector .disconnect_reason = str (err )
253263 finally :
0 commit comments