mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 15:00:57 +03:00
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>
This commit is contained in:
committed by
GitHub
parent
024b2ad22e
commit
d7194e8aaa
@@ -1,5 +1,8 @@
|
||||
import { StatusCodes } from 'http-status-codes'
|
||||
import { ChatFlow } from '../database/entities/ChatFlow'
|
||||
import { InternalFlowiseError } from '../errors/internalFlowiseError'
|
||||
import { getRunningExpressApp } from '../utils/getRunningExpressApp'
|
||||
import { getErrorMessage } from '../errors/utils'
|
||||
|
||||
export const addChatflowsCount = async (keys: any) => {
|
||||
try {
|
||||
@@ -28,6 +31,6 @@ export const addChatflowsCount = async (keys: any) => {
|
||||
}
|
||||
return tmpResult
|
||||
} catch (error) {
|
||||
throw new Error(`Error: addChatflowsCount - ${error}`)
|
||||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: addChatflowsCount - ${getErrorMessage(error)}`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { IFileUpload, getStoragePath, convertSpeechToText, ICommonObject } from
|
||||
import { StatusCodes } from 'http-status-codes'
|
||||
import { IncomingInput, IMessage, INodeData, IReactFlowObject, IReactFlowNode, IDepthQueue, chatType, IChatMessage } from '../Interface'
|
||||
import path from 'path'
|
||||
import { InternalFlowiseError } from '../errors/internalFlowiseError'
|
||||
import { ChatFlow } from '../database/entities/ChatFlow'
|
||||
import { Server } from 'socket.io'
|
||||
import { getRunningExpressApp } from '../utils/getRunningExpressApp'
|
||||
@@ -47,11 +48,7 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
|
||||
id: chatflowid
|
||||
})
|
||||
if (!chatflow) {
|
||||
return {
|
||||
executionError: true,
|
||||
status: StatusCodes.NOT_FOUND,
|
||||
msg: `Chatflow ${chatflowid} not found`
|
||||
}
|
||||
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowid} not found`)
|
||||
}
|
||||
|
||||
const chatId = incomingInput.chatId ?? incomingInput.overrideConfig?.sessionId ?? uuidv4()
|
||||
@@ -60,11 +57,7 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
|
||||
if (!isInternal) {
|
||||
const isKeyValidated = await utilValidateKey(req, chatflow)
|
||||
if (!isKeyValidated) {
|
||||
return {
|
||||
executionError: true,
|
||||
status: StatusCodes.UNAUTHORIZED,
|
||||
msg: `Unauthorized`
|
||||
}
|
||||
throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, `Unauthorized`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,11 +182,7 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
|
||||
const directedGraph = graph
|
||||
const endingNodeIds = getEndingNodes(nodeDependencies, directedGraph)
|
||||
if (!endingNodeIds.length) {
|
||||
return {
|
||||
executionError: true,
|
||||
status: 500,
|
||||
msg: `Ending nodes not found`
|
||||
}
|
||||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Ending nodes not found`)
|
||||
}
|
||||
|
||||
const endingNodes = nodes.filter((nd) => endingNodeIds.includes(nd.id))
|
||||
@@ -203,11 +192,7 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
|
||||
for (const endingNode of endingNodes) {
|
||||
const endingNodeData = endingNode.data
|
||||
if (!endingNodeData) {
|
||||
return {
|
||||
executionError: true,
|
||||
status: 500,
|
||||
msg: `Ending node ${endingNode.id} data not found`
|
||||
}
|
||||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Ending node ${endingNode.id} data not found`)
|
||||
}
|
||||
|
||||
const isEndingNode = endingNodeData?.outputs?.output === 'EndingNode'
|
||||
@@ -219,11 +204,7 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
|
||||
endingNodeData.category !== 'Agents' &&
|
||||
endingNodeData.category !== 'Engine'
|
||||
) {
|
||||
return {
|
||||
executionError: true,
|
||||
status: 500,
|
||||
msg: `Ending node must be either a Chain or Agent`
|
||||
}
|
||||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Ending node must be either a Chain or Agent`)
|
||||
}
|
||||
|
||||
if (
|
||||
@@ -231,11 +212,10 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
|
||||
Object.keys(endingNodeData.outputs).length &&
|
||||
!Object.values(endingNodeData.outputs ?? {}).includes(endingNodeData.name)
|
||||
) {
|
||||
return {
|
||||
executionError: true,
|
||||
status: 500,
|
||||
msg: `Output of ${endingNodeData.label} (${endingNodeData.id}) must be ${endingNodeData.label}, can't be an Output Prediction`
|
||||
}
|
||||
throw new InternalFlowiseError(
|
||||
StatusCodes.INTERNAL_SERVER_ERROR,
|
||||
`Output of ${endingNodeData.label} (${endingNodeData.id}) must be ${endingNodeData.label}, can't be an Output Prediction`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,11 +291,7 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
|
||||
? reactFlowNodes.find((node: IReactFlowNode) => endingNodeIds[0] === node.id)
|
||||
: reactFlowNodes[reactFlowNodes.length - 1]
|
||||
if (!nodeToExecute) {
|
||||
return {
|
||||
executionError: true,
|
||||
status: 404,
|
||||
msg: `Node not found`
|
||||
}
|
||||
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Node not found`)
|
||||
}
|
||||
|
||||
if (incomingInput.overrideConfig) {
|
||||
@@ -417,10 +393,6 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
|
||||
return result
|
||||
} catch (e: any) {
|
||||
logger.error('[server]: Error:', e)
|
||||
return {
|
||||
executionError: true,
|
||||
status: 500,
|
||||
msg: e.message
|
||||
}
|
||||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, e.message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { StatusCodes } from 'http-status-codes'
|
||||
import { INodeParams } from 'flowise-components'
|
||||
import { ChatFlow } from '../database/entities/ChatFlow'
|
||||
import { getRunningExpressApp } from '../utils/getRunningExpressApp'
|
||||
import { IUploadFileSizeAndTypes, IReactFlowNode } from '../Interface'
|
||||
import { INodeParams } from 'flowise-components'
|
||||
import { InternalFlowiseError } from '../errors/internalFlowiseError'
|
||||
|
||||
/**
|
||||
* Method that checks if uploads are enabled in the chatflow
|
||||
@@ -12,7 +14,9 @@ export const utilGetUploadsConfig = async (chatflowid: string): Promise<any> =>
|
||||
const chatflow = await appServer.AppDataSource.getRepository(ChatFlow).findOneBy({
|
||||
id: chatflowid
|
||||
})
|
||||
if (!chatflow) return `Chatflow ${chatflowid} not found`
|
||||
if (!chatflow) {
|
||||
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowid} not found`)
|
||||
}
|
||||
|
||||
const uploadAllowedNodes = ['llmChain', 'conversationChain', 'mrklAgentChat', 'conversationalAgent']
|
||||
const uploadProcessingNodes = ['chatOpenAI', 'chatAnthropic', 'awsChatBedrock', 'azureChatOpenAI']
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Request, Response } from 'express'
|
||||
import { Request } from 'express'
|
||||
import * as fs from 'fs'
|
||||
import { cloneDeep, omit } from 'lodash'
|
||||
import { ICommonObject } from 'flowise-components'
|
||||
@@ -20,14 +20,15 @@ import { IncomingInput, INodeDirectedGraph, IReactFlowObject, chatType } from '.
|
||||
import { ChatFlow } from '../database/entities/ChatFlow'
|
||||
import { getRunningExpressApp } from '../utils/getRunningExpressApp'
|
||||
import { UpsertHistory } from '../database/entities/UpsertHistory'
|
||||
import { InternalFlowiseError } from '../errors/internalFlowiseError'
|
||||
import { StatusCodes } from 'http-status-codes'
|
||||
|
||||
/**
|
||||
* Upsert documents
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {boolean} isInternal
|
||||
*/
|
||||
export const upsertVector = async (req: Request, res: Response, isInternal: boolean = false) => {
|
||||
export const upsertVector = async (req: Request, isInternal: boolean = false) => {
|
||||
try {
|
||||
const appServer = getRunningExpressApp()
|
||||
const chatflowid = req.params.id
|
||||
@@ -36,11 +37,15 @@ export const upsertVector = async (req: Request, res: Response, isInternal: bool
|
||||
const chatflow = await appServer.AppDataSource.getRepository(ChatFlow).findOneBy({
|
||||
id: chatflowid
|
||||
})
|
||||
if (!chatflow) return res.status(404).send(`Chatflow ${chatflowid} not found`)
|
||||
if (!chatflow) {
|
||||
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowid} not found`)
|
||||
}
|
||||
|
||||
if (!isInternal) {
|
||||
const isKeyValidated = await utilValidateKey(req, chatflow)
|
||||
if (!isKeyValidated) return res.status(401).send('Unauthorized')
|
||||
if (!isKeyValidated) {
|
||||
throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, `Unauthorized`)
|
||||
}
|
||||
}
|
||||
|
||||
const files = (req.files as any[]) || []
|
||||
@@ -89,11 +94,14 @@ export const upsertVector = async (req: Request, res: Response, isInternal: bool
|
||||
|
||||
// Check if multiple vector store nodes exist, and if stopNodeId is specified
|
||||
if (vsNodes.length > 1 && !stopNodeId) {
|
||||
return res.status(500).send('There are multiple vector nodes, please provide stopNodeId in body request')
|
||||
throw new InternalFlowiseError(
|
||||
StatusCodes.INTERNAL_SERVER_ERROR,
|
||||
'There are multiple vector nodes, please provide stopNodeId in body request'
|
||||
)
|
||||
} else if (vsNodes.length === 1 && !stopNodeId) {
|
||||
stopNodeId = vsNodes[0].data.id
|
||||
} else if (!vsNodes.length && !stopNodeId) {
|
||||
return res.status(500).send('No vector node found')
|
||||
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, 'No vector node found')
|
||||
}
|
||||
|
||||
const { graph } = constructGraphs(nodes, edges, { isReversed: true })
|
||||
@@ -154,9 +162,12 @@ export const upsertVector = async (req: Request, res: Response, isInternal: bool
|
||||
stopNodeId
|
||||
}
|
||||
})
|
||||
return res.status(201).json(upsertedResult['result'] ?? { result: 'Successfully Upserted' })
|
||||
} catch (e: any) {
|
||||
logger.error('[server]: Error:', e)
|
||||
return res.status(500).send(e.message)
|
||||
|
||||
return upsertedResult['result'] ?? { result: 'Successfully Upserted' }
|
||||
} catch (error) {
|
||||
logger.error('[server]: Error:', error)
|
||||
if (error instanceof Error) {
|
||||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user