Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/consistent services and error handlers #2101

Merged
merged 20 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions packages/server/src/controllers/apikey/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response, NextFunction } from 'express'
import { StatusCodes } from 'http-status-codes'
import { ApiError } from '../../errors/apiError'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import apikeyService from '../../services/apikey'

// Get api keys
Expand All @@ -15,8 +15,8 @@ const getAllApiKeys = async (req: Request, res: Response, next: NextFunction) =>

const createApiKey = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.body.keyName === 'undefined' || req.body.keyName === '') {
throw new ApiError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.createApiKey - keyName not provided!`)
if (typeof req.body === 'undefined' || !req.body.keyName) {
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.createApiKey - keyName not provided!`)
}
const apiResponse = await apikeyService.createApiKey(req.body.keyName)
return res.json(apiResponse)
Expand All @@ -28,11 +28,11 @@ const createApiKey = async (req: Request, res: Response, next: NextFunction) =>
// Update api key
const updateApiKey = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
new ApiError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.updateApiKey - id not provided!`)
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.updateApiKey - id not provided!`)
}
if (typeof req.body.keyName === 'undefined' || req.body.keyName === '') {
new ApiError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.updateApiKey - keyName not provided!`)
if (typeof req.body === 'undefined' || !req.body.keyName) {
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.updateApiKey - keyName not provided!`)
}
const apiResponse = await apikeyService.updateApiKey(req.params.id, req.body.keyName)
return res.json(apiResponse)
Expand All @@ -44,8 +44,8 @@ const updateApiKey = async (req: Request, res: Response, next: NextFunction) =>
// Delete api key
const deleteApiKey = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
new ApiError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.deleteApiKey - id not provided!`)
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.deleteApiKey - id not provided!`)
}
const apiResponse = await apikeyService.deleteApiKey(req.params.id)
return res.json(apiResponse)
Expand All @@ -57,13 +57,10 @@ const deleteApiKey = async (req: Request, res: Response, next: NextFunction) =>
// Verify api key
const verifyApiKey = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.apiKey === 'undefined' || req.params.apiKey === '') {
new ApiError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.verifyApiKey - apiKey not provided!`)
if (typeof req.params === 'undefined' || !req.params.apiKey) {
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.verifyApiKey - apiKey not provided!`)
}
const apiResponse = await apikeyService.verifyApiKey(req.params.apiKey)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
Expand Down
58 changes: 30 additions & 28 deletions packages/server/src/controllers/assistants/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Request, Response, NextFunction } from 'express'
import assistantsService from '../../services/assistants'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes'

const creatAssistant = async (req: Request, res: Response, next: NextFunction) => {
const createAssistant = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.body === 'undefined' || req.body === '') {
throw new Error(`Error: assistantsController.creatAssistant - body not provided!`)
}
const apiResponse = await assistantsService.creatAssistant(req.body)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: assistantsController.createAssistant - body not provided!`
)
}
const apiResponse = await assistantsService.createAssistant(req.body)
return res.json(apiResponse)
} catch (error) {
next(error)
Expand All @@ -18,13 +20,13 @@ const creatAssistant = async (req: Request, res: Response, next: NextFunction) =

const deleteAssistant = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: assistantsController.deleteAssistant - id not provided!`)
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: assistantsController.deleteAssistant - id not provided!`
)
}
const apiResponse = await assistantsService.deleteAssistant(req.params.id, req.query.isDeleteBoth)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
Expand All @@ -34,9 +36,6 @@ const deleteAssistant = async (req: Request, res: Response, next: NextFunction)
const getAllAssistants = async (req: Request, res: Response, next: NextFunction) => {
try {
const apiResponse = await assistantsService.getAllAssistants()
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
Expand All @@ -45,13 +44,13 @@ const getAllAssistants = async (req: Request, res: Response, next: NextFunction)

const getAssistantById = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: assistantsController.getAssistantById - id not provided!`)
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: assistantsController.getAssistantById - id not provided!`
)
}
const apiResponse = await assistantsService.getAssistantById(req.params.id)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
Expand All @@ -60,24 +59,27 @@ const getAssistantById = async (req: Request, res: Response, next: NextFunction)

const updateAssistant = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: assistantsController.updateAssistant - id not provided!`)
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: assistantsController.updateAssistant - id not provided!`
)
}
if (typeof req.body === 'undefined' || req.body === '') {
throw new Error(`Error: assistantsController.updateAssistant - body not provided!`)
if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: assistantsController.updateAssistant - body not provided!`
)
}
const apiResponse = await assistantsService.updateAssistant(req.params.id, req.body)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
}
}

export default {
creatAssistant,
createAssistant,
deleteAssistant,
getAllAssistants,
getAssistantById,
Expand Down
32 changes: 17 additions & 15 deletions packages/server/src/controllers/chat-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import { clearSessionMemory } from '../../utils'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { FindOptionsWhere } from 'typeorm'
import { ChatMessage } from '../../database/entities/ChatMessage'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes'

const createChatMessage = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.body === 'undefined' || req.body === '') {
throw new Error('Error: chatMessagesController.createChatMessage - request body not provided!')
if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
'Error: chatMessagesController.createChatMessage - request body not provided!'
)
}
const apiResponse = await chatMessagesService.createChatMessage(req.body)
return res.json(apiResponse)
Expand Down Expand Up @@ -44,8 +49,11 @@ const getAllChatMessages = async (req: Request, res: Response, next: NextFunctio
const startDate = req.query?.startDate as string | undefined
const endDate = req.query?.endDate as string | undefined
const feedback = req.query?.feedback as boolean | undefined
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: chatMessageController.getAllChatMessages - id not provided!`)
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: chatMessageController.getAllChatMessages - id not provided!`
)
}
const apiResponse = await chatMessagesService.getAllChatMessages(
req.params.id,
Expand All @@ -59,9 +67,6 @@ const getAllChatMessages = async (req: Request, res: Response, next: NextFunctio
messageId,
feedback
)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
Expand Down Expand Up @@ -90,9 +95,6 @@ const getAllInternalChatMessages = async (req: Request, res: Response, next: Nex
messageId,
feedback
)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
Expand All @@ -103,8 +105,11 @@ const getAllInternalChatMessages = async (req: Request, res: Response, next: Nex
const removeAllChatMessages = async (req: Request, res: Response, next: NextFunction) => {
try {
const appServer = getRunningExpressApp()
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error('Error: chatMessagesController.removeAllChatMessages - id not provided!')
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
'Error: chatMessagesController.removeAllChatMessages - id not provided!'
)
}
const chatflowid = req.params.id
const chatflow = await chatflowsService.getChatflowById(req.params.id)
Expand Down Expand Up @@ -139,9 +144,6 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
if (sessionId) deleteOptions.sessionId = sessionId
if (chatType) deleteOptions.chatType = chatType
const apiResponse = await chatMessagesService.removeAllChatMessages(chatId, chatflowid, deleteOptions)
if (apiResponse.executionError) {
res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
Expand Down
Loading
Loading