From 620a58d5ceaf66f2b7e1a7f0738e0dbbed5280ee Mon Sep 17 00:00:00 2001 From: Flavien David Date: Tue, 20 Feb 2024 13:14:04 +0100 Subject: [PATCH] Better handle possible error in resource deletion --- connectors/src/connectors/confluence/index.ts | 11 ++++++-- connectors/src/connectors/github/index.ts | 15 +++++------ .../src/connectors/google_drive/index.ts | 9 ++++++- connectors/src/connectors/intercom/index.ts | 11 ++++++-- connectors/src/connectors/notion/index.ts | 11 ++++++-- connectors/src/connectors/slack/index.ts | 9 ++++++- connectors/src/connectors/webcrawler/index.ts | 9 ++++++- connectors/src/lib/nango_client.ts | 9 +++++-- connectors/src/resources/base_resource.ts | 3 ++- .../src/resources/connector_resource.ts | 27 ++++++++++++------- 10 files changed, 84 insertions(+), 30 deletions(-) diff --git a/connectors/src/connectors/confluence/index.ts b/connectors/src/connectors/confluence/index.ts index 91a269d3914e..d723c64fec7c 100644 --- a/connectors/src/connectors/confluence/index.ts +++ b/connectors/src/connectors/confluence/index.ts @@ -235,8 +235,6 @@ export async function cleanupConfluenceConnector( return new Err(new Error("Connector not found")); } - await connector.delete(); - const nangoRes = await nangoDeleteConnection( connector.connectionId, getRequiredNangoConfluenceConnectorId() @@ -245,6 +243,15 @@ export async function cleanupConfluenceConnector( throw nangoRes.error; } + const res = await connector.delete(); + if (res.isErr()) { + logger.error( + { connectorId, error: res.error }, + "Error cleaning up Confluence connector." + ); + return res; + } + return new Ok(undefined); } diff --git a/connectors/src/connectors/github/index.ts b/connectors/src/connectors/github/index.ts index 6f09054e6d45..c5e4907e869b 100644 --- a/connectors/src/connectors/github/index.ts +++ b/connectors/src/connectors/github/index.ts @@ -219,17 +219,16 @@ export async function cleanupGithubConnector( return new Err(new Error("Connector not found")); } - try { - await connector.delete(); - - return new Ok(undefined); - } catch (err) { + const res = await connector.delete(); + if (res.isErr()) { logger.error( - { connectorId, error: err }, - "Error cleaning up github connector" + { connectorId, error: res.error }, + "Error cleaning up Github connector." ); - return new Err(err as Error); + return res; } + + return new Ok(undefined); } export async function retrieveGithubConnectorPermissions({ diff --git a/connectors/src/connectors/google_drive/index.ts b/connectors/src/connectors/google_drive/index.ts index 22de34368ee1..e5751efd7b95 100644 --- a/connectors/src/connectors/google_drive/index.ts +++ b/connectors/src/connectors/google_drive/index.ts @@ -293,7 +293,14 @@ export async function cleanupGoogleDriveConnector( } } - await connector.delete(); + const res = await connector.delete(); + if (res.isErr()) { + logger.error( + { connectorId, error: res.error }, + "Error cleaning up Google Drive connector." + ); + return res; + } return new Ok(undefined); } diff --git a/connectors/src/connectors/intercom/index.ts b/connectors/src/connectors/intercom/index.ts index 13e54c66be9d..e119d87311de 100644 --- a/connectors/src/connectors/intercom/index.ts +++ b/connectors/src/connectors/intercom/index.ts @@ -220,8 +220,6 @@ export async function cleanupIntercomConnector( return new Err(new Error("Connector not found")); } - await connector.delete(); - const nangoRes = await nangoDeleteConnection( connector.connectionId, NANGO_INTERCOM_CONNECTOR_ID @@ -230,6 +228,15 @@ export async function cleanupIntercomConnector( throw nangoRes.error; } + const res = await connector.delete(); + if (res.isErr()) { + logger.error( + { connectorId, error: res.error }, + "Error cleaning up Intercom connector." + ); + return res; + } + return new Ok(undefined); } diff --git a/connectors/src/connectors/notion/index.ts b/connectors/src/connectors/notion/index.ts index 70590721c115..bb729c1ca08b 100644 --- a/connectors/src/connectors/notion/index.ts +++ b/connectors/src/connectors/notion/index.ts @@ -325,10 +325,17 @@ export async function cleanupNotionConnector( return new Err(new Error("Connector not found")); } - await connector.delete(); - await deleteNangoConnection(connector.connectionId); + const res = await connector.delete(); + if (res.isErr()) { + logger.error( + { connectorId, error: res.error }, + "Error cleaning up Notion connector." + ); + return res; + } + return new Ok(undefined); } diff --git a/connectors/src/connectors/slack/index.ts b/connectors/src/connectors/slack/index.ts index c3d9721ee9b0..d8fbdceb2ea4 100644 --- a/connectors/src/connectors/slack/index.ts +++ b/connectors/src/connectors/slack/index.ts @@ -344,7 +344,14 @@ export async function cleanupSlackConnector( ); } - await connector.delete(); + const res = await connector.delete(); + if (res.isErr()) { + logger.error( + { connectorId, error: res.error }, + "Error cleaning up Slack connector." + ); + return res; + } return new Ok(undefined); } diff --git a/connectors/src/connectors/webcrawler/index.ts b/connectors/src/connectors/webcrawler/index.ts index ab20dc536d15..d4de0bb25e97 100644 --- a/connectors/src/connectors/webcrawler/index.ts +++ b/connectors/src/connectors/webcrawler/index.ts @@ -278,7 +278,14 @@ export async function cleanupWebcrawlerConnector( throw new Error("Connector not found."); } - await connector.delete(); + const res = await connector.delete(); + if (res.isErr()) { + logger.error( + { connectorId, error: res.error }, + "Error cleaning up Webcrawler connector." + ); + return res; + } return new Ok(undefined); } diff --git a/connectors/src/lib/nango_client.ts b/connectors/src/lib/nango_client.ts index 91e50e1224e7..51a1350fa48c 100644 --- a/connectors/src/lib/nango_client.ts +++ b/connectors/src/lib/nango_client.ts @@ -93,13 +93,18 @@ export async function nangoDeleteConnection( } else { logger.error({ connectionId }, "Could not delete Nango connection."); if (res) { + if (res.status === 404) { + logger.error({ connectionId }, "Connection not found on Nango."); + return new Ok(undefined); + } + return new Err( new Error( `Could not delete connection. ${res.statusText}, ${await res.text()}` ) ); - } else { - return new Err(new Error(`Could not delete connection.`)); } + + return new Err(new Error(`Could not delete connection.`)); } } diff --git a/connectors/src/resources/base_resource.ts b/connectors/src/resources/base_resource.ts index cfd0a56c23b2..2d3a8ffd8c7c 100644 --- a/connectors/src/resources/base_resource.ts +++ b/connectors/src/resources/base_resource.ts @@ -1,3 +1,4 @@ +import type { Result } from "@dust-tt/types"; import type { Attributes, Model, ModelStatic, Transaction } from "sequelize"; interface BaseResourceConstructor, M extends Model> { @@ -41,7 +42,7 @@ export abstract class BaseResource { return new this(this.model, blob.get()); } - abstract delete(transaction?: Transaction): Promise; + abstract delete(transaction?: Transaction): Promise>; async update( blob: Partial> diff --git a/connectors/src/resources/connector_resource.ts b/connectors/src/resources/connector_resource.ts index 6837d1a67338..44540d374db5 100644 --- a/connectors/src/resources/connector_resource.ts +++ b/connectors/src/resources/connector_resource.ts @@ -1,4 +1,5 @@ -import type { ConnectorProvider } from "@dust-tt/types"; +import type { ConnectorProvider, Result } from "@dust-tt/types"; +import { Err, Ok } from "@dust-tt/types"; import type { Attributes, ModelStatic } from "sequelize"; import { BaseResource } from "@connectors/resources/base_resource"; @@ -43,16 +44,22 @@ export class ConnectorResource extends BaseResource { ); } - async delete(): Promise { + async delete(): Promise> { return sequelizeConnection.transaction(async (transaction) => { - await this.providerStrategy.delete(this, transaction); - - await this.model.destroy({ - where: { - id: this.id, - }, - transaction, - }); + try { + await this.providerStrategy.delete(this, transaction); + + await this.model.destroy({ + where: { + id: this.id, + }, + transaction, + }); + + return new Ok(undefined); + } catch (err) { + return new Err(err as Error); + } }); } }