Exception handler on embed chat middleware

This commit is contained in:
timothycarambat 2024-08-27 16:27:58 -07:00
parent 47a5c7126c
commit 548da9ade3

View File

@ -41,6 +41,7 @@ async function validEmbedConfigId(request, response, next) {
} }
async function canRespond(request, response, next) { async function canRespond(request, response, next) {
try {
const embed = response.locals.embedConfig; const embed = response.locals.embedConfig;
if (!embed) { if (!embed) {
response.sendStatus(404).end(); response.sendStatus(404).end();
@ -92,7 +93,10 @@ async function canRespond(request, response, next) {
return; return;
} }
if (!isNaN(embed.max_chats_per_day) && Number(embed.max_chats_per_day) > 0) { if (
!isNaN(embed.max_chats_per_day) &&
Number(embed.max_chats_per_day) > 0
) {
const dailyChatCount = await EmbedChats.count({ const dailyChatCount = await EmbedChats.count({
embed_id: embed.id, embed_id: embed.id,
createdAt: { createdAt: {
@ -141,6 +145,17 @@ async function canRespond(request, response, next) {
} }
next(); next();
} catch (e) {
response.status(500).json({
id: uuidv4(),
type: "abort",
textResponse: null,
sources: [],
close: true,
error: "Invalid request.",
});
return;
}
} }
module.exports = { module.exports = {