Skip to content

Commit

Permalink
Better handle possible error in resource deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd committed Feb 20, 2024
1 parent efbf127 commit 620a58d
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 30 deletions.
11 changes: 9 additions & 2 deletions connectors/src/connectors/confluence/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}

Expand Down
15 changes: 7 additions & 8 deletions connectors/src/connectors/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
9 changes: 8 additions & 1 deletion connectors/src/connectors/google_drive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions connectors/src/connectors/intercom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
11 changes: 9 additions & 2 deletions connectors/src/connectors/notion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
9 changes: 8 additions & 1 deletion connectors/src/connectors/slack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 8 additions & 1 deletion connectors/src/connectors/webcrawler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 7 additions & 2 deletions connectors/src/lib/nango_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`));
}
}
3 changes: 2 additions & 1 deletion connectors/src/resources/base_resource.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Result } from "@dust-tt/types";
import type { Attributes, Model, ModelStatic, Transaction } from "sequelize";

interface BaseResourceConstructor<T extends BaseResource<M>, M extends Model> {
Expand Down Expand Up @@ -41,7 +42,7 @@ export abstract class BaseResource<M extends Model> {
return new this(this.model, blob.get());
}

abstract delete(transaction?: Transaction): Promise<void>;
abstract delete(transaction?: Transaction): Promise<Result<undefined, Error>>;

async update(
blob: Partial<Attributes<M>>
Expand Down
27 changes: 17 additions & 10 deletions connectors/src/resources/connector_resource.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -43,16 +44,22 @@ export class ConnectorResource extends BaseResource<ConnectorModel> {
);
}

async delete(): Promise<void> {
async delete(): Promise<Result<undefined, Error>> {
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);
}
});
}
}

0 comments on commit 620a58d

Please sign in to comment.