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:
Octavian FlowiseAI
2024-04-11 03:11:01 -07:00
committed by GitHub
parent 024b2ad22e
commit d7194e8aaa
98 changed files with 862 additions and 651 deletions
+22 -11
View File
@@ -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)
}
}
}