Skip to content

Commit

Permalink
Enforce 500 are internal_server_error (#2498)
Browse files Browse the repository at this point in the history
* Enforce 500 are internal_server_error

* More fix

* Update front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/[mId]/reactions/index.ts

Co-authored-by: Philippe Rolet <[email protected]>

* Update front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/[mId]/reactions/index.ts

Co-authored-by: Philippe Rolet <[email protected]>

* Update front/pages/api/stripe/portal.ts

Co-authored-by: Philippe Rolet <[email protected]>

* Update front/pages/api/stripe/webhook.ts

Co-authored-by: Philippe Rolet <[email protected]>

* Update front/pages/api/stripe/webhook.ts

Co-authored-by: Philippe Rolet <[email protected]>

* lint

---------

Co-authored-by: Philippe Rolet <[email protected]>
  • Loading branch information
spolu and philipperolet authored Nov 13, 2023
1 parent 4a09b1b commit 5c17392
Show file tree
Hide file tree
Showing 22 changed files with 68 additions and 88 deletions.
16 changes: 8 additions & 8 deletions connectors/src/api/bot_enabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@ const _getBotEnabled = async (
) => {
if (!req.params.connector_id) {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: "Missing required parameters. Required: connector_id",
},
status_code: 400,
});
}
const connector = await Connector.findByPk(req.params.connector_id);
if (!connector) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "connector_not_found",
message: "Connector not found",
},
status_code: 404,
});
}
if (connector.type !== "slack") {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: "Connector is not a slack connector",
},
status_code: 400,
});
}

Expand All @@ -54,11 +54,11 @@ const _getBotEnabled = async (

if (botEnabledRes.isErr()) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: `An error occurred while getting the bot status: ${botEnabledRes.error}`,
},
status_code: 500,
});
}
return res.status(200).json({
Expand All @@ -72,30 +72,30 @@ const _setBotEnabled = async (
) => {
if (!req.params.connector_id) {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: "Missing required parameters. Required: connector_id",
},
status_code: 400,
});
}
const connector = await Connector.findByPk(req.params.connector_id);
if (!connector) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "connector_not_found",
message: "Connector not found",
},
status_code: 404,
});
}
if (!req.body || typeof req.body.botEnabled !== "boolean") {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: "Missing required parameters. Required: botEnabled",
},
status_code: 400,
});
}

Expand All @@ -106,11 +106,11 @@ const _setBotEnabled = async (

if (toggleRes.isErr()) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: `An error occurred while toggling the bot: ${toggleRes.error.message}`,
},
status_code: 500,
});
}
return res.status(200).json({
Expand Down
10 changes: 5 additions & 5 deletions connectors/src/api/create_connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ const _createConnectorAPIHandler = async (
!req.body.connectionId
) {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: `Missing required parameters. Required : workspaceAPIKey,
dataSourceName, workspaceId, connectionId`,
},
status_code: 400,
});
}

if (!isConnectorProvider(req.params.connector_provider)) {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "unknown_connector_provider",
message: `Unknown connector provider ${req.params.connector_provider}`,
},
status_code: 400,
});
}

Expand All @@ -67,22 +67,22 @@ const _createConnectorAPIHandler = async (

if (connectorRes.isErr()) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: connectorRes.error.message,
},
status_code: 500,
});
}

const connector = await Connector.findByPk(connectorRes.value);
if (!connector) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: `Created connector not found in database. Connector id: ${connectorRes.value}`,
},
status_code: 500,
});
}

Expand All @@ -101,11 +101,11 @@ const _createConnectorAPIHandler = async (
} catch (e) {
logger.error(errorFromAny(e), "Error in createConnectorAPIHandler");
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: "An unexpected error occured while creating the connector.",
},
status_code: 500,
});
}
};
Expand Down
8 changes: 4 additions & 4 deletions connectors/src/api/delete_connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ const _deleteConnectorAPIHandler = async (
const connector = await Connector.findByPk(req.params.connector_id);
if (!connector) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "connector_not_found",
message: "Connector not found",
},
status_code: 404,
});
}

