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
@@ -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)}`
)
}
}