Skip to content

Commit db25dcc

Browse files
committed
Refactor error handling and code structure in webhook.js
- Improved error handling for fetching knowledge base content. - Streamlined the logic for checking namespace and handling different content types. - Removed redundant code and enhanced readability.
1 parent db9e9c5 commit db25dcc

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

routes/webhook.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var ObjectId = require('mongoose').Types.ObjectId;
1212
const default_embedding = require('../config/kb/embedding');
1313

1414
const port = process.env.PORT || '3000';
15-
let TILEBOT_ENDPOINT = "http://localhost:" + port + "/modules/tilebot/";;
15+
let TILEBOT_ENDPOINT = "http://localhost:" + port + "/modules/tilebot/";
1616
if (process.env.TILEBOT_ENDPOINT) {
1717
TILEBOT_ENDPOINT = process.env.TILEBOT_ENDPOINT + "/"
1818
}
@@ -69,18 +69,12 @@ router.post('/kb/reindex', async (req, res) => {
6969

7070
let content_id = req.body.content_id;
7171

72-
let kb = await KB.findById(content_id).catch( async (err) => {
73-
winston.error("(webhook) Error getting kb content: ", err);
74-
return res.status(500).send({ success: false, error: "Error getting content with id " + content_id });
75-
})
76-
77-
const namespace_id = kb.namespace;
78-
let namespace;
72+
let kb;
7973
try {
80-
namespace = await aiManager.checkNamespace(kb.id_project, namespace_id);
74+
kb = await KB.findById(content_id);
8175
} catch (err) {
82-
let errorCode = err?.errorCode ?? 500;
83-
return res.status(errorCode).send({ success: false, error: err.error });
76+
winston.error("(webhook) Error getting kb content: ", err);
77+
return res.status(500).send({ success: false, error: "Error getting content with id " + content_id });
8478
}
8579

8680
if (!kb) {
@@ -99,8 +93,19 @@ router.post('/kb/reindex', async (req, res) => {
9993
winston.verbose("(webhook) delete response: ", deleteResponse);
10094
return;
10195
}, 10000);
102-
103-
} else if (kb.type === 'sitemap') {
96+
return;
97+
}
98+
99+
const namespace_id = kb.namespace;
100+
let namespace;
101+
try {
102+
namespace = await aiManager.checkNamespace(kb.id_project, namespace_id);
103+
} catch (err) {
104+
let errorCode = err?.errorCode ?? 500;
105+
return res.status(errorCode).send({ success: false, error: err.error });
106+
}
107+
108+
if (kb.type === 'sitemap') {
104109

105110
const urls = await aiManager.fetchSitemap(kb.source).catch((err) => {
106111
winston.error("(webhook) Error fetching sitemap: ", err);

0 commit comments

Comments
 (0)