Skip to content

Commit

Permalink
correctly throw 401 error when unauthorized
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryHengZJ committed Jun 11, 2024
1 parent 5ba468b commit e76c66b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
9 changes: 8 additions & 1 deletion packages/server/src/services/apikey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ const verifyApiKey = async (paramApiKey: string): Promise<string> => {
const dbResponse = 'OK'
return dbResponse
} 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)}`
)
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions packages/server/src/services/chatflows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,14 @@ const getSinglePublicChatflow = async (chatflowId: string): Promise<any> => {
}
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowId} not found`)
} catch (error) {
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.getSinglePublicChatflow - ${getErrorMessage(error)}`
)
if (error instanceof InternalFlowiseError && error.statusCode === StatusCodes.UNAUTHORIZED) {
throw error
} else {
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: chatflowsService.getSinglePublicChatflow - ${getErrorMessage(error)}`
)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/server/src/utils/buildChatflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,11 @@ export const utilBuildChatflow = async (req: Request, socketIO?: Server, isInter
return result
} catch (e) {
logger.error('[server]: Error:', e)
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, getErrorMessage(e))
if (e instanceof InternalFlowiseError && e.statusCode === StatusCodes.UNAUTHORIZED) {
throw e
} else {
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, getErrorMessage(e))
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/server/src/utils/upsertVector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { getRunningExpressApp } from '../utils/getRunningExpressApp'
import { UpsertHistory } from '../database/entities/UpsertHistory'
import { InternalFlowiseError } from '../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes'
import { getErrorMessage } from '../errors/utils'

/**
* Upsert documents
Expand Down Expand Up @@ -163,10 +164,12 @@ export const upsertVector = async (req: Request, isInternal: boolean = false) =>
})

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)
} catch (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))
}
}
}

0 comments on commit e76c66b

Please sign in to comment.