Expand All @@ -41,33 +41,33 @@ const _deleteConnectorAPIHandler = async (

if (stopRes.isErr()) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: stopRes.error.message,
},
status_code: 500,
});
}

if (!connector) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: "Could not find the connector",
},
status_code: 500,
});
}

const connectorDeleter = DELETE_CONNECTOR_BY_TYPE[connector.type];
const cleanRes = await connectorDeleter(connector.id.toString(), force);
if (cleanRes.isErr()) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: cleanRes.error.message,
},
status_code: 500,
});
}

Expand Down
6 changes: 3 additions & 3 deletions connectors/src/api/get_connector_permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const _getConnectorPermissions = async (
) => {
if (!req.params.connector_id) {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: "Missing required parameters. Required: connector_id",
},
status_code: 400,
});
}

Expand All @@ -50,11 +50,11 @@ const _getConnectorPermissions = async (
const connector = await Connector.findByPk(req.params.connector_id);
if (!connector) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "connector_not_found",
message: "Connector not found",
},
status_code: 404,
});
}

Expand All @@ -69,11 +69,11 @@ const _getConnectorPermissions = async (

if (pRes.isErr()) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: pRes.error.message,
},
status_code: 500,
});
}

Expand Down
6 changes: 3 additions & 3 deletions connectors/src/api/get_resources_parents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ const _getResourcesParents = async (
const connector = await Connector.findByPk(req.params.connector_id);
if (!connector) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "connector_not_found",
message: "Connector not found",
},
status_code: 404,
});
}

const bodyValidation = GetResourcesParentsRequestBodySchema.decode(req.body);
if (isLeft(bodyValidation)) {
const pathError = reporter.formatValidationErrors(bodyValidation.left);
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: `Invalid request body: ${pathError}`,
},
status_code: 400,
});
}

Expand All @@ -72,11 +72,11 @@ const _getResourcesParents = async (
if (parentsResult.isErr()) {
logger.error(parentsResult.error, "Failed to get resource parents");
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: parentsResult.error.message,
},
status_code: 500,
});
}

Expand Down
6 changes: 3 additions & 3 deletions connectors/src/api/get_resources_titles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ const _getResourcesTitles = async (
const connector = await Connector.findByPk(req.params.connector_id);
if (!connector) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "connector_not_found",
message: "Connector not found",
},
status_code: 404,
});
}

const bodyValidation = GetResourcesTitlesRequestBodySchema.decode(req.body);
if (isLeft(bodyValidation)) {
const pathError = reporter.formatValidationErrors(bodyValidation.left);
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: `Invalid request body: ${pathError}`,
},
status_code: 400,
});
}

Expand All @@ -65,11 +65,11 @@ const _getResourcesTitles = async (

if (titlesRes.isErr()) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: titlesRes.error.message,
},
status_code: 500,
});
}

Expand Down
6 changes: 3 additions & 3 deletions connectors/src/api/resume_connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const _resumeConnectorAPIHandler = async (
const connector = await Connector.findByPk(req.params.connector_id);
if (!connector) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "connector_not_found",
message: "Connector not found",
},
status_code: 404,
});
}
const connectorResumer = RESUME_CONNECTOR_BY_TYPE[connector.type];
Expand All @@ -32,11 +32,11 @@ const _resumeConnectorAPIHandler = async (

if (resumeRes.isErr()) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: "Could not resume the connector",
},
status_code: 500,
});
}

Expand All @@ -46,11 +46,11 @@ const _resumeConnectorAPIHandler = async (
} catch (e) {
logger.error(errorFromAny(e), "Failed to resume the connector");
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: "Could not resume the connector",
},
status_code: 500,
});
}
};
Expand Down
Loading

0 comments on commit 5c17392

Please sign in to comment.