Skip to content

Commit d7194e8

Browse files
Chore/consistent services and error handlers (#2101)
* Update index.ts * Update buildChatflow.ts * Update index.ts * Update index.ts * Update index.ts * Update index.ts * Update index.ts * Update index.ts * Update index.ts * Update index.ts * Consistency * Rename ApiError to InternalServerError * Use InternalFlowiseError in controllers * Use InternalFlowiseError in services * Catch routes without preconditioned parameters * Reconfigure the route precondition checks * Fix router precondition checks * cleanup status codes, get proper error messages --------- Co-authored-by: Henry <hzj94@hotmail.com>
1 parent 024b2ad commit d7194e8

File tree

98 files changed

+862
-651
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+862
-651
lines changed

packages/server/src/controllers/apikey/index.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response, NextFunction } from 'express'
22
import { StatusCodes } from 'http-status-codes'
3-
import { ApiError } from '../../errors/apiError'
3+
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
44
import apikeyService from '../../services/apikey'
55

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

1616
const createApiKey = async (req: Request, res: Response, next: NextFunction) => {
1717
try {
18-
if (typeof req.body.keyName === 'undefined' || req.body.keyName === '') {
19-
throw new ApiError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.createApiKey - keyName not provided!`)
18+
if (typeof req.body === 'undefined' || !req.body.keyName) {
19+
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.createApiKey - keyName not provided!`)
2020
}
2121
const apiResponse = await apikeyService.createApiKey(req.body.keyName)
2222
return res.json(apiResponse)
@@ -28,11 +28,11 @@ const createApiKey = async (req: Request, res: Response, next: NextFunction) =>
2828
// Update api key
2929
const updateApiKey = async (req: Request, res: Response, next: NextFunction) => {
3030
try {
31-
if (typeof req.params.id === 'undefined' || req.params.id === '') {
32-
new ApiError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.updateApiKey - id not provided!`)
31+
if (typeof req.params === 'undefined' || !req.params.id) {
32+
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.updateApiKey - id not provided!`)
3333
}
34-
if (typeof req.body.keyName === 'undefined' || req.body.keyName === '') {
35-
new ApiError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.updateApiKey - keyName not provided!`)
34+
if (typeof req.body === 'undefined' || !req.body.keyName) {
35+
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.updateApiKey - keyName not provided!`)
3636
}
3737
const apiResponse = await apikeyService.updateApiKey(req.params.id, req.body.keyName)
3838
return res.json(apiResponse)
@@ -44,8 +44,8 @@ const updateApiKey = async (req: Request, res: Response, next: NextFunction) =>
4444
// Delete api key
4545
const deleteApiKey = async (req: Request, res: Response, next: NextFunction) => {
4646
try {
47-
if (typeof req.params.id === 'undefined' || req.params.id === '') {
48-
new ApiError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.deleteApiKey - id not provided!`)
47+
if (typeof req.params === 'undefined' || !req.params.id) {
48+
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.deleteApiKey - id not provided!`)
4949
}
5050
const apiResponse = await apikeyService.deleteApiKey(req.params.id)
5151
return res.json(apiResponse)
@@ -57,13 +57,10 @@ const deleteApiKey = async (req: Request, res: Response, next: NextFunction) =>
5757
// Verify api key
5858
const verifyApiKey = async (req: Request, res: Response, next: NextFunction) => {
5959
try {
60-
if (typeof req.params.apiKey === 'undefined' || req.params.apiKey === '') {
61-
new ApiError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.verifyApiKey - apiKey not provided!`)
60+
if (typeof req.params === 'undefined' || !req.params.apiKey) {
61+
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: apikeyController.verifyApiKey - apiKey not provided!`)
6262
}
6363
const apiResponse = await apikeyService.verifyApiKey(req.params.apiKey)
64-
if (apiResponse.executionError) {
65-
return res.status(apiResponse.status).send(apiResponse.msg)
66-
}
6764
return res.json(apiResponse)
6865
} catch (error) {
6966
next(error)

packages/server/src/controllers/assistants/index.ts

+30-28
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { Request, Response, NextFunction } from 'express'
22
import assistantsService from '../../services/assistants'
3+
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
4+
import { StatusCodes } from 'http-status-codes'
35

4-
const creatAssistant = async (req: Request, res: Response, next: NextFunction) => {
6+
const createAssistant = async (req: Request, res: Response, next: NextFunction) => {
57
try {
6-
if (typeof req.body === 'undefined' || req.body === '') {
7-
throw new Error(`Error: assistantsController.creatAssistant - body not provided!`)
8-
}
9-
const apiResponse = await assistantsService.creatAssistant(req.body)
10-
if (apiResponse.executionError) {
11-
return res.status(apiResponse.status).send(apiResponse.msg)
8+
if (!req.body) {
9+
throw new InternalFlowiseError(
10+
StatusCodes.PRECONDITION_FAILED,
11+
`Error: assistantsController.createAssistant - body not provided!`
12+
)
1213
}
14+
const apiResponse = await assistantsService.createAssistant(req.body)
1315
return res.json(apiResponse)
1416
} catch (error) {
1517
next(error)
@@ -18,13 +20,13 @@ const creatAssistant = async (req: Request, res: Response, next: NextFunction) =
1820

1921
const deleteAssistant = async (req: Request, res: Response, next: NextFunction) => {
2022
try {
21-
if (typeof req.params.id === 'undefined' || req.params.id === '') {
22-
throw new Error(`Error: assistantsController.deleteAssistant - id not provided!`)
23+
if (typeof req.params === 'undefined' || !req.params.id) {
24+
throw new InternalFlowiseError(
25+
StatusCodes.PRECONDITION_FAILED,
26+
`Error: assistantsController.deleteAssistant - id not provided!`
27+
)
2328
}
2429
const apiResponse = await assistantsService.deleteAssistant(req.params.id, req.query.isDeleteBoth)
25-
if (apiResponse.executionError) {
26-
return res.status(apiResponse.status).send(apiResponse.msg)
27-
}
2830
return res.json(apiResponse)
2931
} catch (error) {
3032
next(error)
@@ -34,9 +36,6 @@ const deleteAssistant = async (req: Request, res: Response, next: NextFunction)
3436
const getAllAssistants = async (req: Request, res: Response, next: NextFunction) => {
3537
try {
3638
const apiResponse = await assistantsService.getAllAssistants()
37-
if (apiResponse.executionError) {
38-
return res.status(apiResponse.status).send(apiResponse.msg)
39-
}
4039
return res.json(apiResponse)
4140
} catch (error) {
4241
next(error)
@@ -45,13 +44,13 @@ const getAllAssistants = async (req: Request, res: Response, next: NextFunction)
4544

4645
const getAssistantById = async (req: Request, res: Response, next: NextFunction) => {
4746
try {
48-
if (typeof req.params.id === 'undefined' || req.params.id === '') {
49-
throw new Error(`Error: assistantsController.getAssistantById - id not provided!`)
47+
if (typeof req.params === 'undefined' || !req.params.id) {
48+
throw new InternalFlowiseError(
49+
StatusCodes.PRECONDITION_FAILED,
50+
`Error: assistantsController.getAssistantById - id not provided!`
51+
)
5052
}
5153
const apiResponse = await assistantsService.getAssistantById(req.params.id)
52-
if (apiResponse.executionError) {
53-
return res.status(apiResponse.status).send(apiResponse.msg)
54-
}
5554
return res.json(apiResponse)
5655
} catch (error) {
5756
next(error)
@@ -60,24 +59,27 @@ const getAssistantById = async (req: Request, res: Response, next: NextFunction)
6059

6160
const updateAssistant = async (req: Request, res: Response, next: NextFunction) => {
6261
try {
63-
if (typeof req.params.id === 'undefined' || req.params.id === '') {
64-
throw new Error(`Error: assistantsController.updateAssistant - id not provided!`)
62+
if (typeof req.params === 'undefined' || !req.params.id) {
63+
throw new InternalFlowiseError(
64+
StatusCodes.PRECONDITION_FAILED,
65+
`Error: assistantsController.updateAssistant - id not provided!`
66+
)
6567
}
66-
if (typeof req.body === 'undefined' || req.body === '') {
67-
throw new Error(`Error: assistantsController.updateAssistant - body not provided!`)
68+
if (!req.body) {
69+
throw new InternalFlowiseError(
70+
StatusCodes.PRECONDITION_FAILED,
71+
`Error: assistantsController.updateAssistant - body not provided!`
72+
)
6873
}
6974
const apiResponse = await assistantsService.updateAssistant(req.params.id, req.body)
70-
if (apiResponse.executionError) {
71-
return res.status(apiResponse.status).send(apiResponse.msg)
72-
}
7375
return res.json(apiResponse)
7476
} catch (error) {
7577
next(error)
7678
}
7779
}
7880

7981
export default {
80-
creatAssistant,
82+
createAssistant,
8183
deleteAssistant,
8284
getAllAssistants,
8385
getAssistantById,

packages/server/src/controllers/chat-messages/index.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import { clearSessionMemory } from '../../utils'
66
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
77
import { FindOptionsWhere } from 'typeorm'
88
import { ChatMessage } from '../../database/entities/ChatMessage'
9+
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
10+
import { StatusCodes } from 'http-status-codes'
911

1012
const createChatMessage = async (req: Request, res: Response, next: NextFunction) => {
1113
try {
12-
if (typeof req.body === 'undefined' || req.body === '') {
13-
throw new Error('Error: chatMessagesController.createChatMessage - request body not provided!')
14+
if (!req.body) {
15+
throw new InternalFlowiseError(
16+
StatusCodes.PRECONDITION_FAILED,
17+
'Error: chatMessagesController.createChatMessage - request body not provided!'
18+
)
1419
}
1520
const apiResponse = await chatMessagesService.createChatMessage(req.body)
1621
return res.json(apiResponse)
@@ -44,8 +49,11 @@ const getAllChatMessages = async (req: Request, res: Response, next: NextFunctio
4449
const startDate = req.query?.startDate as string | undefined
4550
const endDate = req.query?.endDate as string | undefined
4651
const feedback = req.query?.feedback as boolean | undefined
47-
if (typeof req.params.id === 'undefined' || req.params.id === '') {
48-
throw new Error(`Error: chatMessageController.getAllChatMessages - id not provided!`)
52+
if (typeof req.params === 'undefined' || !req.params.id) {
53+
throw new InternalFlowiseError(
54+
StatusCodes.PRECONDITION_FAILED,
55+
`Error: chatMessageController.getAllChatMessages - id not provided!`
56+
)
4957
}
5058
const apiResponse = await chatMessagesService.getAllChatMessages(
5159
req.params.id,
@@ -59,9 +67,6 @@ const getAllChatMessages = async (req: Request, res: Response, next: NextFunctio
5967
messageId,
6068
feedback
6169
)
62-
if (apiResponse.executionError) {
63-
return res.status(apiResponse.status).send(apiResponse.msg)
64-
}
6570
return res.json(apiResponse)
6671
} catch (error) {
6772
next(error)
@@ -90,9 +95,6 @@ const getAllInternalChatMessages = async (req: Request, res: Response, next: Nex
9095
messageId,
9196
feedback
9297
)
93-
if (apiResponse.executionError) {
94-
return res.status(apiResponse.status).send(apiResponse.msg)
95-
}
9698
return res.json(apiResponse)
9799
} catch (error) {
98100
next(error)
@@ -103,8 +105,11 @@ const getAllInternalChatMessages = async (req: Request, res: Response, next: Nex
103105
const removeAllChatMessages = async (req: Request, res: Response, next: NextFunction) => {
104106
try {
105107
const appServer = getRunningExpressApp()
106-
if (typeof req.params.id === 'undefined' || req.params.id === '') {
107-
throw new Error('Error: chatMessagesController.removeAllChatMessages - id not provided!')
108+
if (typeof req.params === 'undefined' || !req.params.id) {
109+
throw new InternalFlowiseError(
110+
StatusCodes.PRECONDITION_FAILED,
111+
'Error: chatMessagesController.removeAllChatMessages - id not provided!'
112+
)
108113
}
109114
const chatflowid = req.params.id
110115
const chatflow = await chatflowsService.getChatflowById(req.params.id)
@@ -139,9 +144,6 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
139144
if (sessionId) deleteOptions.sessionId = sessionId
140145
if (chatType) deleteOptions.chatType = chatType
141146
const apiResponse = await chatMessagesService.removeAllChatMessages(chatId, chatflowid, deleteOptions)
142-
if (apiResponse.executionError) {
143-
res.status(apiResponse.status).send(apiResponse.msg)
144-
}
145147
return res.json(apiResponse)
146148
} catch (error) {
147149
next(error)

0 commit comments

Comments
 (0)