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
+9 -10
View File
@@ -1,6 +1,9 @@
import { StatusCodes } from 'http-status-codes'
import { addAPIKey, deleteAPIKey, getAPIKeys, updateAPIKey } from '../../utils/apiKey'
import { addChatflowsCount } from '../../utils/addChatflowsCount'
import { getApiKey } from '../../utils/apiKey'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const getAllApiKeys = async () => {
try {
@@ -8,7 +11,7 @@ const getAllApiKeys = async () => {
const dbResponse = await addChatflowsCount(keys)
return dbResponse
} catch (error) {
throw new Error(`Error: apikeyService.getAllApiKeys - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.getAllApiKeys - ${getErrorMessage(error)}`)
}
}
@@ -18,7 +21,7 @@ const createApiKey = async (keyName: string) => {
const dbResponse = await addChatflowsCount(keys)
return dbResponse
} catch (error) {
throw new Error(`Error: apikeyService.createApiKey - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.createApiKey - ${getErrorMessage(error)}`)
}
}
@@ -29,7 +32,7 @@ const updateApiKey = async (id: string, keyName: string) => {
const dbResponse = await addChatflowsCount(keys)
return dbResponse
} catch (error) {
throw new Error(`Error: apikeyService.updateApiKey - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.updateApiKey - ${getErrorMessage(error)}`)
}
}
@@ -39,7 +42,7 @@ const deleteApiKey = async (id: string) => {
const dbResponse = await addChatflowsCount(keys)
return dbResponse
} catch (error) {
throw new Error(`Error: apikeyService.deleteApiKey - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.deleteApiKey - ${getErrorMessage(error)}`)
}
}
@@ -47,16 +50,12 @@ const verifyApiKey = async (paramApiKey: string): Promise<any> => {
try {
const apiKey = await getApiKey(paramApiKey)
if (!apiKey) {
return {
executionError: true,
status: 401,
msg: `Unauthorized`
}
throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, `Unauthorized`)
}
const dbResponse = 'OK'
return dbResponse
} catch (error) {
throw new Error(`Error: apikeyService.verifyApiKey - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.verifyApiKey - ${getErrorMessage(error)}`)
}
}
@@ -1,21 +1,20 @@
import OpenAI from 'openai'
import path from 'path'
import * as fs from 'fs'
import { StatusCodes } from 'http-status-codes'
import { uniqWith, isEqual } from 'lodash'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { Assistant } from '../../database/entities/Assistant'
import { Credential } from '../../database/entities/Credential'
import { getUserHome, decryptCredentialData, getAppVersion } from '../../utils'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const creatAssistant = async (requestBody: any): Promise<any> => {
const createAssistant = async (requestBody: any): Promise<any> => {
try {
const appServer = getRunningExpressApp()
if (!requestBody.details) {
return {
executionError: true,
status: 500,
msg: `Invalid request body`
}
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Invalid request body`)
}
const assistantDetails = JSON.parse(requestBody.details)
try {
@@ -24,22 +23,14 @@ const creatAssistant = async (requestBody: any): Promise<any> => {
})
if (!credential) {
return {
executionError: true,
status: 404,
msg: `Credential ${requestBody.credential} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${requestBody.credential} not found`)
}
// Decrpyt credentialData
const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
const openAIApiKey = decryptedCredentialData['openAIApiKey']
if (!openAIApiKey) {
return {
executionError: true,
status: 404,
msg: `OpenAI ApiKey not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
}
const openai = new OpenAI({ apiKey: openAIApiKey })
@@ -121,11 +112,7 @@ const creatAssistant = async (requestBody: any): Promise<any> => {
requestBody.details = JSON.stringify(newAssistantDetails)
} catch (error) {
return {
executionError: true,
status: 500,
msg: `Error creating new assistant: ${error}`
}
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error creating new assistant - ${getErrorMessage(error)}`)
}
const newAssistant = new Assistant()
Object.assign(newAssistant, requestBody)
@@ -139,7 +126,10 @@ const creatAssistant = async (requestBody: any): Promise<any> => {
})
return dbResponse
} catch (error) {
throw new Error(`Error: assistantsService.creatTool - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: assistantsService.createAssistant - ${getErrorMessage(error)}`
)
}
}
@@ -150,11 +140,7 @@ const deleteAssistant = async (assistantId: string, isDeleteBoth: any): Promise<
id: assistantId
})
if (!assistant) {
return {
executionError: true,
status: 404,
msg: `Assistant ${assistantId} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Assistant ${assistantId} not found`)
}
try {
const assistantDetails = JSON.parse(assistant.details)
@@ -163,22 +149,14 @@ const deleteAssistant = async (assistantId: string, isDeleteBoth: any): Promise<
})
if (!credential) {
return {
executionError: true,
status: 404,
msg: `Credential ${assistant.credential} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${assistant.credential} not found`)
}
// Decrpyt credentialData
const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
const openAIApiKey = decryptedCredentialData['openAIApiKey']
if (!openAIApiKey) {
return {
executionError: true,
status: 404,
msg: `OpenAI ApiKey not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
}
const openai = new OpenAI({ apiKey: openAIApiKey })
@@ -189,15 +167,14 @@ const deleteAssistant = async (assistantId: string, isDeleteBoth: any): Promise<
if (error.status === 404 && error.type === 'invalid_request_error') {
return 'OK'
} else {
return {
executionError: true,
status: 500,
msg: `Error deleting assistant: ${error}`
}
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error deleting assistant - ${getErrorMessage(error)}`)
}
}
} catch (error) {
throw new Error(`Error: assistantsService.deleteTool - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: assistantsService.deleteAssistant - ${getErrorMessage(error)}`
)
}
}
@@ -207,7 +184,10 @@ const getAllAssistants = async (): Promise<any> => {
const dbResponse = await appServer.AppDataSource.getRepository(Assistant).find()
return dbResponse
} catch (error) {
throw new Error(`Error: assistantsService.getAllAssistants - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: assistantsService.getAllAssistants - ${getErrorMessage(error)}`
)
}
}
@@ -218,15 +198,14 @@ const getAssistantById = async (assistantId: string): Promise<any> => {
id: assistantId
})
if (!dbResponse) {
return {
executionError: true,
status: 404,
msg: `Assistant ${assistantId} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Assistant ${assistantId} not found`)
}
return dbResponse
} catch (error) {
throw new Error(`Error: assistantsService.getAssistantById - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: assistantsService.getAssistantById - ${getErrorMessage(error)}`
)
}
}
@@ -238,11 +217,7 @@ const updateAssistant = async (assistantId: string, requestBody: any): Promise<a
})
if (!assistant) {
return {
executionError: true,
status: 404,
msg: `Assistant ${assistantId} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Assistant ${assistantId} not found`)
}
try {
const openAIAssistantId = JSON.parse(assistant.details)?.id
@@ -253,22 +228,14 @@ const updateAssistant = async (assistantId: string, requestBody: any): Promise<a
})
if (!credential) {
return {
executionError: true,
status: 404,
msg: `Credential ${body.credential} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${body.credential} not found`)
}
// Decrpyt credentialData
const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
const openAIApiKey = decryptedCredentialData['openAIApiKey']
if (!openAIApiKey) {
return {
executionError: true,
status: 404,
msg: `OpenAI ApiKey not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
}
const openai = new OpenAI({ apiKey: openAIApiKey })
@@ -346,19 +313,18 @@ const updateAssistant = async (assistantId: string, requestBody: any): Promise<a
const dbResponse = await appServer.AppDataSource.getRepository(Assistant).save(assistant)
return dbResponse
} catch (error) {
return {
executionError: true,
status: 500,
msg: `Error updating assistant: ${error}`
}
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error updating assistant - ${getErrorMessage(error)}`)
}
} catch (error) {
throw new Error(`Error: assistantsService.updateAssistant - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: assistantsService.updateAssistant - ${getErrorMessage(error)}`
)
}
}
export default {
creatAssistant,
createAssistant,
deleteAssistant,
getAllAssistants,
getAssistantById,
@@ -1,5 +1,6 @@
import { FindOptionsWhere } from 'typeorm'
import path from 'path'
import { StatusCodes } from 'http-status-codes'
import { chatType, IChatMessage } from '../../Interface'
import { utilGetChatMessage } from '../../utils/getChatMessage'
import { utilAddChatMessage } from '../../utils/addChatMesage'
@@ -9,6 +10,8 @@ import { getStoragePath } from 'flowise-components'
import { deleteFolderRecursive } from '../../utils'
import logger from '../../utils/logger'
import { ChatMessage } from '../../database/entities/ChatMessage'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
// Add chatmessages for chatflowid
const createChatMessage = async (chatMessage: Partial<IChatMessage>) => {
@@ -16,7 +19,10 @@ const createChatMessage = async (chatMessage: Partial<IChatMessage>) => {
const dbResponse = await utilAddChatMessage(chatMessage)
return dbResponse
} catch (error) {
throw new Error(`Error: chatMessagesService.createChatMessage - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatMessagesService.createChatMessage - ${getErrorMessage(error)}`
)
}
}
@@ -48,7 +54,10 @@ const getAllChatMessages = async (
)
return dbResponse
} catch (error) {
throw new Error(`Error: chatMessagesService.getAllChatMessages - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatMessagesService.getAllChatMessages - ${getErrorMessage(error)}`
)
}
}
@@ -80,7 +89,10 @@ const getAllInternalChatMessages = async (
)
return dbResponse
} catch (error) {
throw new Error(`Error: chatMessagesService.getAllInternalChatMessages - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatMessagesService.getAllInternalChatMessages - ${getErrorMessage(error)}`
)
}
}
@@ -104,7 +116,10 @@ const removeAllChatMessages = async (chatId: string, chatflowid: string, deleteO
const dbResponse = await appServer.AppDataSource.getRepository(ChatMessage).delete(deleteOptions)
return dbResponse
} catch (error) {
throw new Error(`Error: chatMessagesService.removeAllChatMessages - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatMessagesService.removeAllChatMessages - ${getErrorMessage(error)}`
)
}
}
+53 -60
View File
@@ -1,4 +1,6 @@
import path from 'path'
import { StatusCodes } from 'http-status-codes'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { IChatFlow } from '../../Interface'
import { ChatFlow } from '../../database/entities/ChatFlow'
@@ -18,6 +20,7 @@ import { ChatMessage } from '../../database/entities/ChatMessage'
import { ChatMessageFeedback } from '../../database/entities/ChatMessageFeedback'
import { UpsertHistory } from '../../database/entities/UpsertHistory'
import { containsBase64File, updateFlowDataWithFilePaths } from '../../utils/fileRepository'
import { getErrorMessage } from '../../errors/utils'
// Check if chatflow valid for streaming
const checkIfChatflowIsValidForStreaming = async (chatflowId: string): Promise<any> => {
@@ -28,11 +31,7 @@ const checkIfChatflowIsValidForStreaming = async (chatflowId: string): Promise<a
id: chatflowId
})
if (!chatflow) {
return {
executionError: true,
status: 404,
msg: `Chatflow ${chatflowId} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowId} not found`)
}
/*** Get Ending Node with Directed Graph ***/
@@ -44,11 +43,7 @@ const checkIfChatflowIsValidForStreaming = async (chatflowId: string): Promise<a
const endingNodeIds = getEndingNodes(nodeDependencies, graph)
if (!endingNodeIds.length) {
return {
executionError: true,
status: 500,
msg: `Ending nodes not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Ending nodes not found`)
}
const endingNodes = nodes.filter((nd) => endingNodeIds.includes(nd.id))
@@ -59,11 +54,7 @@ const checkIfChatflowIsValidForStreaming = async (chatflowId: string): Promise<a
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.NOT_FOUND, `Ending node ${endingNode.id} data not found`)
}
const isEndingNode = endingNodeData?.outputs?.output === 'EndingNode'
@@ -75,11 +66,7 @@ const checkIfChatflowIsValidForStreaming = async (chatflowId: string): Promise<a
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`)
}
}
@@ -90,7 +77,10 @@ const checkIfChatflowIsValidForStreaming = async (chatflowId: string): Promise<a
const dbResponse = { isStreaming: isEndingNodeExists ? false : isStreaming }
return dbResponse
} catch (error) {
throw new Error(`Error: chatflowsService.checkIfChatflowIsValidForStreaming - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.checkIfChatflowIsValidForStreaming - ${getErrorMessage(error)}`
)
}
}
@@ -100,7 +90,10 @@ const checkIfChatflowIsValidForUploads = async (chatflowId: string): Promise<any
const dbResponse = await utilGetUploadsConfig(chatflowId)
return dbResponse
} catch (error) {
throw new Error(`Error: chatflowsService.checkIfChatflowIsValidForUploads - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.checkIfChatflowIsValidForUploads - ${getErrorMessage(error)}`
)
}
}
@@ -126,7 +119,10 @@ const deleteChatflow = async (chatflowId: string): Promise<any> => {
}
return dbResponse
} catch (error) {
throw new Error(`Error: chatflowsService.getAllChatflows - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.deleteChatflow - ${getErrorMessage(error)}`
)
}
}
@@ -136,7 +132,10 @@ const getAllChatflows = async (): Promise<IChatFlow[]> => {
const dbResponse = await appServer.AppDataSource.getRepository(ChatFlow).find()
return dbResponse
} catch (error) {
throw new Error(`Error: chatflowsService.getAllChatflows - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.getAllChatflows - ${getErrorMessage(error)}`
)
}
}
@@ -151,15 +150,14 @@ const getChatflowByApiKey = async (apiKeyId: string): Promise<any> => {
.orderBy('cf.name', 'ASC')
.getMany()
if (dbResponse.length < 1) {
return {
executionError: true,
status: 404,
msg: `Chatflow not found in the database!`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow not found in the database!`)
}
return dbResponse
} catch (error) {
throw new Error(`Error: chatflowsService.getChatflowByApiKey - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.getChatflowByApiKey - ${getErrorMessage(error)}`
)
}
}
@@ -170,15 +168,14 @@ const getChatflowById = async (chatflowId: string): Promise<any> => {
id: chatflowId
})
if (!dbResponse) {
return {
executionError: true,
status: 404,
msg: `Chatflow ${chatflowId} not found in the database!`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowId} not found in the database!`)
}
return dbResponse
} catch (error) {
throw new Error(`Error: chatflowsService.getAllChatflows - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.getChatflowById - ${getErrorMessage(error)}`
)
}
}
@@ -210,7 +207,10 @@ const saveChatflow = async (newChatFlow: ChatFlow): Promise<any> => {
})
return dbResponse
} catch (error) {
throw new Error(`Error: chatflowsService.saveChatflow - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.saveChatflow - ${getErrorMessage(error)}`
)
}
}
@@ -231,7 +231,10 @@ const updateChatflow = async (chatflow: ChatFlow, updateChatFlow: ChatFlow): Pro
}
return dbResponse
} catch (error) {
throw new Error(`Error: chatflowsService.updateChatflow - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.updateChatflow - ${getErrorMessage(error)}`
)
}
}
@@ -245,19 +248,14 @@ const getSinglePublicChatflow = async (chatflowId: string): Promise<any> => {
if (dbResponse && dbResponse.isPublic) {
return dbResponse
} else if (dbResponse && !dbResponse.isPublic) {
return {
executionError: true,
status: 401,
msg: `Unauthorized`
}
}
return {
executionError: true,
status: 404,
msg: `Chatflow ${chatflowId} not found`
throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, `Unauthorized`)
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowId} not found`)
} catch (error) {
throw new Error(`Error: chatflowsService.getSinglePublicChatflow - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.getSinglePublicChatflow - ${getErrorMessage(error)}`
)
}
}
@@ -270,11 +268,7 @@ const getSinglePublicChatbotConfig = async (chatflowId: string): Promise<any> =>
id: chatflowId
})
if (!dbResponse) {
return {
executionError: true,
status: 404,
msg: `Chatflow ${chatflowId} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowId} not found`)
}
const uploadsConfig = await utilGetUploadsConfig(chatflowId)
// even if chatbotConfig is not set but uploads are enabled
@@ -284,16 +278,15 @@ const getSinglePublicChatbotConfig = async (chatflowId: string): Promise<any> =>
const parsedConfig = dbResponse.chatbotConfig ? JSON.parse(dbResponse.chatbotConfig) : {}
return { ...parsedConfig, uploads: uploadsConfig }
} catch (e) {
return {
executionError: true,
status: 500,
msg: `Error parsing Chatbot Config for Chatflow ${chatflowId}`
}
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error parsing Chatbot Config for Chatflow ${chatflowId}`)
}
}
return 'OK'
} catch (error) {
throw new Error(`Error: chatflowsService.getSinglePublicChatbotConfig - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.getSinglePublicChatbotConfig - ${getErrorMessage(error)}`
)
}
}
@@ -1,5 +1,8 @@
import { cloneDeep } from 'lodash'
import { StatusCodes } from 'http-status-codes'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
// Get all component credentials
const getAllComponentsCredentials = async (): Promise<any> => {
@@ -12,7 +15,10 @@ const getAllComponentsCredentials = async (): Promise<any> => {
}
return dbResponse
} catch (error) {
throw new Error(`Error: componentsCredentialsService.getAllComponentsCredentials - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: componentsCredentialsService.getAllComponentsCredentials - ${getErrorMessage(error)}`
)
}
}
@@ -23,7 +29,8 @@ const getComponentByName = async (credentialName: string) => {
if (Object.prototype.hasOwnProperty.call(appServer.nodesPool.componentCredentials, credentialName)) {
return appServer.nodesPool.componentCredentials[credentialName]
} else {
throw new Error(
throw new InternalFlowiseError(
StatusCodes.NOT_FOUND,
`Error: componentsCredentialsService.getSingleComponentsCredential - Credential ${credentialName} not found`
)
}
@@ -33,13 +40,19 @@ const getComponentByName = async (credentialName: string) => {
if (Object.prototype.hasOwnProperty.call(appServer.nodesPool.componentCredentials, name)) {
dbResponse.push(appServer.nodesPool.componentCredentials[name])
} else {
throw new Error(`Error: componentsCredentialsService.getSingleComponentsCredential - Credential ${name} not found`)
throw new InternalFlowiseError(
StatusCodes.NOT_FOUND,
`Error: componentsCredentialsService.getSingleComponentsCredential - Credential ${name} not found`
)
}
}
return dbResponse
}
} catch (error) {
throw new Error(`Error: componentsCredentialsService.getSingleComponentsCredential - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: componentsCredentialsService.getSingleComponentsCredential - ${getErrorMessage(error)}`
)
}
}
@@ -50,20 +63,23 @@ const getSingleComponentsCredentialIcon = async (credentialName: string) => {
if (Object.prototype.hasOwnProperty.call(appServer.nodesPool.componentCredentials, credentialName)) {
const credInstance = appServer.nodesPool.componentCredentials[credentialName]
if (credInstance.icon === undefined) {
throw new Error(`Credential ${credentialName} icon not found`)
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialName} icon not found`)
}
if (credInstance.icon.endsWith('.svg') || credInstance.icon.endsWith('.png') || credInstance.icon.endsWith('.jpg')) {
const filepath = credInstance.icon
return filepath
} else {
throw new Error(`Credential ${credentialName} icon is missing icon`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Credential ${credentialName} icon is missing icon`)
}
} else {
throw new Error(`Credential ${credentialName} not found`)
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialName} not found`)
}
} catch (error) {
throw new Error(`Error: componentsCredentialsService.getSingleComponentsCredentialIcon - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: componentsCredentialsService.getSingleComponentsCredentialIcon - ${getErrorMessage(error)}`
)
}
}
@@ -1,8 +1,11 @@
import { omit } from 'lodash'
import { StatusCodes } from 'http-status-codes'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { Credential } from '../../database/entities/Credential'
import { transformToCredentialEntity, decryptCredentialData } from '../../utils'
import { ICredentialReturnResponse } from '../../Interface'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const createCredential = async (requestBody: any) => {
try {
@@ -12,7 +15,10 @@ const createCredential = async (requestBody: any) => {
const dbResponse = await appServer.AppDataSource.getRepository(Credential).save(credential)
return dbResponse
} catch (error) {
throw new Error(`Error: credentialsService.createCredential - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: credentialsService.createCredential - ${getErrorMessage(error)}`
)
}
}
@@ -22,15 +28,14 @@ const deleteCredentials = async (credentialId: string): Promise<any> => {
const appServer = getRunningExpressApp()
const dbResponse = await appServer.AppDataSource.getRepository(Credential).delete({ id: credentialId })
if (!dbResponse) {
return {
executionError: true,
status: 404,
msg: `Credential ${credentialId} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found`)
}
return dbResponse
} catch (error) {
throw new Error(`Error: credentialsService.deleteCredential - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: credentialsService.deleteCredential - ${getErrorMessage(error)}`
)
}
}
@@ -61,7 +66,10 @@ const getAllCredentials = async (paramCredentialName: any) => {
}
return dbResponse
} catch (error) {
throw new Error(`Error: credentialsService.getAllCredentials - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: credentialsService.getAllCredentials - ${getErrorMessage(error)}`
)
}
}
@@ -72,11 +80,7 @@ const getCredentialById = async (credentialId: string): Promise<any> => {
id: credentialId
})
if (!credential) {
return {
executionError: true,
status: 404,
msg: `Credential ${credentialId} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found`)
}
// Decrpyt credentialData
const decryptedCredentialData = await decryptCredentialData(
@@ -91,7 +95,10 @@ const getCredentialById = async (credentialId: string): Promise<any> => {
const dbResponse = omit(returnCredential, ['encryptedData'])
return dbResponse
} catch (error) {
throw new Error(`Error: credentialsService.createCredential - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: credentialsService.createCredential - ${getErrorMessage(error)}`
)
}
}
@@ -102,18 +109,17 @@ const updateCredential = async (credentialId: string, requestBody: any): Promise
id: credentialId
})
if (!credential) {
return {
executionError: true,
status: 404,
msg: `Credential ${credentialId} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found`)
}
const updateCredential = await transformToCredentialEntity(requestBody)
await appServer.AppDataSource.getRepository(Credential).merge(credential, updateCredential)
const dbResponse = await appServer.AppDataSource.getRepository(Credential).save(credential)
return dbResponse
} catch (error) {
throw new Error(`Error: credentialsService.updateCredential - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: credentialsService.updateCredential - ${getErrorMessage(error)}`
)
}
}
+15 -3
View File
@@ -1,7 +1,10 @@
import { StatusCodes } from 'http-status-codes'
import { utilGetChatMessageFeedback } from '../../utils/getChatMessageFeedback'
import { utilAddChatMessageFeedback } from '../../utils/addChatMessageFeedback'
import { utilUpdateChatMessageFeedback } from '../../utils/updateChatMessageFeedback'
import { IChatMessageFeedback } from '../../Interface'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
// Get all chatmessage feedback from chatflowid
const getAllChatMessageFeedback = async (
@@ -15,7 +18,10 @@ const getAllChatMessageFeedback = async (
const dbResponse = await utilGetChatMessageFeedback(chatflowid, chatId, sortOrder, startDate, endDate)
return dbResponse
} catch (error) {
throw new Error(`Error: feedbackService.getAllChatMessageFeedback - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: feedbackService.getAllChatMessageFeedback - ${getErrorMessage(error)}`
)
}
}
@@ -25,7 +31,10 @@ const createChatMessageFeedbackForChatflow = async (requestBody: Partial<IChatMe
const dbResponse = await utilAddChatMessageFeedback(requestBody)
return dbResponse
} catch (error) {
throw new Error(`Error: feedbackService.createChatMessageFeedbackForChatflow - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: feedbackService.createChatMessageFeedbackForChatflow - ${getErrorMessage(error)}`
)
}
}
@@ -35,7 +44,10 @@ const updateChatMessageFeedbackForChatflow = async (chatflowId: string, requestB
const dbResponse = await utilUpdateChatMessageFeedback(chatflowId, requestBody)
return dbResponse
} catch (error) {
throw new Error(`Error: feedbackService.updateChatMessageFeedbackForChatflow - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: feedbackService.updateChatMessageFeedbackForChatflow - ${getErrorMessage(error)}`
)
}
}
@@ -1,14 +1,16 @@
import { webCrawl, xmlScrape } from 'flowise-components'
import { StatusCodes } from 'http-status-codes'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const getAllLinks = async (requestUrl: string, relativeLinksMethod: string, queryLimit: string): Promise<any> => {
try {
const url = decodeURIComponent(requestUrl)
if (!relativeLinksMethod) {
return {
executionError: true,
status: 500,
msg: `Please choose a Relative Links Method in Additional Parameters!`
}
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Please choose a Relative Links Method in Additional Parameters!`
)
}
const limit = parseInt(queryLimit)
if (process.env.DEBUG === 'true') console.info(`Start ${relativeLinksMethod}`)
@@ -20,7 +22,10 @@ const getAllLinks = async (requestUrl: string, relativeLinksMethod: string, quer
}
return dbResponse
} catch (error) {
throw new Error(`Error: fetchLinksService.getAllLinks - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: fetchLinksService.getAllLinks - ${getErrorMessage(error)}`
)
}
}
@@ -1,18 +1,17 @@
import { StatusCodes } from 'http-status-codes'
import { findAvailableConfigs } from '../../utils'
import { IReactFlowObject } from '../../Interface'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import chatflowsService from '../chatflows'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const getSingleFlowConfig = async (chatflowId: string): Promise<any> => {
try {
const appServer = getRunningExpressApp()
const chatflow = await chatflowsService.getChatflowById(chatflowId)
if (!chatflow) {
return {
executionError: true,
status: 404,
msg: `Chatflow ${chatflowId} not found in the database!`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowId} not found in the database!`)
}
const flowData = chatflow.flowData
const parsedFlowData: IReactFlowObject = JSON.parse(flowData)
@@ -20,7 +19,10 @@ const getSingleFlowConfig = async (chatflowId: string): Promise<any> => {
const dbResponse = findAvailableConfigs(nodes, appServer.nodesPool.componentCredentials)
return dbResponse
} catch (error) {
throw new Error(`Error: flowConfigService.getSingleFlowConfig - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: flowConfigService.getSingleFlowConfig - ${getErrorMessage(error)}`
)
}
}
@@ -1,5 +1,8 @@
import { Client } from 'langchainhub'
import { StatusCodes } from 'http-status-codes'
import { parsePrompt } from '../../utils/hub'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const createPrompt = async (promptName: string): Promise<any> => {
try {
@@ -13,7 +16,10 @@ const createPrompt = async (promptName: string): Promise<any> => {
}
return dbResponse
} catch (error) {
throw new Error(`Error: loadPromptsService.createPrompt - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: loadPromptsService.createPrompt - ${getErrorMessage(error)}`
)
}
}
@@ -1,5 +1,8 @@
import path from 'path'
import * as fs from 'fs'
import { StatusCodes } from 'http-status-codes'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
// Get all templates for marketplaces
const getAllTemplates = async () => {
@@ -49,7 +52,10 @@ const getAllTemplates = async () => {
const dbResponse = sortedTemplates
return dbResponse
} catch (error) {
throw new Error(`Error: marketplacesService.getAllTemplates - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: marketplacesService.getAllTemplates - ${getErrorMessage(error)}`
)
}
}
@@ -1,6 +1,9 @@
import { StatusCodes } from 'http-status-codes'
import { findAvailableConfigs } from '../../utils'
import { IReactFlowNode } from '../../Interface'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const getAllNodeConfigs = async (requestBody: any) => {
try {
@@ -9,7 +12,10 @@ const getAllNodeConfigs = async (requestBody: any) => {
const dbResponse = findAvailableConfigs(nodes, appServer.nodesPool.componentCredentials)
return dbResponse
} catch (error) {
throw new Error(`Error: nodeConfigsService.getAllNodeConfigs - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: nodeConfigsService.getAllNodeConfigs - ${getErrorMessage(error)}`
)
}
}
+24 -24
View File
@@ -1,9 +1,12 @@
import { cloneDeep } from 'lodash'
import { StatusCodes } from 'http-status-codes'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { INodeData } from '../../Interface'
import { INodeOptionsValue, ICommonObject, handleEscapeCharacters } from 'flowise-components'
import { databaseEntities } from '../../utils'
import logger from '../../utils/logger'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
// Get all component nodes
const getAllNodes = async () => {
@@ -16,7 +19,7 @@ const getAllNodes = async () => {
}
return dbResponse
} catch (error) {
throw new Error(`Error: nodesService.getAllNodes - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: nodesService.getAllNodes - ${getErrorMessage(error)}`)
}
}
@@ -28,10 +31,10 @@ const getNodeByName = async (nodeName: string) => {
const dbResponse = appServer.nodesPool.componentNodes[nodeName]
return dbResponse
} else {
throw new Error(`Node ${nodeName} not found`)
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Node ${nodeName} not found`)
}
} catch (error) {
throw new Error(`Error: nodesService.getAllNodes - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: nodesService.getAllNodes - ${getErrorMessage(error)}`)
}
}
@@ -42,20 +45,23 @@ const getSingleNodeIcon = async (nodeName: string) => {
if (Object.prototype.hasOwnProperty.call(appServer.nodesPool.componentNodes, nodeName)) {
const nodeInstance = appServer.nodesPool.componentNodes[nodeName]
if (nodeInstance.icon === undefined) {
throw new Error(`Node ${nodeName} icon not found`)
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Node ${nodeName} icon not found`)
}
if (nodeInstance.icon.endsWith('.svg') || nodeInstance.icon.endsWith('.png') || nodeInstance.icon.endsWith('.jpg')) {
const filepath = nodeInstance.icon
return filepath
} else {
throw new Error(`Node ${nodeName} icon is missing icon`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Node ${nodeName} icon is missing icon`)
}
} else {
throw new Error(`Node ${nodeName} not found`)
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Node ${nodeName} not found`)
}
} catch (error) {
throw new Error(`Error: nodesService.getSingleNodeIcon - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: nodesService.getSingleNodeIcon - ${getErrorMessage(error)}`
)
}
}
@@ -78,14 +84,13 @@ const getSingleNodeAsyncOptions = async (nodeName: string, requestBody: any): Pr
return []
}
} else {
return {
executionError: true,
status: 404,
msg: `Node ${nodeName} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Node ${nodeName} not found`)
}
} catch (error) {
throw new Error(`Error: nodesService.getSingleNodeAsyncOptions - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: nodesService.getSingleNodeAsyncOptions - ${getErrorMessage(error)}`
)
}
}
@@ -115,21 +120,16 @@ const executeCustomFunction = async (requestBody: any) => {
return dbResponse
} catch (error) {
return {
executionError: true,
status: 500,
msg: `Error running custom function: ${error}`
}
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error running custom function: ${error}`)
}
} else {
return {
executionError: true,
status: 404,
msg: `Node customFunction not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Node customFunction not found`)
}
} catch (error) {
throw new Error(`Error: nodesService.executeCustomFunction - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: nodesService.executeCustomFunction - ${getErrorMessage(error)}`
)
}
}
@@ -1,7 +1,10 @@
import OpenAI from 'openai'
import { StatusCodes } from 'http-status-codes'
import { decryptCredentialData } from '../../utils'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { Credential } from '../../database/entities/Credential'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
// ----------------------------------------
// Assistants
@@ -15,28 +18,23 @@ const getAllOpenaiAssistants = async (credentialId: string): Promise<any> => {
id: credentialId
})
if (!credential) {
return {
executionError: true,
status: 404,
msg: `Credential ${credentialId} not found in the database!`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
}
// Decrpyt credentialData
const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
const openAIApiKey = decryptedCredentialData['openAIApiKey']
if (!openAIApiKey) {
return {
executionError: true,
status: 404,
msg: `OpenAI ApiKey not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
}
const openai = new OpenAI({ apiKey: openAIApiKey })
const retrievedAssistants = await openai.beta.assistants.list()
const dbResponse = retrievedAssistants.data
return dbResponse
} catch (error) {
throw new Error(`Error: openaiAssistantsService.getAllOpenaiAssistants - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: openaiAssistantsService.getAllOpenaiAssistants - ${getErrorMessage(error)}`
)
}
}
@@ -48,21 +46,13 @@ const getSingleOpenaiAssistant = async (credentialId: string, assistantId: strin
id: credentialId
})
if (!credential) {
return {
executionError: true,
status: 404,
msg: `Credential ${credentialId} not found in the database!`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
}
// Decrpyt credentialData
const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
const openAIApiKey = decryptedCredentialData['openAIApiKey']
if (!openAIApiKey) {
return {
executionError: true,
status: 404,
msg: `OpenAI ApiKey not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
}
const openai = new OpenAI({ apiKey: openAIApiKey })
@@ -74,7 +64,10 @@ const getSingleOpenaiAssistant = async (credentialId: string, assistantId: strin
}
return dbResponse
} catch (error) {
throw new Error(`Error: openaiAssistantsService.getSingleOpenaiAssistant - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: openaiAssistantsService.getSingleOpenaiAssistant - ${getErrorMessage(error)}`
)
}
}
@@ -0,0 +1,22 @@
import { Request } from 'express'
import { Server } from 'socket.io'
import { StatusCodes } from 'http-status-codes'
import { utilBuildChatflow } from '../../utils/buildChatflow'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const buildChatflow = async (fullRequest: Request, ioServer: Server) => {
try {
const dbResponse = await utilBuildChatflow(fullRequest, ioServer)
return dbResponse
} catch (error) {
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: predictionsServices.buildChatflow - ${getErrorMessage(error)}`
)
}
}
export default {
buildChatflow
}
+7 -1
View File
@@ -1,7 +1,10 @@
import { StatusCodes } from 'http-status-codes'
import { chatType } from '../../Interface'
import { ChatMessage } from '../../database/entities/ChatMessage'
import { utilGetChatMessage } from '../../utils/getChatMessage'
import { ChatMessageFeedback } from '../../database/entities/ChatMessageFeedback'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
// get stats for showing in chatflow
const getChatflowStats = async (
@@ -36,7 +39,10 @@ const getChatflowStats = async (
return dbResponse
} catch (error) {
throw new Error(`Error: statsService.getChatflowStats - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: statsService.getChatflowStats - ${getErrorMessage(error)}`
)
}
}
+12 -17
View File
@@ -1,8 +1,11 @@
import { StatusCodes } from 'http-status-codes'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { Tool } from '../../database/entities/Tool'
import { getAppVersion } from '../../utils'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const creatTool = async (requestBody: any): Promise<any> => {
const createTool = async (requestBody: any): Promise<any> => {
try {
const appServer = getRunningExpressApp()
const newTool = new Tool()
@@ -16,7 +19,7 @@ const creatTool = async (requestBody: any): Promise<any> => {
})
return dbResponse
} catch (error) {
throw new Error(`Error: toolsService.creatTool - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.createTool - ${getErrorMessage(error)}`)
}
}
@@ -28,7 +31,7 @@ const deleteTool = async (toolId: string): Promise<any> => {
})
return dbResponse
} catch (error) {
throw new Error(`Error: toolsService.deleteTool - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.deleteTool - ${getErrorMessage(error)}`)
}
}
@@ -38,7 +41,7 @@ const getAllTools = async (): Promise<any> => {
const dbResponse = await appServer.AppDataSource.getRepository(Tool).find()
return dbResponse
} catch (error) {
throw new Error(`Error: toolsService.getAllTools - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.getAllTools - ${getErrorMessage(error)}`)
}
}
@@ -49,15 +52,11 @@ const getToolById = async (toolId: string): Promise<any> => {
id: toolId
})
if (!dbResponse) {
return {
executionError: true,
status: 404,
msg: `Tool ${toolId} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Tool ${toolId} not found`)
}
return dbResponse
} catch (error) {
throw new Error(`Error: toolsService.getToolById - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.getToolById - ${getErrorMessage(error)}`)
}
}
@@ -68,11 +67,7 @@ const updateTool = async (toolId: string, toolBody: any): Promise<any> => {
id: toolId
})
if (!tool) {
return {
executionError: true,
status: 404,
msg: `Tool ${toolId} not found`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Tool ${toolId} not found`)
}
const updateTool = new Tool()
Object.assign(updateTool, toolBody)
@@ -80,12 +75,12 @@ const updateTool = async (toolId: string, toolBody: any): Promise<any> => {
const dbResponse = await appServer.AppDataSource.getRepository(Tool).save(tool)
return dbResponse
} catch (error) {
throw new Error(`Error: toolsService.getToolById - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.updateTool - ${getErrorMessage(error)}`)
}
}
export default {
creatTool,
createTool,
deleteTool,
getAllTools,
getToolById,
@@ -1,6 +1,9 @@
import { MoreThanOrEqual, LessThanOrEqual } from 'typeorm'
import { StatusCodes } from 'http-status-codes'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { UpsertHistory } from '../../database/entities/UpsertHistory'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const getAllUpsertHistory = async (
sortOrder: string | undefined,
@@ -46,7 +49,10 @@ const getAllUpsertHistory = async (
return upsertHistory
} catch (error) {
throw new Error(`Error: upsertHistoryServices.getAllUpsertHistory - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: upsertHistoryServices.getAllUpsertHistory - ${getErrorMessage(error)}`
)
}
}
@@ -56,7 +62,10 @@ const patchDeleteUpsertHistory = async (ids: string[] = []): Promise<any> => {
const dbResponse = await appServer.AppDataSource.getRepository(UpsertHistory).delete(ids)
return dbResponse
} catch (error) {
throw new Error(`Error: upsertHistoryServices.patchUpsertHistory - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: upsertHistoryServices.patchDeleteUpsertHistory - ${getErrorMessage(error)}`
)
}
}
@@ -1,5 +1,8 @@
import { StatusCodes } from 'http-status-codes'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { Variable } from '../../database/entities/Variable'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const createVariable = async (newVariable: Variable) => {
try {
@@ -8,7 +11,10 @@ const createVariable = async (newVariable: Variable) => {
const dbResponse = await appServer.AppDataSource.getRepository(Variable).save(variable)
return dbResponse
} catch (error) {
throw new Error(`Error: variablesServices.createVariable - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: variablesServices.createVariable - ${getErrorMessage(error)}`
)
}
}
@@ -18,7 +24,10 @@ const deleteVariable = async (variableId: string): Promise<any> => {
const dbResponse = await appServer.AppDataSource.getRepository(Variable).delete({ id: variableId })
return dbResponse
} catch (error) {
throw new Error(`Error: variablesServices.deleteVariable - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: variablesServices.deleteVariable - ${getErrorMessage(error)}`
)
}
}
@@ -28,7 +37,10 @@ const getAllVariables = async () => {
const dbResponse = await appServer.AppDataSource.getRepository(Variable).find()
return dbResponse
} catch (error) {
throw new Error(`Error: variablesServices.getAllVariables - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: variablesServices.getAllVariables - ${getErrorMessage(error)}`
)
}
}
@@ -40,7 +52,10 @@ const getVariableById = async (variableId: string) => {
})
return dbResponse
} catch (error) {
throw new Error(`Error: variablesServices.getVariableById - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: variablesServices.getVariableById - ${getErrorMessage(error)}`
)
}
}
@@ -51,7 +66,10 @@ const updateVariable = async (variable: Variable, updatedVariable: Variable) =>
const dbResponse = await appServer.AppDataSource.getRepository(Variable).save(tmpUpdatedVariable)
return dbResponse
} catch (error) {
throw new Error(`Error: variablesServices.updateVariable - ${error}`)
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: variablesServices.updateVariable - ${getErrorMessage(error)}`
)
}
}
@@ -1,11 +1,14 @@
import { Request, Response } from 'express'
import { Request } from 'express'
import { StatusCodes } from 'http-status-codes'
import { upsertVector } from '../../utils/upsertVector'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const upsertVectorMiddleware = async (req: Request, res: Response, isInternal: boolean = false) => {
const upsertVectorMiddleware = async (req: Request, isInternal: boolean = false) => {
try {
await upsertVector(req, res, isInternal)
return await upsertVector(req, isInternal)
} catch (error) {
throw new Error(`Error: vectorsService.getRateLimiter - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: vectorsService.upsertVector - ${getErrorMessage(error)}`)
}
}
+6 -11
View File
@@ -1,5 +1,8 @@
import path from 'path'
import * as fs from 'fs'
import { StatusCodes } from 'http-status-codes'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const getVersion = async () => {
try {
@@ -20,11 +23,7 @@ const getVersion = async () => {
}
const packagejsonPath = getPackageJsonPath()
if (!packagejsonPath) {
return {
executionError: true,
status: 404,
msg: 'Version not found'
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Version not found`)
}
try {
const content = await fs.promises.readFile(packagejsonPath, 'utf8')
@@ -33,14 +32,10 @@ const getVersion = async () => {
version: parsedContent.version
}
} catch (error) {
return {
executionError: true,
status: 500,
msg: `Version not found: ${error}`
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Version not found- ${getErrorMessage(error)}`)
}
} catch (error) {
throw new Error(`Error: versionService.getVersion - ${error}`)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: versionService.getVersion - ${getErrorMessage(error)}`)
}
}