From cf21c56231c2c865dc0d20036ed2a804fae2b4c1 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Fri, 26 Aug 2022 18:50:02 -0400 Subject: [PATCH] A bit more error handling. Seeing if we can consistently inform the frontend about request errors. Also, trying to outright crash with slightly less frequency. --- app.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/app.js b/app.js index e85e939..c88aafd 100644 --- a/app.js +++ b/app.js @@ -24,12 +24,13 @@ const buildCluster = (clusterConfig, connect = true) => { const kafka = new Kafka(kafkaConfig) const admin = kafka.admin() - admin.connect().catch(console.error) - return c({ + const cluster = { kafka, admin, config: clusterConfig - }) + } + admin.connect().catch(e => console.error(cluster.error = e.toString())) + return cluster } const clusters = @@ -50,15 +51,19 @@ app.use(express.static(path.join(__dirname, 'public'))); const router = express.Router() +process.on('unhandledRejection', console.error) + /* GET topics listing. */ router.get('/topics/:cluster', async (req, res, _next) => { const legalName = topicName => !topicName.startsWith("__") - const topicList = await clusters[req.params.cluster]?.admin.listTopics() - if (topicList) { + try { + const topicList = (await clusters[req.params.cluster]?.admin.listTopics() || []) res.send(topicList.filter(legalName)) - } else { - res.send([]) - //res.status(400).send({error: 'Invalid cluster name'}) + } catch (e) { + res.status(502).send({ + error: `Could not connect to cluster '${req.params.cluster}'`, + errorDetails: e.toString() + }) } }) @@ -75,6 +80,7 @@ const getClusterData = () => })) router.get('/clusters', async (req, res, _next) => { + console.log('/clusters') res.send(getClusterData()) }) @@ -88,7 +94,6 @@ router.post('/clusters', async (req, res, _next) => { }) router.post('/clusters/delete', async (req, res, _next) => { - console.log('/clusters/delete post body', req.body) const clusterName = req.body.clusterName // TODO: Disconnect delete clusters[clusterName] @@ -98,10 +103,7 @@ router.post('/clusters/delete', async (req, res, _next) => { }) router.put('/clusters', async (req, res, _next) => { - console.log('/clusters put body', req.body) - const hasPlaceholderPassword = req.body.sasl.password === passwordPlaceholder - console.log('Has placeholder password:', hasPlaceholderPassword) const clusterName = req.body.clusterName if (hasPlaceholderPassword) { req.body.password = config.clusters[clusterName].password @@ -167,14 +169,12 @@ wsServer.on('connection', socket => { }) socket.on('message', async message => { - socket.send('Loading...') + console.log('socket message') + //socket.send('Loading...') message = JSON.parse(message) const currentMode = message.mode === 'realTime' ? realTimeSearch : search - console.log('CLUSTERS before run', clusters) - console.log('message.cluster', message.cluster) const cluster = clusters[message.cluster] - console.log('clusters[message.cluster]', cluster) const run = async () => consumers.set(socket, await currentMode({ kafka: cluster.kafka, searchCode: message.searchCode,