Skip to content

Commit

Permalink
connectors API: improve error handling (#3443)
Browse files Browse the repository at this point in the history
* connectors API: improve error handling

* iterate on types

* re-align types

* WithConnectorsAPIErrorResponse

* move auth.ts to apiError

* lint

* nit

* fix typo

* nit typeguard
  • Loading branch information
spolu authored Jan 26, 2024
1 parent 56ea8dd commit d45324e
Show file tree
Hide file tree
Showing 37 changed files with 280 additions and 282 deletions.
10 changes: 6 additions & 4 deletions connectors/src/api/connector_config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";
import { isLeft } from "fp-ts/lib/Either";
import * as t from "io-ts";
Expand All @@ -9,16 +10,17 @@ import {
} from "@connectors/connectors";
import { Connector } from "@connectors/lib/models";
import { apiError, withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";

const ConfigSetReqBodySchema = t.type({
configValue: t.string,
});
type ConfigSetReqBody = t.TypeOf<typeof ConfigSetReqBodySchema>;

type ConfigGetResBody =
| { connectorId: number; configKey: string; configValue: string }
| ConnectorsAPIErrorResponse;
type ConfigGetResBody = WithConnectorsAPIErrorReponse<{
connectorId: number;
configKey: string;
configValue: string;
}>;

const _getConnectorConfig = async (
req: Request<{ connector_id: string; config_key: string }>,
Expand Down
4 changes: 2 additions & 2 deletions connectors/src/api/create_connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
CreateConnectorOAuthRequestBodySchema,
CreateConnectorUrlRequestBodySchema,
Result,
WithConnectorsAPIErrorReponse,
} from "@dust-tt/types";
import {
assertNever,
Expand All @@ -24,9 +25,8 @@ import { errorFromAny } from "@connectors/lib/error";
import { Connector } from "@connectors/lib/models";
import logger from "@connectors/logger/logger";
import { apiError, withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";

type ConnectorCreateResBody = ConnectorType | ConnectorsAPIErrorResponse;
type ConnectorCreateResBody = WithConnectorsAPIErrorReponse<ConnectorType>;

const provider2createConnectorType: Record<ConnectorProvider, "oauth" | "url"> =
{
Expand Down
4 changes: 2 additions & 2 deletions connectors/src/api/delete_connector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";

import {
Expand All @@ -7,14 +8,13 @@ import {
import { Connector } from "@connectors/lib/models";
import { terminateAllWorkflowsForConnectorId } from "@connectors/lib/temporal";
import { apiError, withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";

type ConnectorDeleteReqBody = {
dataSourceName: string;
workspaceId: string;
};

type ConnectorDeleteResBody = { success: true } | ConnectorsAPIErrorResponse;
type ConnectorDeleteResBody = WithConnectorsAPIErrorReponse<{ success: true }>;

const _deleteConnectorAPIHandler = async (
req: Request<
Expand Down
8 changes: 5 additions & 3 deletions connectors/src/api/get_connector.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { ConnectorType } from "@dust-tt/types";
import type {
ConnectorType,
WithConnectorsAPIErrorReponse,
} from "@dust-tt/types";
import type { Request, Response } from "express";

import { Connector } from "@connectors/lib/models";
import { GithubDiscussion, GithubIssue } from "@connectors/lib/models/github";
import { NotionPage } from "@connectors/lib/models/notion";
import { apiError, withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";

type GetConnectorRes = ConnectorType | ConnectorsAPIErrorResponse;
type GetConnectorRes = WithConnectorsAPIErrorReponse<ConnectorType>;

const _getConnector = async (
req: Request<{ connector_id: string }, GetConnectorRes, undefined>,
Expand Down
8 changes: 4 additions & 4 deletions connectors/src/api/get_connector_permissions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";

import { RETRIEVE_CONNECTOR_PERMISSIONS_BY_TYPE } from "@connectors/connectors";
import { Connector } from "@connectors/lib/models";
import { apiError, withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";
import type {
ConnectorPermission,
ConnectorResource,
} from "@connectors/types/resources";

type GetConnectorPermissionsRes =
| { resources: ConnectorResource[] }
| ConnectorsAPIErrorResponse;
type GetConnectorPermissionsRes = WithConnectorsAPIErrorReponse<{
resources: ConnectorResource[];
}>;

const _getConnectorPermissions = async (
req: Request<{ connector_id: string }, GetConnectorPermissionsRes, undefined>,
Expand Down
5 changes: 3 additions & 2 deletions connectors/src/api/get_resources_parents.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";
import { zip } from "fp-ts/lib/Array";
import { isLeft } from "fp-ts/lib/Either";
Expand All @@ -17,12 +18,12 @@ export type GetResourcesParentsRequestBody = t.TypeOf<
typeof GetResourcesParentsRequestBodySchema
>;

type GetResourcesParentsResponseBody = {
type GetResourcesParentsResponseBody = WithConnectorsAPIErrorReponse<{
resources: {
internalId: string;
parents: string[] | null;
}[];
};
}>;

const _getResourcesParents = async (
req: Request<
Expand Down
5 changes: 3 additions & 2 deletions connectors/src/api/get_resources_titles.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";
import { isLeft } from "fp-ts/lib/Either";
import * as t from "io-ts";
Expand All @@ -15,12 +16,12 @@ type GetResourcesTitlesRequestBody = t.TypeOf<
typeof GetResourcesTitlesRequestBodySchema
>;

type GetResourcesTitlesResponseBody = {
type GetResourcesTitlesResponseBody = WithConnectorsAPIErrorReponse<{
resources: {
internalId: string;
title: string | null;
}[];
};
}>;

const _getResourcesTitles = async (
req: Request<
Expand Down
8 changes: 4 additions & 4 deletions connectors/src/api/resume_connector.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";

import { RESUME_CONNECTOR_BY_TYPE } from "@connectors/connectors";
import { errorFromAny } from "@connectors/lib/error";
import { Connector } from "@connectors/lib/models";
import logger from "@connectors/logger/logger";
import { apiError, withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";

type ConnectorResumeResBody =
| { connectorId: string }
| ConnectorsAPIErrorResponse;
type ConnectorResumeResBody = WithConnectorsAPIErrorReponse<{
connectorId: string;
}>;

const _resumeConnectorAPIHandler = async (
req: Request<{ connector_id: string }, ConnectorResumeResBody>,
Expand Down
8 changes: 4 additions & 4 deletions connectors/src/api/set_connector_permissions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";
import { isLeft } from "fp-ts/lib/Either";
import * as t from "io-ts";
Expand All @@ -6,11 +7,10 @@ import * as reporter from "io-ts-reporters";
import { SET_CONNECTOR_PERMISSIONS_BY_TYPE } from "@connectors/connectors";
import { Connector } from "@connectors/lib/models";
import { apiError, withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";

type SetConnectorPermissionsRes =
| { success: true }
| ConnectorsAPIErrorResponse;
type SetConnectorPermissionsRes = WithConnectorsAPIErrorReponse<{
success: true;
}>;

const SetConnectorPermissionsRequestBodySchema = t.type({
resources: t.array(
Expand Down
30 changes: 12 additions & 18 deletions connectors/src/api/slack_channels_linked_with_agent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";
import { isLeft } from "fp-ts/lib/Either";
import * as t from "io-ts";
Expand All @@ -6,7 +7,6 @@ import { Op } from "sequelize";

import { joinChannel } from "@connectors/connectors/slack/lib/channels";
import { getChannels } from "@connectors/connectors/slack/temporal/activities";
import type { APIErrorWithStatusCode } from "@connectors/lib/error";
import { sequelize_conn } from "@connectors/lib/models";
import { SlackChannel } from "@connectors/lib/models/slack";
import { apiError, withLogging } from "@connectors/logger/withlogging";
Expand All @@ -21,9 +21,9 @@ type PatchSlackChannelsLinkedWithAgentReqBody = t.TypeOf<
typeof PatchSlackChannelsLinkedWithAgentReqBodySchema
>;

type PatchSlackChannelsLinkedWithAgentResBody =
| { success: true }
| APIErrorWithStatusCode;
type PatchSlackChannelsLinkedWithAgentResBody = WithConnectorsAPIErrorReponse<{
success: true;
}>;

const _patchSlackChannelsLinkedWithAgentHandler = async (
req: Request<
Expand Down Expand Up @@ -152,22 +152,16 @@ export const patchSlackChannelsLinkedWithAgentHandler = withLogging(
_patchSlackChannelsLinkedWithAgentHandler
);

type GetSlackChannelsLinkedWithAgentResBody =
| {
slackChannels: {
slackChannelId: string;
slackChannelName: string;
agentConfigurationId: string;
}[];
}
| APIErrorWithStatusCode;
type GetSlackChannelsLinkedWithAgentResBody = WithConnectorsAPIErrorReponse<{
slackChannels: {
slackChannelId: string;
slackChannelName: string;
agentConfigurationId: string;
}[];
}>;

const _getSlackChannelsLinkedWithAgentHandler = async (
req: Request<
Record<string, string>,
{ slackChannelIds: string[] } | APIErrorWithStatusCode,
undefined
>,
req: Request<Record<string, string>>,
res: Response<GetSlackChannelsLinkedWithAgentResBody>
) => {
const { connector_id: connectorId } = req.query;
Expand Down
8 changes: 4 additions & 4 deletions connectors/src/api/stop_connector.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";

import { STOP_CONNECTOR_BY_TYPE } from "@connectors/connectors";
import { errorFromAny } from "@connectors/lib/error";
import { Connector } from "@connectors/lib/models";
import logger from "@connectors/logger/logger";
import { apiError, withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";

type ConnectorStopResBody =
| { connectorId: string }
| ConnectorsAPIErrorResponse;
type ConnectorStopResBody = WithConnectorsAPIErrorReponse<{
connectorId: string;
}>;

const _stopConnectorAPIHandler = async (
req: Request<{ connector_id: string }, ConnectorStopResBody>,
Expand Down
7 changes: 5 additions & 2 deletions connectors/src/api/sync_connector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";

import { SYNC_CONNECTOR_BY_TYPE } from "@connectors/connectors";
import { Connector } from "@connectors/lib/models";
import { withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";

type GetSyncStatusRes = { workflowId: string } | ConnectorsAPIErrorResponse;
type GetSyncStatusRes = WithConnectorsAPIErrorReponse<{ workflowId: string }>;

const _syncConnectorAPIHandler = async (
req: Request<{ connector_id: string }, GetSyncStatusRes, undefined>,
Expand All @@ -14,6 +14,7 @@ const _syncConnectorAPIHandler = async (
if (!req.params.connector_id) {
res.status(400).send({
error: {
type: "invalid_request_error",
message: `Missing required parameters. Required : connector_id`,
},
});
Expand All @@ -25,6 +26,7 @@ const _syncConnectorAPIHandler = async (
if (!connector) {
res.status(404).send({
error: {
type: "connector_not_found",
message: `Connector with id ${req.params.connector_id} not found`,
},
});
Expand All @@ -37,6 +39,7 @@ const _syncConnectorAPIHandler = async (
if (launchRes.isErr()) {
res.status(500).send({
error: {
type: "internal_server_error",
message: launchRes.error.message,
},
});
Expand Down
20 changes: 7 additions & 13 deletions connectors/src/api/update_connector.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import type { Request, Response } from "express";

import { UPDATE_CONNECTOR_BY_TYPE } from "@connectors/connectors";
import { Connector } from "@connectors/lib/models";
import { apiError, withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";

type ConnectorUpdateReqBody = {
connectionId?: string | null;
};
type ConnectorUpdateResBody =
| { connectorId: string }
| ConnectorsAPIErrorResponse;
type ConnectorUpdateResBody = WithConnectorsAPIErrorReponse<{
connectorId: string;
}>;

const _getConnectorUpdateAPIHandler = async (
req: Request<{ connector_id: string }, ConnectorUpdateReqBody>,
Expand Down Expand Up @@ -44,23 +44,17 @@ const _getConnectorUpdateAPIHandler = async (
});

if (updateRes.isErr()) {
const errorRes = updateRes as { error: ConnectorsAPIErrorResponse };
const error = errorRes.error.error;

if (error.type === "connector_oauth_target_mismatch") {
if (updateRes.error.type === "connector_oauth_target_mismatch") {
return apiError(req, res, {
api_error: {
type: error.type,
message: error.message,
},
api_error: updateRes.error,
status_code: 401,
});
} else {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: `Could not update the connector: ${error.message}`,
message: `Could not update the connector: ${updateRes.error.message}`,
},
});
}
Expand Down
5 changes: 3 additions & 2 deletions connectors/src/api/webhooks/webhook_github.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import { assertNever } from "@dust-tt/types";
import type { Request, Response } from "express";
import { isLeft } from "fp-ts/lib/Either";
Expand Down Expand Up @@ -29,7 +30,6 @@ import {
} from "@connectors/lib/models/github";
import mainLogger from "@connectors/logger/logger";
import { withLogging } from "@connectors/logger/withlogging";
import type { ConnectorsAPIErrorResponse } from "@connectors/types/errors";

const HANDLED_WEBHOOKS = {
installation_repositories: new Set(["added", "removed"]),
Expand All @@ -42,7 +42,7 @@ const HANDLED_WEBHOOKS = {

const logger = mainLogger.child({ provider: "github" });

type GithubWebhookResBody = null | ConnectorsAPIErrorResponse;
type GithubWebhookResBody = WithConnectorsAPIErrorReponse<null>;

const _webhookGithubAPIHandler = async (
req: Request<
Expand All @@ -59,6 +59,7 @@ const _webhookGithubAPIHandler = async (
if (!event || typeof event !== "string") {
return res.status(400).json({
error: {
type: "invalid_request_error",
message: "Missing `x-github-event` header",
},
});
Expand Down
4 changes: 2 additions & 2 deletions connectors/src/api/webhooks/webhook_google_drive.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { WithConnectorsAPIErrorReponse } from "@dust-tt/types";
import { RateLimitError } from "@dust-tt/types";
import type { Request, Response } from "express";

import { launchGoogleDriveIncrementalSyncWorkflow } from "@connectors/connectors/google_drive/temporal/client";
import type { APIErrorWithStatusCode } from "@connectors/lib/error";
import { GoogleDriveWebhook } from "@connectors/lib/models/google_drive";
import logger from "@connectors/logger/logger";
import { apiError, withLogging } from "@connectors/logger/withlogging";

type GoogleDriveWebhookResBody = null | APIErrorWithStatusCode;
type GoogleDriveWebhookResBody = WithConnectorsAPIErrorReponse<null>;

const _webhookGoogleDriveAPIHandler = async (
req: Request<Record<string, string>, GoogleDriveWebhookResBody>,
Expand Down
Loading

0 comments on commit d45324e

Please sign in to comment.