Bugfix/Correctly throw 401 error when unauthorized (#2626)

correctly throw 401 error when unauthorized
This commit is contained in:
Henry Heng
2024-06-12 19:10:42 +01:00
committed by GitHub
parent 5ba468b4cc
commit 3ab0d99711
4 changed files with 28 additions and 10 deletions
+8 -1
View File
@@ -55,7 +55,14 @@ const verifyApiKey = async (paramApiKey: string): Promise<string> => {
const dbResponse = 'OK' const dbResponse = 'OK'
return dbResponse return dbResponse
} catch (error) { } catch (error) {
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.verifyApiKey - ${getErrorMessage(error)}`) if (error instanceof InternalFlowiseError && error.statusCode === StatusCodes.UNAUTHORIZED) {
throw error
} else {
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: apikeyService.verifyApiKey - ${getErrorMessage(error)}`
)
}
} }
} }
@@ -236,12 +236,16 @@ const getSinglePublicChatflow = async (chatflowId: string): Promise<any> => {
} }
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowId} not found`) throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowId} not found`)
} catch (error) { } catch (error) {
if (error instanceof InternalFlowiseError && error.statusCode === StatusCodes.UNAUTHORIZED) {
throw error
} else {
throw new InternalFlowiseError( throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR, StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.getSinglePublicChatflow - ${getErrorMessage(error)}` `Error: chatflowsService.getSinglePublicChatflow - ${getErrorMessage(error)}`
) )
} }
} }
}
// Get specific chatflow chatbotConfig via id (PUBLIC endpoint, used to retrieve config for embedded chat) // Get specific chatflow chatbotConfig via id (PUBLIC endpoint, used to retrieve config for embedded chat)
// Safe as public endpoint as chatbotConfig doesn't contain sensitive credential // Safe as public endpoint as chatbotConfig doesn't contain sensitive credential
@@ -417,9 +417,13 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
return result return result
} catch (e) { } catch (e) {
logger.error('[server]: Error:', e) logger.error('[server]: Error:', e)
if (e instanceof InternalFlowiseError && e.statusCode === StatusCodes.UNAUTHORIZED) {
throw e
} else {
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, getErrorMessage(e)) throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, getErrorMessage(e))
} }
} }
}
const utilBuildAgentResponse = async ( const utilBuildAgentResponse = async (
chatflow: IChatFlow, chatflow: IChatFlow,
+7 -4
View File
@@ -22,6 +22,7 @@ import { getRunningExpressApp } from '../utils/getRunningExpressApp'
import { UpsertHistory } from '../database/entities/UpsertHistory' import { UpsertHistory } from '../database/entities/UpsertHistory'
import { InternalFlowiseError } from '../errors/internalFlowiseError' import { InternalFlowiseError } from '../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes' import { StatusCodes } from 'http-status-codes'
import { getErrorMessage } from '../errors/utils'
/** /**
* Upsert documents * Upsert documents
@@ -163,10 +164,12 @@ export const upsertVector = async (req: Request, isInternal: boolean = false) =>
}) })
return upsertedResult['result'] ?? { result: 'Successfully Upserted' } return upsertedResult['result'] ?? { result: 'Successfully Upserted' }
} catch (error) { } catch (e) {
logger.error('[server]: Error:', error) logger.error('[server]: Error:', e)
if (error instanceof Error) { if (e instanceof InternalFlowiseError && e.statusCode === StatusCodes.UNAUTHORIZED) {
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, error.message) throw e
} else {
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, getErrorMessage(e))
} }
} }
} }